HDU4983 Goffi and GCD

Goffi and GCD

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1259    Accepted Submission(s): 436


Problem Description
Goffi is doing his math homework and he finds an equality on his text book:  gcd(na,n)×gcd(nb,n)=nk .

Goffi wants to know the number of ( a,b ) satisfy the equality, if  n  and  k  are given and  1a,bn .

Note:  gcd(a,b)  means greatest common divisor of  a  and  b .
 

Input
Input contains multiple test cases (less than 100). For each test case, there's one line containing two integers  n  and  k  ( 1n,k109 ).
 

Output
For each test case, output a single integer indicating the number of ( a,b ) modulo  109+7 .
 

Sample Input
  
  
2 1 3 2
 

Sample Output
  
  
2 1
Hint
For the first case, (2, 1) and (1, 2) satisfy the equality.
题意:给定n和k,求满足gcd(a, n) * gcd(b, n) == n^k的(a, b)的对数。

利用的两个结论:

1、对于1<=x<=n,有gcd(n-x , n) = gcd(x , n) 。

证明:假设gcd(n-a , n) = x(1<=a<=n),则x是(n-a)和n的最大公约数,所以存在整数k1,k2(并且k1<k2,而且gcd(k1,k2)=1即k1和k2没有除1以外的公约数)使得n-a=k1*x,n=k2*x;那么a=n-k1*x=k2*x-k1*x=(k2-k1)*x,则

gcd(a,n)=gcd((k2-k1)*x,n)=gcd((k2-k1)*x,k2*x)。那么gcd((k2-k1)*x,k2*x)是否等于x呢???

因为k1与k2没有除1以外的公约数,所以对任意的i(i>1)都有k2!=k1*i,则(k2-k1)与k2没有除1以外的公约数。

所以gcd(a,n)=gcd((k2-k1)*x,k2*x)=x=gcd(n-a , n)

2、gcd(a, n) = x中n和x为已知,用欧拉函数求满足条件的a的个数:num(a) = euler(n/a)。

证明:设m = n / x,p为小于m且与m互质的数,则gcd(p*x, m*x) = x,所以求p的个数即a的个数,即p的个数 = euler(m);


解析:

1、当n = 1时,ans = 1;

2、当n > 1时,

(1)当k > 2时,由于n*n < n^k,所以ans = 0;

(2)当k = 2时,只有n*n = n^k,所以ans = 1;

(3)当k = 1时,即求gcd(a, n) * gcd(b, n) = n。如果gcd(a,n)=x,则gcd(b,n)=n/x,所以遍历x即可,x为n的因子。对于每一个x可能会有多个a(1<=a<=n)存在,使得gcd(a,n)=x,假设存在ma个;那么对于每一个n/x,同样会有多个b(1<=b<=n)存在,使得gcd(b,n)=n/x,假设存在mb个。ma和mb使用euler函数即可求得。那么对于一个x,如果x*x!=n,那么就存在2*ma*mb对结果,如果x*x==n,那么就存在ma*mb对结果。

代码:

#include <stdio.h>
#include <string.h>
#define MOD 1000000007
typedef long long ll;
ll euler(ll x){
	ll res = x;
	for(ll i = 2; i*i <= 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;
}
int main(){
	ll n, k;
	while(~scanf("%lld%lld", &n, &k)){
		if(n == 1 || k == 2) {
			puts("1");
			continue;
		}
		if(k > 2){
			puts("0");
			continue;
		}
		ll ans = 0;
		for(ll i = 1; i*i <= n; i++){
			if(n % i == 0){
				if(i * i == n)
					ans = (ans + euler(n/i) * euler(i) % MOD) % MOD;
				else
					ans = (ans + 2 * euler(n/i) * euler(i) % MOD) % MOD;
			}
		}
		printf("%lld\n", ans);
	}
}
使用欧拉函数求个数这一关键方法没想到,看了网上大神们的做法才明白。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值