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"). ...
随机推荐
- POJ 1737 统计有n个顶点的连通图有多少个 (带标号)
设f(n)为所求答案 g(n)为n个顶点的非联通图 则f(n) + g(n) = h(n) = 2^(n * (n - 1) / 2) 其中h(n)是n个顶点的联图的个数 这样计算 先考虑1所在的连通 ...
- jquery+正則表達式验证邮箱格式的样例
js: $("#email").blur(function(){ //获取id相应的元素的值,去掉其左右的空格 var email = $.trim($('#email').val ...
- 开始lisp的旅程
不知道是不是<黑客与画家>的老pual太能忽悠了,一直想把他吹捧的lisp学习一下. 看common lisp和On lisp两本书也有一段时间了,中间还夹着看了一点SICP和land o ...
- 搜索框中“请输入搜索keyword”
$(function(){ $("#ctl00_txtKey").val("请输入搜索keyword").addClass("search&qu ...
- Myeclipse快捷键的设置
- C#,MVC视图中把枚举转成DropdownList
1.拓展EnumHelper public static class EnumHelper { // Get the value of the description attribute if the ...
- 读取xml字符串
string strXml = @"<MessageData><pm_id>10</pm_id><pm_title>这是公司或者产品的名称&l ...
- JAVA除去制定字符的方法
只需调用replaceAll()方法: public class Test { public static void main(String[] args) { String s= "abc ...
- org.springframework.beans.factory.BeanCreationException: 求教育!
2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...
- CentOS 7重装mysql编译过程报错解决方法
错误记录: [ 82%] Building C object libmysql/CMakeFiles/clientlib.dir/__/sql-common/client.c.o/usr/local/ ...