PTA ---- 特立独行的幸福_第1张图片

#include 
#include 
int m,n,c;
int num[10001],flag[10001],pri[10001];
int get(int d) {
    if(num[d] != -1) return num[d];
    num[d] = -2;
    int t = d,sum = 0;
    while(t) {
        sum += (t % 10) * (t % 10);
        t /= 10;
    }
    t = get(sum);
    if(t >= 0) flag[sum] = 1;
    return num[d] = t == -2 ? t : t + 1;
}
int main() {
    memset(num,-1,sizeof(num));
    pri[0] = pri[1] = 1;
    for(int i = 2;i * i <= 10000;i ++) {
        if(pri[i]) continue;
        for(int j = i * i;j <= 10000;j += i) {
            pri[j] = 1;
        }
    }
    num[1] = 0;
    scanf("%d %d",&m,&n);
    for(int i = m;i <= n;i ++) {
        get(i);
    }
    for(int i = m;i <= n;i ++) {
        if(num[i] != -2 && !flag[i]) {
            printf("%d %d\n",i,num[i] * (2 - pri[i]));
            c ++;
        }
    }
    if(!c) printf("SAD");
    return 0;
}