vijos Superprime

11月noip之后我估计就没有机会再更博客了,所以我现在打算多水几篇博客

描述

农民约翰的母牛总是生产出最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。

农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说:
7 3 3 1
全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。

7331 被叫做长度 4 的特殊质数。

写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数。数字1不被看作一个质数。

格式

输入格式

单独的一行包含N。

输出格式

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

并按大小顺序排列(从小到大).

样例1

样例输入1

4

样例输出1

2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393

提示

很简单,不要先算出来在交表-_-~~

来源

原题来自USACO

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int n;
int p[12][100];
//直接枚举超时了...
//如果一个k位的数是要求的数,那么它的前k-1位也是这种数 
bool zhishu(int x)
{
	if(x == 1)return false;
	else
	{
		for(int i = 2;i <= sqrt(x);i++)
		{
			if(x % i == 0)return false;
		}
		return true;		
	} 
}
int main()
{
	scanf("%d",&n);
/*	for(int i = 1;i <= n - 1;i++)
	{
		l = l * 10;
	}
	for(int i = l * 2;i <= 10 * l;i++)
	{
		int s = i;
		while(1)
		{
			if(s == 1)break;
			if(zhishu(s) == false)s = s / 10;
			else break;
			if(s == 0)
			{
				printf("%d\n",i);
				break;
			}
		}
	}
*/
	int tot = 4,t = 0;
	p[1][1] = 2;//n = 1的情况特殊处理 
    p[1][2] = 3;
    p[1][3] = 5;
    p[1][4] = 7;
    for(int k = 2;k <= n;k++)
	{
		for(int i = 1;i <= 9;i++)
		{
			for(int j = 1;j <= tot;j++)
			{
				if(zhishu(p[k - 1][j] * 10 + i))p[k][++t] = p[k - 1][j] * 10 + i;
			}
		}
		tot = t;
		t = 0;
	} 
	sort(p[n] + 1,p[n] + tot + 1);//二维数组的强大排序方式
	for(int i = 1;i <= tot;i++)
	{
		printf("%d\n",p[n][i]);
	} 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42914224/article/details/82871756