HDU 1431 思维 基础数论

找范围内回文素数,最大到1e8,我就是要枚举回文串,再判素数,然后因为这种弱智思路死磕了很久题目。

/** @Date    : 2017-09-08 15:24:43
  * @FileName: HDU 1431 思维 找回文素数.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL pri[N];
bool vis[N];
int c = 0;

void prime()
{
	MMF(pri);
	MMF(vis);
	for(int i = 2; i < N; i++)
	{
		if(!vis[i])
			pri[c++] = i;
		for(int j = 0; j < c && i * pri[j] < N; j++)
		{
			vis[i * pri[j]] = 1;
			if(i % pri[j] == 0) break;
		}
	}
}

int isk[2010];

int main()
{
	prime();
	//freopen("x.txt","w", stdout);
	LL a, b;
	while(cin >> a >> b)
	{
		for(int i = 0; pri[i] < 10; i++)
			if(pri[i] >= a && pri[i] <= b)
				printf("%lld\n", pri[i]);
		priority_queue<int, vector<int>, greater<int>>q;
		for(int i = 1; i < 10000; i++)
		{
			int flag = log10(i);
			for(int k = -1; k <= 9; k++)
			{
				int s = i * pow(10, flag + 1);
				if(k > -1)
					s = s * 10 + k * pow(10, flag + 1);
				int t = i;
				int cnt = flag;
				//cout << s << "~";;
				while(t)
				{
					s += (t % 10) * pow(10, cnt);
					t /= 10;
					cnt--;
				}
				if(s < a || s > b)
					continue;
				int flag = 0;
				for(int j = 0; j < c && pri[j] * pri[j] <= s; j++)
					if(s % pri[j] == 0)
					{
						flag = 1;
						break;
					}
				if(!flag)
					q.push(s)/*,cout << s << endl*/;
				
			}
		}
		while(!q.empty())
		{
			printf("%d\n", q.top());
			q.pop();
		}
		printf("\n");
	}	
    return 0;
}
posted @ 2017-09-08 21:17  Lweleth  阅读(127)  评论(0编辑  收藏  举报