CodeForces_920G List Of Integers(二分+容斥)

List Of Integers

time limit per test:5 seconds
memory limit per test:256 megabytes
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 1t30000) — 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 1x,p,k106).

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=a1b1a2b2.....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=Si=1maiS+i=1mj=i+1maiajS.......

#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]);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值