// 演算法 / Algorithm:
// 從 n 開始逐一往上枚舉每個整數 x，計算各位數字乘積，
// 回傳第一個乘積能被 t 整除的 x。帶 0 的數字保證答案存在。
// Enumerate x upward from n, compute digit product, return the first x whose
// product is divisible by t. A 0 digit guarantees an answer exists nearby.

class Solution {
public:
    int smallestNumber(int n, int t) {
        // 從 n 開始，一直往上枚舉，直到 return / enumerate upward from n until we return
        for (int x = n; ; ++x) {
            long long product = 1;  // 乘積初始化為 1 / init product to 1 (multiplicative identity)

            // 用副本 cur 逐位拆解 x，保留 x 本身
            // Use a copy `cur` to peel digits, leaving x untouched.
            for (int cur = x; cur > 0; cur /= 10)   // cur /= 10 每輪去掉最右一位 / drop the last digit each round
                product *= cur % 10;                // cur % 10 是最右一位，乘進 product / last digit, multiplied in

            // product % t == 0 代表能被整除；找到就回傳
            // product % t == 0 means divisible; return once found.
            if (product % t == 0)
                return x;   // 第一個符合條件的數字即為最小答案 / the first match is the smallest answer
        }
    }
};
