1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
#include <iostream> #include <string> #include <vector> #include <string.h> using namespace std; int prime[1000001]; vector <long long> vb; long long sumof(long long a) { long long l=0; while(a) { l+=a%10; a/=10; } return l; } int isPrime(long long n) { if(n<1000001) { if(prime[n]==0) return 1; else return 0; } for(int i=2;i*i<=n;i++) if(n%i==0) return 0; return 1; } int factorsum(long long n) { if(n<1000001) { if(prime[n]==0) return 1; } long long tmp=0; long long org=n; while(n) { int f=0; for(int i=0;i<vb.size();i++) { if(vb[i]>n) { f=0; break; } if(n%vb[i]==0) { tmp+=sumof(vb[i]); n/=vb[i]; f=1; break; } } if(f==0) { if(isPrime(n)==1 && n!=org) tmp+=sumof(n); break; } } //cout<<tmp<<" ds\n"; if(tmp==0) return 1; if(tmp==sumof(org)) return 0; return 1; } int main() { prime[0]=1; prime[1]=1; for(int i=4;i<1000001;i+=2) { prime[i]=1; } for(int i=3;i*i<1000001;i+=2) { if(prime[i]==0) { for(int j=i+i;j<1000001;j+=i) prime[j]=1; } } //vb.clear(); vb.push_back(2); for(int i=3;i<1000001;i+=2) { if(prime[i]==0) vb.push_back(i); } int t; long long n; //cout<<vb[vb.size()-1]<<"\n"; //cout<<isPrime(1000000007)<<"\n"; //cout<<factorsum(1000000007)<<"\n"; //cout<<prime[45495]<<"\n"; //cout<<sumof(456502); cin>>t; while(t--) { cin>>n; n++; while(factorsum(n)) { n++; } cout<<n<<"\n"; } //getchar(); return 0; } |