回文质数(各个数字倍数的特征)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<sstream>
using namespace std;

const int N=1e7+10;//有点nb ,8位回文数一定是11的倍数 

int a,b;
int pri[N],cnt;
bool st[N];

void get_primes(int n)
{
	for(int i=2;i<=n;i++)
	{
		if(!st[i])
			pri[cnt++]=i;
		for(int j=0;pri[j]<=n/i;j++)
		{
			st[pri[j]*i]=true;
			if(i%pri[j]==0)	
				break;
		}
	}
}

bool check(int x)
{
//	stringstream ss;
//	ss<<x;
//	string s;
//	ss>>s;
	string s=to_string(x);//第一种写法非常费时间! 
	for(int i=0,j=s.size()-1;i<j;i++,j--)
		if(s[i]!=s[j])
			return false;
	return true;
}

void solve()
{
	for(int i=0;i<cnt;i++)
	{
		if(pri[i]>=a&&pri[i]<=b&&check(pri[i]))
		{
			printf("%d\n",pri[i]);
		}
	}
}
	

int main()
{
	cin>>a>>b;
	get_primes(b);
	solve();
	return 0;
}
posted @ 2021-02-18 15:57  30天CF上蓝!!!  阅读(139)  评论(0编辑  收藏  举报