PTA ---- 特立独行的幸福

PTA ----  特立独行的幸福

#include <stdio.h>
#include <string.h>
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;
}
上一篇:【算法学习笔记】筛法(算法翻译类)


下一篇:INCA_ProF脚本教程