题目描述

Let's denote as L(x,p)L(x,p) an infinite sequence of integers yy such that gcd(p,y)=1gcd(p,y)=1 and y>xy>x (where gcdgcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L(x,p)L(x,p) are 11 -indexed; for example, 99 , 1313 and 1515 are the first, the second and the third elements of L(7,22)L(7,22) , respectively.

You have to process tt queries. Each query is denoted by three integers xx , pp and kk , and the answer to this query is kk-th element of L(x,p)L(x,p) .

输入输出格式

输入格式:

The first line contains one integer tt ( 1<=t<=300001<=t<=30000 ) — the number of queries to process.

Then tt lines follow. ii -th line contains three integers xx , pp and kk for ii -th query ( 1<=x,p,k<=10^{6}1<=x,p,k<=106 ).

输出格式:

Print tt integers, where ii -th integer is the answer to ii -th query.

输入输出样例

输入样例#1:

3
7 22 1
7 22 2
7 22 3
输出样例#1:

9
13
15
输入样例#2:

5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
输出样例#2:

187
87
139
128
141 二分+容斥就ojbk了,考虑到<=10^6的数最多有10种质因子,我们就把p质因子分解一下,然后二分第k大的满足条件的数y是多少。
需要写一个用来计算前i个数中有多少数与p互质的函数get,那么当get(y)-get(x)>=k时,更新答案并调整右边界;否则调整左边界。
之所以这么做的原因是,我们需要找到get(y)-get(x)==k的最小的y,这个y才是要求的第k大的与p互质的数。 还有不是很明白为什么时限是5s(虽然看洛谷上好多人都是卡着时限过的),反正我最慢的一个点也才跑了300+ms,如图
可能是因为我加了一些预处理来优化计算过程吧hhhh,请不要把我想成用循环展开什么缓存dark技巧来卡常的毒瘤人士(虽然迫不得已的时候会用一些dark卡常技巧)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
using namespace std;
int num,d[66],f[2066],jc[2066];
int T,n,a,p,k,ci[30];
int l,r,mid,ans,now;
const int inf=1000000000; inline void dvd(){
num=0,n=sqrt(p+0.5);
for(int i=2;i<=n;i++) if(!(p%i)){
d[++num]=i;
while(!(p%i)) p/=i;
if(p==1) break;
} if(p!=1) d[++num]=p; for(int S=0;S<ci[num];S++){
jc[S]=1;
for(int j=0;j<num;j++) if(ci[j]&S) jc[S]*=d[j+1];
}
} //<=x的数中有多少与p互质
inline int get(int x){
int an=0;
for(int s=0;s<ci[num];s++) an+=f[s]*(x/jc[s]);
return an;
} int main(){
ci[0]=1;
for(int i=1;i<=20;i++) ci[i]=ci[i-1]<<1;
f[0]=1;
for(int i=1;i<=2055;i++) f[i]=-f[i^(i&-i)]; scanf("%d",&T);
while(T--){
scanf("%d%d%d",&a,&p,&k);
dvd();
l=a+1,r=inf,now=get(a);
while(l<=r){
mid=l+r>>1;
if(get(mid)-now<k) l=mid+1;
else ans=mid,r=mid-1;
} printf("%d\n",ans);
} return 0;
}

  


Codeforces 920 G List Of Integers的更多相关文章

  1. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  2. [codeforces 549]G. Happy Line

    [codeforces 549]G. Happy Line 试题描述 Do you like summer? Residents of Berland do. They especially love ...

  3. CodeForces 794 G.Replace All

    CodeForces 794 G.Replace All 解题思路 首先如果字符串 \(A, B\) 没有匹配,那么二元组 \((S, T)\) 合法的一个必要条件是存在正整数对 \((x,y)\), ...

  4. codeforces 920 EFG 题解合集 ( Educational Codeforces Round 37 )

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  5. Codeforces 1207 G. Indie Album

    Codeforces 1207 G. Indie Album 解题思路 离线下来用SAM或者AC自动机就是一个单点加子树求和,套个树状数组就好了,因为这个题广义SAM不能存在 \(len[u] = l ...

  6. Educational Codeforces Round 37-G.List Of Integers题解

    一.题目 二.题目链接 http://codeforces.com/contest/920/problem/G 三.题意 给定一个$t$,表示有t次查询.每次查询给定一个$x$, $p$, $k$,需 ...

  7. codeforces 659 G. Fence Divercity 组合数学 dp

    http://codeforces.com/problemset/problem/659/G 思路: f(i,0/1,0/1) 表示到了第i个,要被切的块开始了没有,结束了没有的状态的方案数 递推看代 ...

  8. Codeforces 803 G. Periodic RMQ Problem

    题目链接:http://codeforces.com/problemset/problem/803/G 大致就是线段树动态开节点. 然后考虑到如果一个点还没有出现过,那么这个点显然未被修改,就将这个点 ...

  9. Codeforces 954 G. Castle Defense

    http://codeforces.com/problemset/problem/954/G 二分答案 检验的时候,从前往后枚举,如果发现某个位置的防御力<二分的值,那么新加的位置肯定是越靠后越 ...

随机推荐

  1. YUI Compressor是如何压缩JS代码的?

    YUI Compressor 压缩 JavaScript 的内容包括: 移除注释 移除额外的空格 细微优化 标识符替换(Identifier Replacement) YUI Compressor 包 ...

  2. MySQL使用笔记(一)安装配置

    By francis_hao    Nov 27,2016   一般软件的安装都是可以通过源码和安装包安装,源码安装可配置性好些,安装包安装比较省事,况且使用yum也可以解决依赖的问题,基本实现了一键 ...

  3. Spring学习--基于 XML 的配置声明切面

    正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...

  4. Python 入门学习笔记

    安装和运行 官网下载安装包https://www.python.org/downloads/mac-osx/下载完直接安装即可 运行打开 terminal,输入命令 python,进入 python ...

  5. webdriver函数

    import sys; print('%s %s' % (sys.executable or sys.platform, sys.version)) PyDev console: starting. ...

  6. 详解SHOW PROCESSLIST显示哪些线程正在运行列出的状态

    SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到所有线程.否则,您只能看到您自己的线程( ...

  7. guake 3.4发布,支持切分窗口

    guake是一款下拉式终端,美观实用. 近日发布了3.4版本,在3.4版本中支持了切分窗口功能.如图所示,还是比较实用的一个功能. 目前ubuntu仓库中还未更新,需要使用pip安装,或者自行从源码编 ...

  8. rhel5.5 linux系统下安装Oracle 11g

    一.配置环境变量1.我将环境变量配置写成了一个脚本,将这个脚本copy到一个新建的linux系统.(脚本是本人原创,前2篇文章里有,感兴趣的朋友可以去看看) 2.进入脚本所在的目录. 3.执行脚本,需 ...

  9. LAMP安装细则

    利用xshell从Windows向Linux传输文件[root@nanainux ~]#yum install lrzsz[root@nanalinux ~]#rz  MySq二进制包安装 mysql ...

  10. k8s job的使用

    1.运行一次性容器 容器按照持续运行的时间可分为两类: 服务类容器 服务类容器通常持续提供服务,需要一直运行,比如 http server,daemon 等. 工作类容器 工作类容器则是一次性任务,比 ...