https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858886

题解:

暴力

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,temp,sum;
int a[N];
char str;
int pre[N];
bool prime[N];
bool vis[N];
int val[N];
int flag[N];
struct node{};
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    //scanf("%d",&t);
    //while(t--){
    prime[0]=prime[1]=1;
    for(int i=2;i<=10000;i++){
        if(!prime[i]){
            pre[++cnt]=i;//cout<<i<<endl;
        }//cout<<t<<endl;
        for(int j=1;j<=cnt&&i*pre[j]<=10000;j++){
            prime[i*pre[j]]=1;
            if(i%pre[j]==0){
                break;
            }
        }
    }

    scanf("%d%d",&l,&r);
    for(int i=l;i<=r;i++){
        t=i;
        cnt=0;
        while(t!=1){
            flag[t]++;
            if(cnt>100)
                break;
            int T=t/10000;
            int a=t/1000%10;
            int b=t/100%10;
            int c=t/10%10;
            int d=t%10;
            cnt++;
            t=T*T+a*a+b*b+c*c+d*d;

        }
        if(cnt<=100)
            vis[i]=1,val[i]=prime[i]?cnt:2*cnt;
    }
    ans=1;
    for(int i=l;i<=r;i++){
        if(vis[i]&&flag[i]==1){
            ans=0;
            printf("%d %d\n",i,val[i]);
        }
    }
    if(ans)
        cout<<"SAD"<<endl;
    //}

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}