GCD
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
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.-
输入
- 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 (1<=N<=10^9, 1<=M<=10^9), representing a test case. 输出
- Output the answer mod 1000000007 样例输入
-
3 1 1 10 2 10000 72
样例输出
-
1 35 1305000
上传者
- ACM_张书军
- 题意:给你两个数 N M 求1~N 之间所有x中gcd(N,x)>=M的x的和
- 想法:
-
介绍欧拉函数:,它又称为Euler's totient function、φ函数、欧拉商数等,对正整数n,欧拉函数是小于n且和n互质的正整数(包括1)的个数。例如Euler(8)=4,因为1,3,5,7均和8互质,下面用E(n)表示欧拉函数的值。
Euler函数表达通式:E(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…(1-1/pn),其中p1,p2……pn为x的所有素因数,x是不为0的整数。E(1)=1(唯一和1互质的数就是1本身)。
欧拉公式的延伸:一个数的所有质因子之和是 E(n)*n/2。
欧拉定理:对于互质的正整数a和n,有a^E(n) ≡ 1 mod n。
欧拉函数是积性函数——若m,n互质,E(m*n)=E(m)*E(n)。
若n是质数p的k次幂,E(n)=p^k-p^(k-1)=(p-1)*p^(k-1),因为除了p的倍数外,其他数都跟n互质。
特殊性质:当n为奇数时,E(2n)=E(n) - 步骤见代码注释
- 代码:
-
#include<stdio.h>
#include<string.h>
const int mod=1e9+7;
long long Euler(long long n)//欧拉函数
{
long long c=n,i;
for(i=2; i*i<=n; i++)
{
if(n%i==0)
{
while(n%i==0)
n/=i;
c=c/i*(i-1);//φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn);
}
}
if(n!=1)
c=c/n*(n-1);
return c;
}
//求 x和---一个数的所有质因子之和是euler(n)*n/2
long long Euler_sum(long long n)
{
if(n==1)
return 1;
else
return n*Euler(n)/2;
}
int main()
{
long long a,b;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&a,&b);
int cnt;
long long i,c=0;
for(i=1;i*i<=a;i++)
{
if(a%i==0)
{
if(i>=b)//枚举sqrt(n)到n的因子
{
cnt=i;
c=(c+cnt*Euler_sum(a/cnt))%mod;//求得与a最大公约数为cnt(i)的数的和
}
if(i*i!=a&&a/i>=b)//枚举1到sqrt(n)的因子。
{
cnt=a/i;
c=(c+cnt*Euler_sum(a/cnt))%mod;//求得与a最大公约数为cnt(a/i)的数的和
}
}
}
printf("%lld\n",c);
}
return 0;
}
-