LA 3998 Prime k-tuple
题意:如果K个相邻素数p1,p2,p3.....pk满足pk-p1=s,称这些素数组成一个距离为s的素数K元组,输入a,b,k,s,输出区间[a,b]内距离为s的素数k元组的个数。
思路:先打到50000素数表,然后暴力求出a,b区间的素数,然后判断。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; int t;
int a,b,k,s,cnt;
bool vis[];
int f[];
int p[];
void getprime()
{
cnt=;
memset(vis,,sizeof(vis));
vis[]=true;
vis[]=true;
for(int i=; i<=; i++)
{
if(!vis[i])
{
f[cnt++]=i;
for(int j=i*; j<=; j+=i)
{
vis[j]=true;
}
}
}
} int main()
{
getprime();
scanf("%d\n",&t);
while(t--)
{
scanf("%d%d%d%d",&a,&b,&k,&s);
memset(p,,sizeof(p));
int ans=;
int t1=;
for(int i=a; i<=b; i++)
{
if(i<=)
{
if(!vis[i]) p[t1++]=i;
}
else
{
bool flag=true;
for(int j=; j<cnt&&f[j]*f[j]<=i; j++)
{
if(i%f[j]==)
{
flag=false;
break;
}
}
if(flag)
{
p[t1++]=i;
}
}
}
for(int i=; i<t1; i++)
{
if(p[i+k-]-p[i]==s)
{
ans++;
}
}
printf("%d\n",ans);
}
return ;
}
LA 3998 Prime k-tuple的更多相关文章
- 3998 - Prime k-tuple
{p1,..., pk : p1 < p2 <...< pk} is called a prime k -tuple of distance s if p1, p2,..., pk ...
- (算法入门经典大赛 优先级队列)LA 3135(之前K说明)
A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor da ...
- .Uva&LA部分题目代码
1.LA 5694 Adding New Machine 关键词:数据结构,线段树,扫描线(FIFO) #include <algorithm> #include <cstdio&g ...
- hdu4935 Prime Tree(2014多校联合第七场)
首先这是一道dp题,对题意的把握和对状态的处理是解题关键. 题目给出的范围是n在1到1e11之间,由于在裂变过称中左儿子总是父亲节点的一个非平凡约数,容易看出裂变过程只与 素数幂有关,并且显然有素数不 ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- POJ2739 Sum of Consecutive Prime Numbers(尺取法)
POJ2739 Sum of Consecutive Prime Numbers 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
- ZOJ 3707 Calculate Prime S 数论
思路:容易得到s[n]=s[n-1]+s[n-2],也就是fib数. 求第k小的fib质数的也就是第k个质数数-2,当k>2时. 在就是s[n]/x%m=s[n]%(x*m)/x. 代码如下: ...
- Project Euler:Problem 87 Prime power triples
The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...
- Java 的 Tuple 元组数据类型
元组类型,即 Tuple 常在脚本语言中出现,例如 Scala 的 ("Unmi", "china@qq.com", "blahbla"). ...
随机推荐
- Struts 有哪些经常使用标签库
Struts 有哪些经常使用标签库 1.html标签库 2.bean标签库 3.logic标签库
- 《大话操作系统——做坚实的project实践派》(6)
继续写硬件体系.这个不写完.不会写操作系统内核.由于根基不正,则难于达到上层境地.
- oracle 异常管理
命名的系统异常 产生原因 access_into_null 未定义对象 CASE_NOT_FOUND CASE 中若未包含相应的 WHEN ,并且没有设置 ELSE ...
- TFS 服务器更换后工作区无法绑定
需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...
- android 如何解决模块之间的通讯的耦合问题
使用EventBus http://wuyexiong.github.io/blog/2013/04/30/android-fragment/ http://yunfeng.sinaapp.com/? ...
- WPF里面的常用笔刷
程序运行效果 <Window x:Class="This_brush.MainWindow" xmlns="http://schemas.microsoft.com ...
- MYSQL命令行连接数据库
连接数据库 mysql -uroot -proot -P3306 -Dmydemo # 参数详解 -uuser 用户user -ppwd 密码 -P3306 端口 -Dmysql 数据库 #显示所有数 ...
- 一个中型项目:本地校园App
好暨: 这个项目的起源于课堂老师作业的要求.老师要求一年下来完成一个构想并实现Demo.思考良久,在要提交构想的那个晚上,想到了校园App,当时团队只有两个人,但我感觉到:就是它了!项目启动时间——2 ...
- How far away ?
#include<iostream> #include <algorithm> using namespace std; const int M=40010; int dis[ ...
- photoshop cc 版本安装失败解决办法
好久没有碰ps,看了下在ps版本都到cc了.忍不住也想尝试最新版本,但是安装出现了很多问题,导致我花了很多时间才搞定,现在分享给大家几点经验吧. Exit Code: Please see speci ...