UVA 10533(prime number)
Solution: #include <iostream> #include <vector> using namespace std; int main(){ vector<bool> prime(1000001,1); vector<int> digitPrime(1000001,0); int digitPrimeNum=0; prime[0]=0; prime[1]=0; for(int i=2;i<=1000000;i++){ if(prime[i]){ int temp=i,sum=0; while(temp){ sum+=(temp%10); temp/=10; }if(prime[sum]){ digitPrime[i]=++digitPrimeNum; } for(int j=i+i;j<=1000000;j+=i){ prime[j]=0; } } } int N; cin>>N; while(N--){ int t1,t2; cin>>t1>>t2; int result=0; while(t1<t2){ if(!digitPrime[t1]){ t1++; } if(!digitPrime[t2]){ t2--; } if(digitPrime[t2]&&digitPrime[t1]){ result=digitPrime[t2]-digitPrime[t1]; break; } } if(digitPrime[t1]){ result++; } cout<<result<<endl; }return 0; } /* Input: 3 10 20 10 100 100 10000 Output: 1 10 576 */