Coprime (二分+容斥原理)


Problem Description

Please write a program to calculate the k-th positive integer that is coprime with m and n simultaneously. A is coprime with B when their greatest common divisor is 1.

Input

The first line contains one integer T representing the number of test cases.
For each case, there's one line containing three integers m, n and k (0 < m, n, k <= 10^9).

Output

For each test case, in one line print the case number and the k-th positive integer that is coprime with m and n.
Please follow the format of the sample output.

Sample Input

3
6 9 1
6 9 2
6 9 3

Sample Output

Case 1: 1
Case 2: 5
Case 3: 7
题意:给a,b两个数字,寻找第k个二者都互素的数字
思路:枚举的话肯定会超时,所以可以使用二分+容斥原理,由于所找的数与m,n互质,那么这个数不能含有m,n所包含的素因子。但是k很大,不可能一个一个生成。于是二分,找到最小
的x,使得小于或等于x的数中满足条件的数的个数大于或等于k(刚开始比较浮躁,理解不了这里,后来发现就是寻找到一个x,然后判断x中的),则这个最小值即为答案。在判断小于或等于x的数中满足条件的数的个数时,可用容斥原理找出这些数中是m,n所含质因子倍数的数的个数。x减去所得
个数即为小于或等于x的数中满足条件的数的个数。
 
    
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
ll cnt,ans;
ll m,n,k,p,q;
ll prime[205];
setpp;
void findprime(ll g)
{
    ll h=g;
    for(int i=2;i*i<=h;i++){
        if(h%i==0){
            pp.insert(i);
            while(h%i==0)
                h=h/i;
        }
    }
    if(h>1) pp.insert(h);
}
ll dfs(ll mid,ll t)
{
    ll sum=0;
    for(ll i=t;i=k) high=mid-1;
            else low=mid+1;
        }
        printf("Case %d: %lld\n",++icase,low);
    }
    return 0;
}


你可能感兴趣的:(ACM_二分法,ACM_数字处理与数论)