G. List Of Integers
time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

You have to process t queries. Each query is denoted by three integers xp and k, and the answer to this query is k-th element of L(x, p).

Input

The first line contains one integer t (1 ≤ t ≤ 30000) — the number of queries to process.

Then t lines follow. i-th line contains three integers xp and k for i-th query (1 ≤ x, p, k ≤ 106).

Output

Print t integers, where i-th integer is the answer to i-th query.

Examples
input

Copy
3
7 22 1
7 22 2
7 22 3
output

Copy
9
13
15
input

Copy
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
output

Copy
187
87
139
128
141
题意:求出与p互质并且>x的第k小数
题解:首先求出p所有的质因子,状压枚举出质因子相乘的情况,比如000001表示只有一个最小的质因子,则在1-y中有y/prime[1]个,再通过容斥即可求出与p不互质的个数,此时二分答案y即可
 #include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
ll sol(ll x,vector<ll>&f){
ll res=;
int n=(int)f.size();
for(int i=;i<(<<n);i++)
{
ll now=,sgn=-;
for(int j=;j<n;j++)
if(i&(<<j))now*=f[j],sgn*=-;
res+=sgn*(x/now);
}
return x-res;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
ll x,p,k;
scanf("%lld%lld%lld",&x,&p,&k);
vector<ll>g;
for(ll i=;i*i<=p;i++){
if(p%i==){
g.push_back(i);
while(p%i==){
p/=i;
}
}
}
if(p>)g.push_back(p);
ll r=1000000000000LL;
ll l=;
k+=sol(x,g);
ll ans;
while(l<=r){
ll mid=(l+r)/;
if(sol(mid,g)>=k){
ans=mid;
r=mid-;
}
else{
l=mid+;
}
}
printf("%lld\n",ans);
}
return ;
}

[cf920G][容斥原理+二分]的更多相关文章

  1. Bzoj 2440: [中山市选2011]完全平方数(莫比乌斯函数+容斥原理+二分答案)

    2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MB Description 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平 ...

  2. Happy 2006 POJ - 2773 容斥原理+二分

    题意: 找到第k个与m互质的数 题解: 容斥原理求区间(1到r)里面跟n互质的个数时间复杂度O(sqrt(n))- 二分复杂度也是O(log(n)) 容斥原理+二分这个r 代码: 1 #include ...

  3. POJ 2773 Happy 2006#素数筛选+容斥原理+二分

    http://poj.org/problem?id=2773 说实话这道题..一点都不Happy好吗 似乎还可以用欧拉函数来解这道题,但正好刚学了容斥原理和二分,就用这个解法吧. 题解:要求输出[1, ...

  4. HDU 3388 Coprime(容斥原理+二分)

    Coprime Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. poj 2773 Happy 2006 容斥原理+二分

    题目链接 容斥原理求第k个与n互质的数. #include <iostream> #include <vector> #include <cstdio> #incl ...

  6. BZOJ 2986: Non-Squarefree Numbers [容斥原理 二分]

    题意:求第$n \le 10^{10}$个不是无平方因子数 二分答案, 容斥一下,0个质数的平方因子-1个..... 枚举$\sqrt$的平方因子乘上莫比乌斯函数,最后求出无平方因子数的个数取补集 # ...

  7. 【BZOJ】2440: [中山市选2011]完全平方数(莫比乌斯+容斥原理+二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2440 我觉得网上很多题解都没说清楚...(还是我太弱了? 首先我们可以将问题转换为判定性问题,即给出 ...

  8. POJ 2773 Happy 2006(容斥原理+二分)

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10827   Accepted: 3764 Descr ...

  9. POJ2773(容斥原理)

    Happy 2006 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11458   Accepted: 4001 Descr ...

随机推荐

  1. 022 Android .9图片的含义及制作教程

    1.图片(.9.png格式)的概念 (1)9patch图片是andriod app开发里一种特殊的图片形式,文件的扩展名为:.9.png (2)9patch图片的作用就是在图片拉伸的时候保证其不会失真 ...

  2. [转帖]六种Socket I/O模型幽默讲解

    六种Socket I/O模型幽默讲解 https://www.cnblogs.com/jikebiancheng/p/6225009.html 原贴已经找不到了.. 老陈有一个在外地工作的女儿,不能经 ...

  3. NGINX---一次阿里云宝塔开发flask经历

    1.放行端口问题 不但需要在阿里云官网服务器控制台放行端口,还需要在宝塔里面放行端口 2.nginx 宝塔默认的用户是www 句法: user user [group]; 默认: 用户无人; 语境: ...

  4. 2018ACM-ICPC亚洲区域赛南京站I题Magic Potion(网络流)

    http://codeforces.com/gym/101981/attachments 题意:有n个英雄,m个敌人,k瓶药剂,给出每个英雄可以消灭的敌人的编号.每个英雄只能消灭一个敌人,但每个英雄只 ...

  5. OpenCV学习笔记5

    OpenCV学习笔记5 图像变换 傅里叶变换 这里可以先学习一下卷积分,了解清除卷积的过程和实际意义,在看这一章节的内容. 原理: 傅里叶变换经常被用来分析不同滤波器的频率特性.我们可以使用 2D 离 ...

  6. ReflectionTest:由输入的类名得到类的信息

    package reflection; import java.lang.reflect.*; import java.util.*; public class ReflectionTest { pu ...

  7. 解决warning: Clock skew detected. Your build may be incomplete

    原因:机器系统时间与文件时间不一致 解决:更新所有文件的时间后重新编译 find . -type f | xargs -n 5 touch make clean make xargs  -n num ...

  8. maven配置阿里镜像

    在conf\settings.xml 在<mirrors>里面添加   <mirror>    <id>nexus-aliyun</id>    < ...

  9. 在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串)

    原文:在论坛中出现的比较难的sql问题:36(动态行转列 解析json格式字符串) 所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路.

  10. css 层叠 比较特殊性

    css 层叠: 多个相同的css声明(属性),应用到同一个元素上.当一个标签声明冲突时,浏览器会自动出发层叠机制 1:比较优先级 2:比较特殊性 3:比较源次序 依次经过上面的1,2,3的比较后,最终 ...