A - Sequence with Digits

A - Sequence with Digits

\(题意:给定a_1,递推式如下:a_n=a_{n-1}+max\{{a_{n-1}\}}*min\{{a_{n-1}\}},其中max\{{a_{n-1}\}}/min\{{a_{n-1}\}}表示\)\(a_{n-1}的各个位数中最大/最小的那个。给定k,求第k项。\)

思路:

就硬算……稍微思考一下就能发现如果位数中有出现0的话,\(a_n\)就会重复下去,就在这里剪枝即可。

void solve() {
	LL a1, k;
	set<LL> s;
	cin >> a1 >> k;
	for (int i = 1; i < k; i++) {
		s.clear();
		LL x = a1;
		while (x) { s.insert(x % 10); x /= 10; }
		LL p = a1;
		a1 = a1 + *s.begin() * *(--s.end());
		if (p == a1) break;//如果与前一项相同,以后的项都是这个值
	}
	cout << a1 << endl;
}

猜你喜欢

转载自www.cnblogs.com/streamazure/p/12907936.html