题解:CF2132B The Secret Number

· · 题解

洛谷CF2132B || CodeForces 2132 B

简要题意

对一个数 x 进行操作,在其末尾加上若干个 0 变成数字 y,最后令 n=x+y。现给出 n,请反解出所有可能的 x

思路

注意到 n\le10^{18},且 n=x+y=x+x\times10^k=x(10^k+1),那么 k 最大至 17。因此我们可以枚举可能的 k,如果存在 x=\frac{n}{10^k+1} 为整数则记录下,最后统一输出即可。

如果最后发现数组内无元素,代表无解,记得输出 0

#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll t, n;
int main()
{
    cin >> t;
    while (t--)
    {
        cin >> n;
        ll a[1005], tot = 0; ll i = 11;
        while (n >= i)
        {
            if (n % i == 0) a[++tot] = n / i;
            i = (i - 1) * 10 + 1;
        }
        cout << tot << endl;
        if (tot == 0) continue;
        for (int j = tot; j >= 1; j--) cout << a[j] << " ";
        cout << endl;
    }
}