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 题目鬼图 刚开始看到题目的图觉得这题 ...
随机推荐
- typedef 的用法
[2]typedef (1)在C语言中,允许使用关键字typedef定义新的数据类型 其语法如下: typedef <已有数据类型> <新数据类型>; 如: typedef i ...
- Slay 全场!Erda 首次亮相 GopherChina 大会
来源|尔达 Erda 公众号 相关视频:https://www.bilibili.com/video/BV1MV411x7Gm 2021 年 6 月 26 日,GopherChina 大会准时亮相北京 ...
- day9 图书设计项目
总路由层url from django.conf.urls import url from django.contrib import admin from app01 import views ur ...
- git stash命令及提交指定文件
一.git stash命令 常用git stash命令: (1)git stash save "save message" : 执行存储时,添加备注,方便查找,只有git stas ...
- Linux系统根目录下各文件夹介绍
参考自:[1]Linux 系统根目录下各个文件夹的作用 https://www.cnblogs.com/jiangfeilong/p/10538795.html[2]了解Linux根目录"/ ...
- jvm的优化
a) 设置参数,设置jvm的最大内存数 b) 垃圾回收器的选择
- vue2 页面路由
vue官方文档 src/views/Login.vue <template> <div> <h2>登录页</h2> </div> </ ...
- ligerUI 关闭父弹窗JS报错问题 解决方法
1:调用父窗口某一个文件框,获取焦点, parent.window.document.getElementById("roleName").focus(); 2:关闭父窗口pare ...
- Mysql配置文件 扩展详细配置
目录 配置文件中有些特定参数 扩展配置 max_connections connect_timeout interactive_timeout|wait_timeout net_retry_count ...
- Linux的编译安装、压缩打包、定时任务
昨日内容回顾 # 1.搭建yum私有仓库 1.安装必须的工具 yum install createrepo yum-utils nginx -y 2.创建目录 mkdir /opt/test 3.创建 ...