E - GCD HDU - 2588

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.

InputThe first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.OutputFor each test case,output the answer on a single line.Sample Inputios

3
1 1
10 2
10000 72

Sample Outputide

1
6
260
題意:給你一個t表示測試組數,每一組數據給你兩個數n,m(範圍2~1e8),求在小於等於n的數中,有多少數知足gcd(i,n)>=m;
思路:很明顯是一個歐拉函數,可是它的gcd不是一是m,所以咱們要把它化爲m,gcd爲一,乘上m,gcd不就是m了嗎
所以,咱們要找n全部因子,
  1. 當因子p大於等於m的時, n = p *n/p,這個時候只要知道n/p的歐拉值就能夠了euler(n/p),表示的gcd爲一,*p,gcd就是p>=m;
  2. 當因子p,有n/p>=m時,而且保證 n/p !=p,這個時候只要知道p的歐拉值就能夠了euler(p),表示的gcd爲一,*n/p,gcd就是n/p>=m;
  3. 二者之間是沒有重複的,惟一存在可能重複的就是當n/p=p的時候,特判一下就能夠了euler(p),and euler(n/p) 會有重複的部分,但他們的乘項不一樣,乘出的結果也就不一樣
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <map>
#define Mod 1000000007
using namespace std;
typedef long long ll;
const  ll N = 3000000+10;
map<ll,ll> elh;
long long a,b;
ll sum [N];
ll Euler(ll n)
{
    if(n==1)return 1;
    ll res =n;
    for(ll i=2;i<=n/i;i++)
    {
        if(n%i==0)
        {
            res = res -res/i;
        }
        while(n%i==0)n/=i;
    }
    if(n>1)res -= res/n;
    return res;
}
int main()
{
    int t;
    cin >>t;
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        ll sum =0;
        for(int i=1;i<=a/i;i++)
        {
           if(a%i==0)
           {
               if(i>=b)
               {
                   sum +=Euler(a/i);
               }
               if(a/i>=b&&a/i!=i)
               {
                   sum+=Euler(i);
               }
           }
        }
            cout <<sum<<endl;
    }
    return 0;
}

無論前方的路有多麼曲折,我都會告訴本身,本身的選擇,沒有後悔,後退的可能,由於熱愛因此堅持,既然已經走上這條路,就走出一點風采!!!函數

相關文章
相關標籤/搜索