Prime k-tuple UVA - 1404
就是大区间求素数 参考
LightOJ - 1197
https://www.cnblogs.com/WTSRUVF/p/9190660.html
直接套那个代码就好了
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
using namespace std;
const int maxn = * 1e9;
int primes[];
bool vis[maxn+];
int ans = ;
vector<int> G; void init()
{
mem(vis,);
vis[] = ;
for(int i=; i<=; i++)
if(!vis[i])
{
primes[ans++] = i;
for(LL j=(LL)i*i; j<=; j+=i)
vis[j] = ;
}
} int main()
{
init();
int T;
int kase = ;
LL a, b, u, s;
cin>> T;
while(T--)
{
G.clear();
int res = ;
mem(vis,);
cin>> a >> b >> u >>s;
// if(a <= 2) a = 2;
int len = b - a;
for(int i=; i<ans && primes[i] * primes[i] <= b; i++)
{
int j = a/primes[i];
if(j*primes[i] < a) j++;
if(j <= ) j++;
while(j*primes[i] <= b)
{
vis[j*primes[i] - a] = ;
j++;
} }
if(a == ) vis[] = ;
for(int k=; k<=len; k++)
if(!vis[k] && a + k > )
G.push_back(a+k);
for(int i=; i + u - <G.size(); i++)
if(G[i+u-] - G[i] == s)
res++;
printf("%d\n",res); } return ;
}
我觉着人家写的代码比较好 参考一下
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm> using namespace std;
typedef long long ll;
const int sqrt_inf = ;
const int maxn = * 1e9; int np, pri[sqrt_inf];
bool vis[maxn+];
vector<int> vec; void prime_table (int n) {
np = ;
memset(vis, , sizeof(vis)); for (int i = ; i <= n; i++) {
if (vis[i])
continue; pri[np++] = i;
for (int j = i * i; j <= n; j += i)
vis[j] = ;
}
} int solve () {
int ret = ;
int a, b, s, k;
vec.clear();
memset(vis, , sizeof(vis)); scanf("%d%d%d%d", &a, &b, &k, &s); for (int i = ; i < np && pri[i] * pri[i] <= b; i++) {
int u = pri[i], d = (u - a % u) % u; if (u == a + d)
d += u; while (d <= b - a) {
vis[d] = ;
d += u;
}
} for (int i = ; i <= b-a; i++) {
if (vis[i] == && a + i > )
vec.push_back(a+i);
} for (int i = ; i + k - < vec.size(); i++) {
if (vec[i+k-] - vec[i] == s)
ret++;
} return ret;
} int main () {
prime_table(sqrt_inf); int cas;
scanf("%d", &cas); while (cas--) {
printf("%d\n", solve());
}
return ;
}
Prime k-tuple UVA - 1404的更多相关文章
- 暴力求解——素环数 Prime Ring Problem ,UVa 524
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...
- Again Prime? No Time. UVA - 10780(质因子分解)
m^k就是让m的每个质因子个数都增加了k倍 求m的质因子 在n!中增加了多少倍就好了,因为m^k 表示每一个质因子增加相同的倍数k 所以我们需要找到增加倍数最小的那个..短板效应 其它质因子多增加 ...
- Prime Ring Problem UVA - 524
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,...,n int ...
- uva 524(Prime Ring Problem UVA - 524 )
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...
- 3998 - Prime k-tuple
{p1,..., pk : p1 < p2 <...< pk} is called a prime k -tuple of distance s if p1, p2,..., pk ...
- .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 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
随机推荐
- golang--性能测试和分析
前言 测试分为:压力测试.负载测试.性能测试,功能测试等等,其中在开发过程中开发人员经常要写一些test case unit 自己的模块进行功能测试测和性能.在分析出模块的性能瓶颈后开发人员就需要针对 ...
- 机器学习基础 --- numpy的基本使用
一.numpy的简介 numpy是Python的一种开源的数值计算扩展库.这种工具可用来存储和处理大型矩阵,比Python自身的嵌套列表(nested list structure)结构要高效的多(该 ...
- Ubuntu16.04安装搜狗拼音输入法
为了让自己的电脑相对安全一些,我安装了ubuntu的物理机 因为要经常输入汉字,我就在unbuntu里面安装了搜狗输入法 1.在搜狗输入法官网下载Linux版本的安装包:https://pinyin. ...
- 多线程分段下载研究的python实现(一)
我一直对下载文件比较感兴趣.现在我下载文件大部分是用迅雷,但迅雷也有一些不如意的地方,内存占用大,一些不必要的功能太多,不可定制.尤其是最后一点.现在有些下载对useragent,cookie,aut ...
- lambda----jdk8重头戏
简介(译者注:虽然看着很先进,其实Lambda表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为常规的代码,因此你可以使用更少的代码来实现同样的功能.本人建议不要乱用,因 ...
- T&F 圆桌:儿童智能玩具离我们还有多远?
“女人和孩子的钱是最好挣的”,这句经典的名句被很多商家奉为信条,这在现实生活中也得到了很好的印证. 各种步行街.商业街.Mall 干的做多的事情就是:围绕着女人和孩子打造创造消费点.步行街.商业街上各 ...
- vmware_vcenter_api
VMware Vcenter_API 介绍 本文主要通过调用Vcenter_API,获取其中的数据中心,集群,主机,网络,存储,虚拟机信息. 开发语言 python 使用官方sdk pyvmomi 文 ...
- iOS 判断当前app版本,升级
要获取当前app store上的最新的版本,有两种方法, 一.在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询. 二.从app store上查询,可以获取到app的作者 ...
- WinForm中DataGridView的全选与取消全选
/// <summary> /// 全选 /// </summary> private void SelectAll() { //结束列表的编辑状态,否则可能无法改变Check ...
- React鼠标事件
说明:假设有一个用户名片,当鼠标滑到上面,显示用户详细信息,且用户详情卡片位置随鼠标位置改变而改变. UI框架:Material-ui 实现思路: 1.一个用户简介组件A(用于展示用户列表): 2.一 ...