Hdu--4983(数论,欧拉函数)

本文解析了一道关于最大公约数的算法题目,通过分析得出当K大于2时无解,K等于2时仅有一组解,重点讨论了K等于1的情况,并提供了一种有效的求解方法。

2014-09-04 14:10:38

Goffi and GCD

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


Problem Description
Goffi is doing his math homework and he finds an equality on his text book: \(\gcd(n - a, n) \times \gcd(n - b, n) = n^k\).

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

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\) (\(1 \le n, k \le 10^9\)).
 
Output
For each test case, output a single integer indicating the number of (\(a, b\)) modulo \(10^9+7\).
 
Sample Input
2 1 3 2
 
Sample Output
2 1
Hint
For the first case, (2, 1) and (1, 2) satisfy the equality.
 
Source
 
思路:搬运一下hdu题解:
题意:
给你 N 和 K,问有多少个数对满足 gcd(N-A, N) * gcd(N - B, N) = N^K

分析:
由于 gcd(a, N) <= N,于是 K>2 都是无解,K=2 只有一个解 A=B=N,只要考虑 K = 1 的情况就好了
其实上式和这个是等价的 gcd(A, N) * gcd(B, N) = N^K,我们枚举 gcd(A, N) = g,那么gcd(B, N) = N / g。问题转化为统计满足 gcd(A, N) = g 的 A 的个数。这个答案就是 ɸ(N/g)
只要枚举 N 的 约数就可以了。答案是 Σɸ(N/g)*ɸ(g) g | N
计算 ɸ 可以递归,也可以直接暴力计算,两个都可以。

 

 1 /*************************************************************************
 2     > File Name: 1003.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Thu 04 Sep 2014 11:40:55 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 typedef long long ll;
16 const int mod = (1e9) + 7;
17 
18 ll n,k;
19 ll ans;
20 
21 ll Phi(ll v){
22     ll tmp = v;
23     ll m = sqrt(v + 0.5);
24     for(ll i = 2; i <= m; ++i) if(v % i == 0){
25         tmp = tmp / i * (i - 1);
26         while(v % i == 0) v /= i;
27         if(v == 1) break;
28     }
29     if(v != 1) tmp = tmp / v * (v - 1);
30     return tmp;
31 }
32 
33 int main(){
34     while(scanf("%I64d%I64d",&n,&k) != EOF){
35         if(n == 1) printf("1\n");
36         else if(k > 2) printf("0\n");
37         else if(k == 2) printf("1\n");
38         else{
39             ans = 0;
40             ll m = sqrt(n + 0.5);
41             for(ll i = 1; i <= m; ++i) if(n % i == 0){
42                 ll sum = Phi(i) * Phi(n / i) * 2;
43                 ans = (ans + sum) % mod;
44             }
45             if(m * m == n){
46                 ll sum = Phi(m);
47                 ans = (ans - sum * sum + mod) % mod;
48             }
49             printf("%I64d\n",ans);
50         }
51     }
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3955984.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值