注意到 pp 为奇数时 10q+1  10pq+110^q + 1\ |\ 10^{pq} + 1,于是答案必定是 22 的非负整数次幂,逐个判断即可。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 12;
int a[N];
int main()
{
	string n;
	while (cin >> n)
	{
		int s = n.size();
		int b = -1;
		for (int i = 1; i <= s; i <<= 1)
		{
			for (int j = 0; j <= 2 * s; j++)
				a[j] = 0;
			for (int j = 0, k = s - 1; k >= 0; j++, k--)
				a[j] = n[k] - '0';
			for (int j = 0; j <= s - 1; j++)
			{
				a[j + i] -= a[j];
				a[j] = 0;
				if (a[j + i] < 0)
				{
					a[j + i] += 10;
					a[j + i + 1]--;
				}
			}
			bool ok = true;
			for (int j = 0; j <= 2 * s; j++)
				if (a[j]) ok = false;
			if (ok)
			{
				b = i;
				break;
			}
		}
		cout << b << '\n';
	}
	return 0;
}