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 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
随机推荐
- Windows7 jmeter3.1安装(咋个安装?)
这是一个比较详细的安装教程0.0,不懂可以私我,有错也可以私我 1.0 首先我们得有jdk,我选择的是1.8版本的jdk, QQ群:550654190,(进群答案:李熠)群文件里有. 进去后下载 ...
- 行为事件(ActionChains)源码详解
ActionChains简介 actionchains是selenium里面专门处理鼠标相关的操作如:鼠标移动,鼠标按钮操作,按键和上下文菜单(鼠标右键)交互. 这对于做更复杂的动作非常有用,比如悬停 ...
- Educational Codeforces Round 61 (Rated for Div. 2) E. Knapsack
非常经典的dp题,因为1至8的最大公约数是840,任何一个数的和中840的倍数都是可以放在一起算的, 所以我只需要统计840*8的值(每个数字(1-8)的sum%840的总和),剩下都是840的倍数 ...
- SVN For Mac: Cornerstone.app破解版免费下载
Cornerstone.app下载地址 链接:https://pan.baidu.com/s/1kwQ65SBgfWXQur8Zdzkyyw 密码:rqe7 Cornerstone303 MAS.a ...
- k8s学习-资源管理
在云计算领域,资源可被分为计算资源.网络资源.存储资源三大类,也可被分别称作为计算云.网络云.存储云.在以容器为核心的云平台上,应用容器镜像也是一种资源. 一.计算资源管理 计算资源在云平台上主要指应 ...
- Linux虚拟机安装教程
必备组件: vmware(程序主题) 链接:https://pan.baidu.com/s/14OplOGOQTVAnf0iDqgDhDQ 提取码:jape centos(Linux系统) 链接:ht ...
- PHP 伪协议
1.file:// file://用于访问本地文件系统,不受allow_url_fopen影响 <?php include($_GET['file']); ?> 2.http:// GET ...
- 记录一次redis故障
ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persi ...
- final文案+美工展示
作业要求:https://edu.cnblogs.com/campus/nenu/SWE2017FALL/homework/1438 团队介绍:thunder 组成员及各位博客地址: 1.王航:htt ...
- Daily Scrum 11.10
今日完成任务: 1.加入更改头像功能 2.解决不发送激活邮件和重置密码邮件的问题 3.在服务器上部署网站 4.加入匿名提问功能 明日任务: 黎柱金 修改数据库用户表,实现用户积分管理功能 晏旭瑞 解决 ...