题目:
测试点 4出的问题(后面的点可能被前面点筛过,而不是单纯的只会更改前面的点)
对拍了一晚上获得的一个不对的数据
36 139
对于这题的其他一些想法:可以用树来解决,树根为1,可以加入线性筛优化
下面是ac代码
#include<bits/stdc++.h>
using namespace std;
int a, b;
unordered_map<int, int> mp;
unordered_set<int> ck;
int check(int x) {
int now = x, cnt = 1;
unordered_set<int> st;
while(x != 1) {
if(mp.count(x)) {
int n = mp[x];
mp.erase(x);
return n + cnt;
}
if(st.count(x)) return -1;
if(x != now) st.insert(x), ++cnt,ck.insert(x);
int mid = 0;
while(x) {
mid += (x % 10) * (x % 10);
x /= 10;
}
x = mid;
}
return cnt;
}
bool is_prime(int x) {
if(x == 2 || x == 3) return true;
if((x + 1) % 6 && (x - 1) % 6 || x < 2) return false;
for(int i = 2; i <= sqrt(x); ++i) if(!(x % i)) return false;
return true;
}
int main() {
cin >> a >> b;
for(int i = a; i <= b; ++i) {
int x = check(i);
if(x != -1) mp[i] = x;
}
for(int i = a; i <= b; ++i) {
if(!mp.count(i) || ck.count(i)) continue;
if(is_prime(i)) mp[i] *= 2;
cout << i << " " << mp[i] << endl;
}
if(!mp.size()) cout << "SAD";
return 0;
}