Solve of UVA 12802: Gift From the Gods
#include<bits/stdc++.h>
using namespace std;
int prime[1000100]={0};
void pri()
{
long int n,i,j,k;
prime[0]=prime[1]=1;
for(i=0;i<1000100;i++)
{
if(!prime[i])
{
for(j=2*i;j<1000100;j+=i)
prime[j]=1;
}
}
prime[1]=0;
}
bool pal(long int x)
{
long int n,i,j,k=0;
n=x;
while(n)
{
i=n%10;
k=k*10+i;
n/=10;
}
if(x==k) return true;
else return false;
}
int main()
{
long int n,i,j,k,l;
pri();
while(scanf("%ld" , &n)==1)
{
if(pal(n)==true && prime[n]==0)
{
printf("%ld\n",2*n);
return 0;
}
else printf("%ld\n",2*n);
}
return 0;
}
No comments:
Post a Comment