2080 特殊的质数肋骨 USACO
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题解
题目描述 Description
农民约翰的母牛总是产生最好的肋骨。 你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。 农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说: 7 3 3 1 全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。 7331 被叫做长度 4 的特殊质数。 写一个程序对给定的肋骨的数目 N(1<=N<=8),求出所有的特殊质数。 数字1不被看作一个质数。

输入描述 Input Description
单独的一行包含N。

输出描述 Output Description
按顺序输出长度为 N 的特殊质数,每行一个。

样例输入 Sample Input
4

样例输出 Sample Output
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393


【分析】
首位只能是 2 3 5 7,其他位只能是 1 3 7 9,然后裸搜。


【代码】

//codevs 特殊质数肋骨 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define fo(i,j,k) for(int i=j;i<=k;i++)
using namespace std;
int n,tp,r;
int a[5]={0,2,3,5,7};
int b[5]={0,1,3,7,9};
//int po[9]={0,1,10,100,1000,10000,100000,1000000,10000000};
int ans[10000001];
inline bool judge(int x)
{
int tmp=sqrt(x);
fo(i,2,tmp)
if(x%i==0) return 0;
return 1;
}
inline void dfs(int s)
{
if(!judge(r)) return;
if(s==0)
{
if(judge(r))
ans[++tp]=r;
return;
}
fo(i,1,4)
{
r=r*10+b[i];
dfs(s-1);
r=(r-b[i])/10;
}
}
int main()
{
scanf("%d",&n);
fo(i,1,4)
{
r+=a[i];
dfs(n-1);
r-=a[i];
}
fo(i,1,tp) printf("%d\n",ans[i]);
return 0;
}