P1050 [NOIP2005 普及组] 循环 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class BigInt : public vector<int> {
public:
BigInt() { push_back(0); }
BigInt(int n, int v) : vector<int>(n, v) {}
BigInt(int i) {
push_back(i);
process_digit();
return;
}
BigInt(string &s, int k) {
for (int i = 1; i <= k; i++) {
if (i > s.size()) push_back(0);
else push_back(s[s.size() - i] - '0');
}
/*for (int i = 0, j = min(k - 1, (int)s.size() - 1); i < k; i++, j--) {
push_back(s[j] - '0');
}*/
}
BigInt& operator*=(int x) {
for (int i = 0; i < size(); i++) {
at(i) *= x;
}
process_digit();
return *this;
}
BigInt operator*(const BigInt &a) {
BigInt ret(min(Maxlen, int(size() + a.size() - 1)), 0);
for (int i = 0; i < size(); i++) {
for (int j = 0; j < a.size(); j++) {
if (i + j >= Maxlen) continue;
ret[i + j] += at(i) * a[j];
}
}
ret.process_digit();
return ret;
}
static int Maxlen;
private:
void process_digit() {
for (int i = 0; i < size(); i++) {
if (at(i) < 10) continue;
if (i + 1 < Maxlen) {
if (i + 1 == size()) push_back(0);
at(i + 1) += at(i) / 10;
}
at(i) %= 10;
}
return;
}
};
ostream& operator<<(ostream& out, const BigInt& a) {
for (int i = a.size() - 1; i >= 0; i--) {
out << a[i];
}
return out;
}
int BigInt::Maxlen = 0;
int main() {
string s;
int k;
cin >> s >> k;
BigInt::Maxlen = k;
BigInt n(s, k);
BigInt pre_y = n, y;
vector<int> arr;
for (int i = 0; i < n.size(); i++) {
y = pre_y;
int cnt = 1;
while ((y * n).at(i) != n[i]) {
y = y * pre_y;
cnt++;
if (cnt == 11) {
cout << "-1" << endl;
return 0;
}
}
arr.push_back(cnt);
pre_y = y;
}
BigInt ans = 1;
for (auto x : arr) {
ans *= x;
}
cout << ans << endl;
return 0;
}