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 x, p 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 x, p 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
37 22 17 22 27 22 3
output
Copy
91315
input
Copy
542 42 4243 43 4344 44 4445 45 4546 46 46
output
Copy
18787139128141
题目链接:
https://codeforces.com/contest/920/problem/G
题意:
有t组询问,对于每一组询问,
给你三个整数x,p,k
问有在大于x的整数中,与p互质的第k小的数y是哪个?
思路:
对于每一组询问我们在区间\([x+1,1e9]\) 这个区间内,二分答案y
同时容斥定律可以求得区间\([l,r]\) 中与一个数num互质的数个数。——知识点[1]
那么我们可以求区间\([x+1,y]\)中与p互质的数个数与k比较,然后进行转移区间即可。
先筛出\(1e6\) 内的所有质数,然后\(log(p)\) 的时间复杂度去唯一分解询问中的p,然后二进制枚举+容斥定律辅助二分即可。
不会的话,建议先学一下知识点1,再来解决本题。
code:
#include
#include
#include
#include
#include
#include
#include
#include