GCD NYOJ 1007

#include<stdio.h>//GCD(1007) 
#include<math.h>
#define mod 1000000007
typedef long long ll;

// 设n的质因数分别为p1,p2,.....,pn.

//求欧拉函数(即n以内所有与n互质的数的个数) 

//f(x)=n*(1-p1)*(1-p2)*...*(1-pn)

//求与n互质的数之和S(x)=f(x)/2*x 

ll euler(ll x)
{  
    ll res=x,i;
    for(i=2;i<=sqrt(x);i++){
        if(x%i==0){
            res=res/i*(i-1);
            while(x%i==0)x/=i;    
        }
    }
    if(x>1)res=res/x*(x-1);
    return res;
}
void f(ll n,ll m)
{
    ll res=0,i;
    for(i=1;i<=sqrt(n);i++){
        if(n%i==0){
            if(n/i!=i&&n/i>=m){
                if(i==1||i==2){
                    res=(res+n/i)%mod;
                }
                else{
                    res=(res+euler(i)/2*i*(n/i))%mod;
                }
            }
            if(i>=m){
                if(n/i==1||n/i==2){
                    res=(res+i)%mod;
                }
                else{
                    res=(res+euler(n/i)/2*(n/i)*i)%mod;
                }
            }
        }
    }
    printf("%lld\n",res);
}
int main()
{
    ll n,m;
    int x;
    scanf("%d",&x);
    while(x--){
        scanf("%lld%lld",&n,&m);
        f(n,m);
    }
    return 0;
}

 

posted @ 2015-05-28 18:27  minimalism_Geek  阅读(133)  评论(0)    收藏  举报