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"). ...
随机推荐
- [Flux] 3. Actions
Actions contain no functionality, but rather describe an event in our application. In this lesson we ...
- JMX 与系统管理--转
前言 在 Java 程序的运行过程中,对 JVM 和系统的监测一直是 Java 开发人员在开发过程所需要的.一直以来,Java 开发人员必须通过一些底层的 JVM API,比如 JVMPI 和 JVM ...
- 趣谈iOS运行时的方法调用原理
一个成熟的计算机语言必然有丰富的体系,复杂的容错机制,处理逻辑以及判断逻辑.但这些复杂的逻辑都是围绕一个主线丰富和展开的,所以在学习计算机语言的时候,先掌握核心,然后了解其原理,明白程序语言设计的实质 ...
- HTML设置固定页脚飘浮
Css /* 页脚 */.footSty{bottom: 0pt; margin: 0pt; position: fixed; width: 100%; z-index: 10 ! important ...
- Android性能优化之ViewStub
http://www.cnblogs.com/lwbqqyumidi/p/4047108.html ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Andro ...
- 如何用visual studio控件(repeater)绑定数据库(SQL server)信息并显示
今天学习了下如何间接绑定数据库网上看了很多信息,都云里雾里,没有图片说明,初学者完全看不懂,我自己做了一个DEMO,相信可以帮到大家! 一.建立数据库,并构建表信息,我的表信息如下: 表中的数据在数据 ...
- angularjs ngTable -Custom filter template-calendar
jsp页面: <script type="text/ng-template" id="path/to/your/filters/top-Date-One.html& ...
- Oracle (内连接)
例如: 表xuesheng id name 1, Jack 2, Tom 3, Kity 4, nono 表kaoshi id grade 1, 56 2, 76 11, 89 内连接(显示两表匹配的 ...
- redhat6.4 配置centos6 yum替换
1.卸载掉系统redhat自带的yum rpm -qa |grep yum |xargs rpm -e --nodeps 2 下载相关的centos yum插件 主要有python-inipa ...
- 在xcode6.1和ios10.10.1环境下实现app发布
之前写过在xcode6.1和ios10.10.1环境下实现真机测试,以及最近提交的app一直在审核当中,所以木有发布如何实现app发布来分享给大家.刚好昨天app审核通过了,所以就分享一篇如何实现ap ...