HDU 2588 GCD && GCD問題總結,hdu2588gcd

來源:互聯網
上載者:User

HDU 2588 GCD && GCD問題總結,hdu2588gcd

                                                                                                             GCD(一)

題目:

 The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.

   求滿足題目要求的x個數。

演算法:

   直接篩選會逾時,根據題目給出的不等式特點GCD(x,N) >= M 可以知道滿足題目要求的一定是N的因子而且必須大於等於M(想想為什嗎?解體關鍵)。所以,只要枚舉N的大於等於M的因子就可以了。因為,在10^9內最多的因子數不超過30個。所以,總時間是O(30*loglogn)接近常數。

 

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;typedef __int64 LL;const int MOD = 1000000007;int euler_phi(int n){    int k = (int)sqrt(n + 0.5);    int ans = n;    for(int i = 2;i <= k;++i) if(0 == n % i){        ans = ans / i * (i - 1);        while(0 == n % i) n /= i;    }    if(n > 1) ans = ans / n * (n - 1);    return ans;}LL getFact(int n,int m){    LL res = 0;    int k = sqrt(n + 0.5);    int tmp;    for(int i = 1;i <= k;++i){        if(0 == n % i){            tmp = n / i;            if(i >= m) res += euler_phi(n / i);            if(tmp >= m && i != tmp) res += euler_phi(n / tmp);        }    }    return res;}int main(){    int T,n,m;    scanf("%d",&T);    while(T--){        scanf("%d%d",&n,&m);        if(n == 1 && m == 1){            puts("1");            continue;        }        printf("%I64d\n",getFact(n,m));    }    return 0;}


 

  

                                GCD(二)

題目:

   給你一個數N,使得在1~N之間能夠找到x使得x滿足gcd( x ,  N  ) >= M,求解gcd(x,N)的和。

演算法:

  由上題的知識可以知道,1...N的互質個數為歐拉函數值且其gcd只能是N的因子。所以,對於N = x * y。我們只要

求出x在y內的互質個數就好了,結果乘以x就是gcd = x的和了.

證明:

   SUM(gcd = x ) = 1*x + 2*x + 3*x ..... y*x

  所以,當gcd = x的時候只要求出y的歐拉函數值就好了。

 

而一個數的因子又可以在sqrt(N)內求出。

 

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;typedef long long LL;int euler_phi(int n){    int m = sqrt(n + 0.5);    int ans = n;    for(int i = 2;i <= m;++i) if(0 == n % i){        ans = ans / i * (i - 1);        while(0 == n % i) n /= i;    }    if(n > 1) ans = ans / n * (n - 1);    return ans;}LL solve(int n,int m){   LL res = 0;   int k = sqrt(n + 0.5);   for(int i = 1;i <= k;++i){       if(0 == n % i){           if(i >= m)              res += i * euler_phi(n / i);           if(i != n / i && n / i >= m)              res += n / i * euler_phi(i);       }   }   return res;}int main(){    int n,m;    while(~scanf("%d%d",&n,&m)){        printf("%lld\n",solve(n,m));    }    return 0;}


 

                                                                                                  GCD(三)

 

題目:

    The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6.
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem:
Given integers N and M,please answer sum of  X satisfies 1<=X<=N and (X,N)>=M.

 

演算法:

   跟GCD(一)不同的是這題求得是滿足gcd(x,n) >= m ,x的和。而由歐拉函數中的一個定理可以知道

 

所以,只要SUM(n = x * y) = y*α(y) / 2 * x 

因為要的是x的和,而我們是在把X先進行X / x處理的所以最後要在乘回上x得到原值。

 

 #include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;typedef long long LL;const int MOD = 1000000007;int euler_phi(int n){    int k = (int)sqrt(n + 0.5);    int ans = n;    for(int i = 2;i <= k;++i) if(0 == n % i){        ans = ans / i * (i - 1);        while(0 == n % i) n /= i;    }    if(n > 1) ans = ans / n * (n - 1);    return ans;}LL getFact(int n,int m){    LL res = 0;    int k = sqrt(n + 0.5);    LL tmp;    for(int i = 1;i <= k;++i){        if(0 == n % i){            tmp = n / i;            if(i >= m){                LL t1 = tmp * euler_phi(tmp) / 2 % MOD;                t1 = t1 ? t1 : 1;                res = (res + t1 * i) % MOD;            }            if(tmp >= m && i != tmp) {                LL t1 = i * euler_phi(i) / 2 % MOD;                t1 = t1 ? t1 : 1;                res = (res + t1 * tmp) % MOD;            }        }    }    return res >= MOD ? res%MOD : res;}int main(){    int T,n,m;    scanf("%d",&T);    while(T--){        scanf("%d%d",&n,&m);        printf("%lld\n",getFact(n,m));    }    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.