HDU 1431 素数回文
有人问我这个问题。
个人感觉暴搜会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");
}
}
HDU 1431 素数回文的更多相关文章
- F题 hdu 1431 素数回文
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1431 素数回文 Time Limit: 2000/1000 MS (Java/Others) M ...
- 题解报告:hdu 1431 素数回文
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1431 Problem Description xiaoou33对既是素数又是回文的数特别感兴趣.比如说 ...
- hdu 1431 素数回文(暴力打表,埃托色尼筛法)
这题开始想时,感觉给的范围5 <= a < b <= 100,000,000太大,开数组肯定爆内存,而且100000000也不敢循环,不超时你打我,反正我是不敢循环. 这题肯定得打表 ...
- HDU 1431 素数回文 离线打表
题目描述:给定一个区间,将这个区间里所有既是素数又是回文数的数输出来. 题目分析:这题的这个数据范围比较大,达到了10^8级别,而且输入的数据有多组,又因为判断一个数是否是回文数貌似只有暴力判断,时间 ...
- 素数回文(hdu1431)
素数回文 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 素数回文(dfs,有bug)
素数回文 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 4N - 素数回文
xiaoou33对既是素数又是回文的数特别感兴趣.比如说151既是素数又是个回文.现在xiaoou333想要你帮助他找出某个范围内的素数回文数,请你写个程序找出 a 跟b 之间满足条件的数.(5 &l ...
- 12--c完数/最大公约数/最小公倍数/素数/回文数
完数/最大公约数/最小公倍数/素数/回文数 2015-04-08 10:33 296人阅读 评论(0) 收藏 举报 分类: C/C++(60) 哈尔滨工业大学(8) 版权声明:本文为博主原创文章 ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
随机推荐
- 使用 git post-receive 钩子部署服务端代码
在 git 中提交服务器源码的时候,如果能够直接更新到测试服务器,并且重启服务使其生效,会节省懒惰的程序员们大量的时间. git 的 Server-side hook (服务端钩子/挂钩)可以用来做件 ...
- 另一鲜为人知的单例写法-ThreadLocal
另一鲜为人知的单例写法-ThreadLocal 源代码范例 当我阅读FocusFinder和Choreographer的时候,我发现这两类的单例实现和我们寻经常使用双重检查锁非常不一样.而是用来一个T ...
- RHEL7 MariaDB测试
MariaDB安装及启动: yum groupinstall mariadb 启动mariadb systemctl restart mariadb systemctl enable mariadb ...
- [已解决]通过多层nginx,tomcat服务无法获取外网真实IP
问题描述: 使用腾讯的御天验证码,提示IP非法IP,内网的tomcat,经过2层nginx代理,服务获取的IP地址为内网ip地址,由于腾讯云的御天验证码对单一IP的频繁访问有拦截的,认定为非法IP(刷 ...
- Ubuntu16.04下的英文词典Artha
地址: http://artha.sourceforge.net http://artha.sourceforge.net/wiki/index.php/Download 在Ubuntu下可以直接安 ...
- update pm storage
BEGIN #Routine body goes here... INSERT INTO EMS_PM_STORAGE ( AMOID, GP_BEGIN_TIME, EMS_RECORD_TIME, ...
- 【php】分享一个php转换微信、QQ、微博 特殊非主流 | 杀马特 网名的 function
1.实现的效果: 2.将上面带有图标.非主流的文字,转换成utf-8格式,能存进去和读出来. 3.code: function emoji_encode($nickname){ $strEncode ...
- 一个worker thread服务一个客户端
服务器端对一个客户端来了就开启一个工作线程,最多可接受64个. 具体看代码: #pragma once #include <winsock.h> #include <stdio.h& ...
- 【DeepLearning】Exercise:Convolution and Pooling
Exercise:Convolution and Pooling 习题链接:Exercise:Convolution and Pooling cnnExercise.m %% CS294A/CS294 ...
- Nginx配置Awstats分析Nginx日志笔记
1.修改Nginx日志格式: log_format json '$remote_addr - $remote_user [$time_local] "$request" ' ...