P1050 [NOIP2005 普及组] 循环

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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云儿乱飘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值