题目链接:
https://nanti.jisuanke.com/t/31452
AC代码(看到不好推的定理就先打表!!!!):
#include<bits/stdc++.h>
using namespace std;
# define maxn 40000+100
int a[maxn];
int b[maxn];
int ans=0;
char s[1000];
int c[maxn];
int ti=0;
void f()
{
memset(c,0,sizeof(c));
memset(b,0,sizeof(b));
memset(a,0,sizeof(a));
for(int i=2; i<=maxn; i++)
{
if(a[i])continue;
b[i]=1;
c[++ans]=i;
for(int j=i; j<=maxn; j+=i)
{
a[j]=1;
}
}
}
bool judge(int t)
{
int y=t;
int t1=0,t2=0,t3=0,t4=0,t5=0,t6=0;
int num=0;
int w[220];
memset(w,0,sizeof(w));
while(t)
{
int g=t%10;
w[++num]=g;
if(b[g]==0)return false;
t=t/10;
}
if(num==3)
{
t1=w[num]*10+w[num-1];
t2=w[num-1]*10+w[num-2];
t3=w[num-1]*10+w[num];
t4=w[num-2]*10+w[num-1];
t5=w[1]*10+w[3];
t6=w[3]*10+w[1];
}
if(t1!=0&&b[t1]==0)return false;
if(t2!=0&&b[t2]==0)return false;
if(t3!=0&&b[t3]==0)return false;
if(t4!=0&&b[t4]==0)return false;
if(t5!=0&&b[t5]==0)return false;
if(t6!=0&&b[t6]==0)return false;
printf("Case #%d: %d
",++ti,y);
return true;
}
int main()
{
f();
b[1]=1;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
int len=strlen(s);
if(len>=4)printf("Case #%d: %d
",++ti,317);
else
{
int temp=0;
for(int i=0; i<len; i++)
{
temp=temp*10+(s[i]-'0');
}
int t=lower_bound(c+1,c+ans+1,temp)-c;
if(c[t]!=temp)
{
t--;
for(int j=t; j>=1; j--)
{
if(judge(c[j]))break;
}
}
else
{
for(int j=t; j>=1; j--)
{
if(judge(c[j]))break;
}
}
}
}
return 0;
}