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. torch.Tensor和numpy.ndarray

    1. torch.Tensor和numpy.ndarray相互转换 import torch import numpy as np # <class 'numpy.ndarray'> np ...

  2. [转帖]五分钟彻底搞懂你一直没明白的Linux内存管理

    五分钟彻底搞懂你一直没明白的Linux内存管理 https://cloud.tencent.com/developer/article/1462476 现在的服务器大部分都是运行在Linux上面的,所 ...

  3. varnish缓存清理

    本篇日志应该较早该去写的,一直脱了好久,直到最近才写.在使用任务cache工具时,都会提到的一个问题.如何只清理想清理的那部分缓存,而其已缓存的部分不受影响 .这里就要用到varnishadm工具,先 ...

  4. 多线程(11) — NIO

    Java NIO是new IO的简称,是一种可以替代Java IO的一套新的IO机制.它提供了一套不同于Java标准IO的操作机制,严格来说,NIO与并发并无直接关系,但是使用NIO技术可以大大提高线 ...

  5. 剑指offer44:翻转单词顺序列

    1 题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“stude ...

  6. 二十、网卡框架分析、虚拟网卡驱动和DM9621驱动分析

    一.网络设备驱动的结构 网卡设备不同于字符设备和块设备, 网络设备并不对应于/dev目录下的文件,它存放在/sys/class/net目录下. Linux系统对网络设备驱动定义了四个层次: 1. 网络 ...

  7. 【并发】7、借助redis 实现多线程生产消费队列

    1.这是第一个简单的初始化版本,看起来比使用fqueue似乎更好用 package queue.redisQueue; import queue.fqueue.vo.TempVo; import re ...

  8. mysql优化limit

    limit 1.当只需要一条数据的时候,用limit1: 2.当需要提高分页效率的时候: 如果用上面的语句分页少量数据还是可以用的,但是随着数据量越来越大,直接用limit语句查询速度就会越来越慢,降 ...

  9. •C#进阶系列——WebApi接口测试工具:WebApiTestClient

    阅读目录 一.WebApiTestClient介绍 二.WebApiTestClient展示 三.WebApiTestClient使用 1.如何引入组件 2.如何使用组件 四.总结 正文 前言:这两天 ...

  10. 打印html页面

    // 打印类属性.方法定义 const Print = function (dom, options) { if (!(this instanceof Print)) return new Print ...