List Of Integers
Problem Description
Let’s denote as L ( x , p ) L(x, p) L(x, p) an infinite sequence of integers y y y such that g c d ( p , y ) = 1 gcd(p, y) = 1 gcd(p, y) = 1 and y > x y > x y > x (where gcd is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of L ( x , p ) L(x, p) 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 ) L(7, 22) L(7, 22), respectively.
You have to process t t t queries. Each query is denoted by three integers x x x, p p p and k k k, and the answer to this query is k k k-th element of L ( x , p ) L(x, p) L(x, p).
Input
The first line contains one integer t t t ( 1 ≤ t ≤ 30000 1 ≤ t ≤ 30000 1 ≤ t ≤ 30000) — the number of queries to process.
Then t lines follow. i-th line contains three integers x , p x, p x,p and k k k for i-th query ( 1 ≤ x , p , k ≤ 1 0 6 1 ≤ x, p, k ≤ 10^6 1 ≤ x, p, k ≤ 106).
Output
Print t integers, where i-th integer is the answer to i-th query.
Sample Input
5
42 42 42
43 43 43
44 44 44
45 45 45
46 46 46
Sample Output
187
87
139
128
141
题意
设序列 L ( x , p ) L(x, p) L(x,p)为所有满足 g c d ( y , p ) = = 1 , y > x gcd(y,p)==1, y>x gcd(y,p)==1,y>x的y组成的有序序列。给出询问, x , p , k x, p, k x,p,k,求序列 L ( x , p ) L(x,p) L(x,p)的第k个数。
题解
不考虑x,求出所有与p互质的数,组成序列,设序列中小于等于x的数有num个,则实际上问题转化为求与p互质的第k+num个数。
二分,检查小于等于mid的与p互质的数的数量,若
>
=
k
+
n
u
m
>=k+num
>=k+num,则
r
=
m
i
d
r=mid
r=mid;否则
l
=
m
i
d
+
1
l = mid+1
l=mid+1(注意等于时应变更右区间)。
求小于等于S的数中与p互质的数的数量:容斥定理。将p分解质因数,
p
=
a
1
b
1
∗
a
2
b
2
.
.
.
.
.
a
m
b
m
p=a_1^{b_1}*a_2^{b_2}.....a_m^{b_m}
p=a1b1∗a2b2.....ambm。则
a
n
s
=
S
−
∑
i
=
1
m
S
a
i
+
∑
i
=
1
m
∑
j
=
i
+
1
m
S
a
i
∗
a
j
−
.
.
.
.
.
.
.
ans = S-\sum_{i=1}^{m}\frac{S}{a_i}+\sum_{i=1}^{m}\sum_{j=i+1}^{m}\frac{S}{a_i*a_j} -.......
ans=S−∑i=1maiS+∑i=1m∑j=i+1mai∗ajS−.......
#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<map>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define eps 1e-6
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 100100;
const int mod = 1000000007;
int ans, top, a[20];
int getnum(int x);
void dfs(int pos, int sig, int x, int y);
int main()
{
int t, n, m, i, j, k, x, p, s;
scanf("%d", &t);
while(t--)
{
top = 0;
scanf("%d %d %d", &x, &p, &k);
for(i=2;i*i<=p;i++)
if(p%i == 0){
a[top++] = i;
while(p%i == 0)p/=i;
}
if(p!=1)a[top++] = p;
k += getnum(x);
int l = x, r = 10000000;
while(l<r){
int mid = (l+r)/2;
if(getnum(mid) >= k)r = mid;
else l = mid+1;
}
printf("%d\n", l);
}
return 0;
}
int getnum(int x)
{
ans = 0;
dfs(0, 1, x, 1);
return ans;
}
void dfs(int pos, int sig, int x, int y)
{
if(pos == top){
ans += sig*x/y;
return ;
}
dfs(pos+1, sig, x, y);
dfs(pos+1, sig*-1, x, y*a[pos]);
}