3998 - Prime k-tuple
{p1,..., pk : p1 < p2 <...< pk} is called a prime k -tuple of distance s if p1, p2,..., pk are consecutive prime numbers and pk - p1 = s . For example, with k = 4 , s = 8 , {11, 13, 17, 19} is a prime 4-tuple of distance 8.
Given an interval [a, b] , k , and s , your task is to write a program to find the number of prime k -tuples of distance s in the interval [a, b] .
Input
The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.
For each data set, there is only one line containing 4 numbers, a , b , k and s (a, b < 2 * 109, k < 10, s < 40) .
Output
For each test case, write in one line the numbers of prime k -tuples of distance s .
Sample Input
1
100 200 4 8
Sample Output
2
思路:区间筛素数;
数据就是很水,没给定【a,b】区间的大小,然后筛法水过。然后统计的时候,维护两个端点就行了。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<math.h>
7 using namespace std;
8 typedef long long LL;
9 bool prime[100005];
10 bool prime_max[20*1000000];
11 int ans[100005];
12 LL ac[1000007];
13 int main(void)
14 {
15 memset(prime,0,sizeof(prime));
16 int i,j;
17 int cn = 0;
18 for(i = 2; i < 1000; i++)
19 if(!prime[i])
20 for(j = i; (i*j) < 100005; i++)
21 prime[i*j] = true;
22 for(i = 2; i < 100005; i++)
23 {
24 if(!prime[i])
25 ans[cn++] = i;
26 }
27 int n;
28 scanf("%d",&n);
29 LL a,b,k,t;
30 while(n--)
31 {
32 scanf("%lld %lld %lld %lld",&a,&b,&k,&t);
33 memset(prime_max,0,sizeof(prime_max));
34 LL s ;
35 for(i = 0; i < cn; i++)
36 {
37 for(s = max(2LL,a/ans[i])*ans[i]; s <= b; s += ans[i])
38 {
39 if(s >= a)
40 prime_max[s-a] = true;
41 }
42 }
43 int v = 0;
44 for(s = a; s <= b; s++)
45 {
46 if(!prime_max[s-a])
47 ac[v++] = s;
48 }
49 int l = 0;
50 int r = k-1;
51 LL ask = 0;
52 while(true)
53 {
54 if(r >= v)
55 break;
56 if(ac[r] - ac[l] == t)
57 {
58 ask++;
59 }
60 l++;
61 r++;
62 }
63 printf("%lld\n",ask);
64 }
65 return 0;
66 }
3998 - Prime k-tuple的更多相关文章
- LA 3998 Prime k-tuple
题意:如果K个相邻素数p1,p2,p3.....pk满足pk-p1=s,称这些素数组成一个距离为s的素数K元组,输入a,b,k,s,输出区间[a,b]内距离为s的素数k元组的个数. 思路:先打到500 ...
- 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"). ...
- F. Relatively Prime Powers (求([2,n],内不是次方的数量)
题目:经过提炼后, 题目的意思就是问[2,n] 内,不是次方数的数量 ,: 思路: 答案就是 原理是利用容斥,注意n开i次根是向下取整(这题巨卡精度) 这是大神的思路 ,, 我还没有理解, 先放着,等 ...
- 【Poj3126】【BNUOJ3245】Prime Path
http://poj.org/problem?id=3126 https://www.bnuoj.com/v3/problem_show.php?pid=3245 题目鬼图 刚开始看到题目的图觉得这题 ...
随机推荐
- 19.Happy Number-Leetcode
Write an algorithm to determine if a number is "happy". A happy number is a number defined ...
- Hadoop入门 完全分布式运行模式-准备
目录 Hadoop运行环境 完全分布式运行模式(重点) scp secure copy 安全拷贝 1 hadoop102上的JDK文件推给103 2 hadoop103从102上拉取Hadoop文件 ...
- The Go tools for Windows + Assembler很好玩
我想用python做个tiny BASIC编译器.赋值和加减乘除,IF和FOR. 语法分析python有ply包,用ply.lex和ply.yacc做个计算器很简单,我已经做了. 做个解释器应该也不难 ...
- day11 四层负载均衡和http协议
day11 四层负载均衡和http协议 四层负载均衡和七层负载均衡的区别 四层和七层负载均衡的区别 四层负载均衡数据包在底层就进行了分发,而七层负载均衡数据包则是在最顶层进行分发.由此可以看出,七层负 ...
- Spark基础:(七)Spark Streaming入门
介绍 1.是spark core的扩展,针对实时数据流处理,具有可扩展.高吞吐量.容错. 数据可以是来自于kafka,flume,tcpsocket,使用高级函数(map reduce filter ...
- Web安全学习二
目录 常见漏洞攻防 SQL注入 注入分类 按技巧分类 按获取数据的方式分类 注入检测 权限提升 数据库检测 绕过技巧 CheatSheet SQL Server Payload MySQL Paylo ...
- Linux—禁止用户SSH登录方法总结
Linux-禁止用户SSH登录方法总结 一.禁止用户登录 1.修改用户配置文件/etc/shadow 将第二栏设置为"*",如下.那么该用户就无法登录.但是使用这种方式 ...
- API接口设计之token、timestamp、sign 具体架构与实现(APP/小程序,传输安全)
Java生鲜电商平台-API接口设计之token.timestamp.sign 具体设计与实现 说明:在实际的业务中,难免会跟第三方系统进行数据的交互与传递,那么如何保证数据在传输过程中的安全呢(防窃 ...
- MyBatis(3):优化MyBatis配置文件
一.连接数据库的配置单独放在一个properties文件中 1,创建一个database.properties driver=com.mysql.jdbc.Driver url=jdbc:mysql: ...
- redis入门到精通系列(八):redis的高可用--主从复制详解
(一)主从复制介绍 前面所讲的关于redis的操作都属于单机操作,单机操作虽然操作简单,但是处理能力有限,无法高可用.所谓高可用性,就是指当一台服务器宕机的时候,有备用的服务器能顶替上,在单机操作上这 ...