USACO 1.5.3 – SuperPrime Rib


#include <iostream>
#include <cstdio>
using namespace std;

typedef long long int LL;

int isp(LL n) {
	if (n == 1) return 0;
	for (LL i = 2; i * i <= n; i++)
		if (!(n % i)) return 0;
	return 1;
}
void dfs(LL n, int d, int D){
	if(!isp(n))
		return;
	if(d==D)
		printf("%lld\n", n);
	else
		for(int i=1; i<10; i+=2)
			dfs(n*10+i, d+1, D);

}

int main() {
	int D;
	freopen("sprime.in", "r", stdin), freopen("sprime.out", "w", stdout);
	scanf("%d", &D);
	dfs(2, 1, D), dfs(3, 1, D), dfs(5, 1, D), dfs(7, 1, D);
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: