hdu 2588 gcd 欧拉函数

GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
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.
 
Input
The 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.
 
Output
For each test case,output the answer on a single line.
 
Sample Input
3 1 1 10 2 10000 72
 
Sample Output
1 6 260
 
Source
思路:跟poj 2480 差不多;
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
#define MAXN 100001
ll prime[MAXN];//保存素数
ll vis[MAXN],ji;//初始化
ll Prime(ll n)
{
    ll cnt=0;
    //memset(vis,0,sizeof(vis));
    for(ll i=2;i<=n;i++)
    {
        if(!vis[i])
        prime[cnt++]=i;
        for(ll j=0;j<cnt&&i*prime[j]<n;j++)
        {
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)//关键
            break;
        }
    }
    return cnt;
}
ll phi(ll n)
{
    ll i,rea=n;
    for(i=0;i<ji;i++)
    {
        if(prime[i]*prime[i]>n)break;
        if(n%prime[i]==0)
        {
            rea=rea-rea/prime[i];
            while(n%prime[i]==0)  n/=prime[i];
        }
    }
    if(n>1)
        rea=rea-rea/n;
    return rea;
}
int main()
{
    ll x,y,z,i,t;
    ji=Prime(52010);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%I64d%I64d",&x,&y);
        ll ans=0;
        for(i=1;i*i<=x;i++)
        {
            if(x%i==0)
            {
                ll gg=i;
                ll hh=x/i;
                if(gg>=y)
                ans+=phi(hh);
                if(gg!=hh&&hh>=y)
                ans+=phi(gg);
            }
        }
        printf("%I64d
",ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/jhz033/p/5469132.html