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 题目大意:给出一个整数,如果有一段连续的素数之和等于该数,即满足要求,求出这种连续的素数的个数 水题:艾氏筛法打表+尺 ...
随机推荐
- Discuz x3.2利用阿里云cdn处理https访问亲测教程
第一步配置cdn和https 1.首先去阿里云.腾讯云.七牛云等申请免费https证书 2.虚拟主机是不能直接支持https的,需要cdn处理后才可以,并且端口是80 3.开启cdn加速处理,(买一个 ...
- 阿里路由框架ARouter的使用步骤
ARouter的使用步骤(以宿主APP modulebase和moduleuser 三大模块组成的工程为例) 第一步 因为路由跳转是子模块都需要用到的,所以我们在module_base模块中引入 co ...
- oracle存储过程 关于update的动态SQL-工作心得
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 12 月 20 日 原地址:https://niaobulashi.com/archives/oracle-procedure ...
- oracle的多表合并查询-工作心得
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 发布时间: 2018 年 11 月 29 日 原地址:https://niaobulashi.com/archives/oracle-select-al ...
- elementUI实现前端分页
按照他的文档来写分页,最主要的是el-table里面展示的数据怎么处理 <el-table :data="AllCommodityList.slice((currentPage-1)* ...
- 腾讯hr面
腾讯hr面面经 20181018 寒暄几句 hr自我介绍 hr介绍面试和最后出结果的时间周期 进入主题 自我介绍 考研成绩专业第一 聊考研(考研的经过.考研和保研) 本科成绩 考研成绩超长发挥还是正常 ...
- Redis源码阅读(五)集群-故障迁移(上)
Redis源码阅读(五)集群-故障迁移(上) 故障迁移是集群非常重要的功能:直白的说就是在集群中部分节点失效时,能将失效节点负责的键值对迁移到其他节点上,从而保证整个集群系统在部分节点失效后没有丢失数 ...
- 配置Ubuntu16.04虚拟机 (用途:CTF_pwn)
因为学习需要16.xx的虚拟机,所以把之前18.04的Ubuntu卸掉重装了一遍Ubuntu16.04, 考虑到我有备份和重装系统的爱好,故记之,以备后用. 目录: //最后更新时间:190122·1 ...
- Linux 学习记录 20170218
一.Linux 硬件查看命令 ----/proc 文件系统是一种内核和内核模块用来向进程(process) 发送信息的机制.我们可以从这个文件里获取到系统的相关信息. 1.显卡信息dmesg ...
- Python学习二
list [ ] Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素 classmates = ['Michael', 'Bob', 'Tracy ...