hdu3388 Coprime【容斥原理】,hdu3388coprime

來源:互聯網
上載者:User

hdu3388 Coprime【容斥原理】,hdu3388coprime
Problem DescriptionPlease 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. 
InputThe 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). 
OutputFor 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

36 9 16 9 26 9 3
 題目翻譯:求同時與m,n互質的第k個數是多少!解題思路:直接求m,n的最小公倍,然後通過二分尋找找到第k個數!不得不說題目資料的嚴密性,卡逾時比較嚴,直接求m,n的最小公倍其實是花費不了很多時間的,gcd你懂得但是求m*n的素因子時就不一樣了,由於m,n較大,直接遍曆m*n勢必會花費很多時間,自然沒有分開求m的素因子和n的素因子省時間,由於m,n可能存在相同的素因子,那麼需要我們處理一下,我採取的是裡喲辦法set容器,排序去重,由於在set裡面對資料的操作不方便,於是又把裡面的元素取出放到數組裡,接下來就是容斥原理+二分了!
#include <cstdio>#include <set>using namespace std;#define LL long longLL p[200],top;set<LL>pp;void getp(LL m){    LL i;    for(i=2,top=0;i*i<=m;i++)        if(m%i==0){            pp.insert(i);            while(m%i==0) m/=i;        }    if(m>1) pp.insert(m);}void GETP(){    top = 0;    while(!pp.empty()){        p[top++] = *pp.begin();        pp.erase(pp.begin());    }}LL nop(LL mid,LL t){    LL i,sum=0;    for(i=t;i<top;i++)        sum+=mid/p[i]-nop(mid/p[i],i+1);    return sum;}int main(){    LL k,m,n;    int T,q=0;    scanf("%d",&T);    while(T--){        scanf("%lld%lld%lld",&m,&n,&k);        getp(m);        getp(n);        GETP();        LL mid,l=0,r=0x3f3f3f3f3f3f3f3f,t;        while(l<=r){            mid=(l+r)>>1;            t=mid-nop(mid,0);            if(t>=k) r=mid-1;            else l=mid+1;        }        printf("Case %d: %lld\n",++q,l);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.