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 题目鬼图 刚开始看到题目的图觉得这题 ...
随机推荐
- linux中chage命令的基本使用
在Linux中chage命令常用于设置系统用户的账户属性 Usage: chage [options] LOGIN Options: -d, --lastday LAST_DAY set date o ...
- QQ空间技术架构之深刻揭秘
QQ空间技术架构之深刻揭秘 来源: 腾讯大讲堂 发布时间: 2012-05-17 17:24 阅读: 7822 次 推荐: 4 [收藏] QQ 空间作为腾讯海量互联网服务产品,经过近七年 ...
- Kafka入门教程(二)
转自:https://blog.csdn.net/yuan_xw/article/details/79188061 Kafka集群环境安装 相关下载 JDK要求1.8版本以上. JDK安装教程:htt ...
- Scala(七)【异常处理】
目录 一.try-catch-finally 二.Try(表达式).getOrElse(异常出现返回的默认值) 三. 直接抛出异常 一.try-catch-finally 使用场景:在获取外部链接的时 ...
- 【leetcode】170. Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations: add and find. add ...
- Oracle中dbms_random包详解
Oracle之DBMS_RANDOM包详解参考自:https://www.cnblogs.com/ivictor/p/4476031.html https://www.cnblogs.com/shen ...
- 【编程思想】【设计模式】【行为模式Behavioral】备忘录模式Memento
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/memento.py #!/usr/bin/env pyt ...
- 【编程思想】【设计模式】【行为模式Behavioral】迭代器模式iterator
Python版 https://github.com/faif/python-patterns/blob/master/behavioral/iterator.py #!/usr/bin/env py ...
- 4.Vue.js-起步
Vue.js 起步 每个 Vue 应用都需要通过实例化 Vue 来实现. 语法格式如下: var vm = new Vue({ // 选项 }) 接下来让我们通过实例来看下 Vue 构造器中需要哪些内 ...
- shell脚本 mysqldump方式全备份mysql
一.简介 源码地址 日期:2018/10/8 介绍:mysqldump方式全备份脚本,并保存固定天数的全备份 效果图: 二.使用 适用:centos6+ 语言:中文 注意:使用前先查看脚本,修改对应变 ...