首页 技术 正文
技术 2022年11月15日
0 收藏 568 点赞 2,564 浏览 1339 个字

有人问我这个问题。

个人感觉暴搜会TLE O(n*sqrt(n))。n=100000000;(推断素数用2~sqrt(n)+1 去除)

还是枚举好了。

枚举 1~10000,把他每一位存下来,回文数已知 left 。求 right ,然后组合起来。

比如 1 ,推断 11 是否素数。

比如 10 。推断 101 是否素数, 推断 1001 是否素数。

这样复杂度就是 O(n^2)。

開始我 bool pa[100000000] 准备用标记来确定。

结果MLE。

然后算了一下 总共同拥有多少个数,最多 781 个。 int pa[1000] 就能够装下了。

G++ 15ms

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR0(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)#define SIZE 100000000 +1
using namespace std;int pa[1000];
int cot;
bool prime(int n)
{
FOR(i,2,sqrt(n)+2)
if(n%i==0)return 0;
return 1;
}void PA()
{
cot=0;
pa[cot++]=2;
pa[cot++]=3;
pa[cot++]=5;
pa[cot++]=7;
FOR(i,1,10000)
{
int num[5];
int len=0;
int m=i;
while(m)
{
int tmp=m%10;
num[len++]=tmp;
m/=10;
}
int ans=i;
if(len>1)
{
FOR(r,1,len)
ans=ans*10+num[r];
if(prime(ans))
pa[cot++]=ans;
}
ans=i;
FOR(r,0,len)
ans=ans*10+num[r];
if(prime(ans))
pa[cot++]=ans;
}
}int main()
{
PA();
int a,b;
while(~sf("%d%d",&a,&b))
{
FOR(i,0,cot)
if(pa[i]>=a&&pa[i]<=b)pf("%d\n",pa[i]);
pf("\n");
}
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,020
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,772
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,850