hdu 4983 Goffi and GCD(欧拉函数)
Problem Description
Goffi is doing his math homework and he finds an equality on his text book: gcd(n−a,n)×gcd(n−b,n)=nk.
Goffi wants to know the number of (a,b) satisfy the equality, if n and k are given and 1≤a,b≤n.
Note: gcd(a,b) means greatest common divisor of a and b.
Goffi wants to know the number of (a,b) satisfy the equality, if n and k are given and 1≤a,b≤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≤n,k≤109).
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.
Source
发现自己欧拉函数都给忘记了,所有赶紧补题。。。
1、k!=1时情况很简单,记住将if(k==2 || n==1)这个特判放在if(k>2)的前面,因为这个WA了很久,各种原因自己思考。
2、下面讨论k=1时情况。x=gcd(n-a,n),则n/x=gcd(n-b,n),因为n-a可以取到0...n-1也就是1....n,所以完全可以去掉n-这个限制条件,即gcd(a,n)=x、gcd(b,n)=n/x时个数,因为a<=n,所以gcd(a,n)的个数=u[n/x],u是欧拉函数。所以原式等于sigma(u[n/x]*u[x])其中x是n的约数。

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #define MOD 1000000007 6 #define ll long long 7 ll eular(ll n) 8 { 9 ll res=1; 10 for(ll i=2;i*i<=n;i++) 11 { 12 if(n%i==0) 13 { 14 n/=i,res*=i-1; 15 while(n%i==0) 16 { 17 n/=i; 18 res*=i; 19 } 20 } 21 } 22 if(n>1) res*=n-1; 23 return res; 24 } 25 ll n,k; 26 int main() 27 { 28 while(scanf("%I64d%I64d",&n,&k)==2) 29 { 30 if(k==2 || n==1) 31 { 32 printf("1\n"); 33 continue; 34 } 35 if(k>2) 36 { 37 printf("0\n"); 38 continue; 39 } 40 41 ll ans=0; 42 for(ll i=1;i*i<=n;i++) 43 { 44 if(n%i==0) 45 { 46 if(i*i!=n) 47 ans=(ans+eular(n/i)*eular(i)*2)%MOD; 48 else 49 ans=(ans+eular(n/i)*eular(i))%MOD; 50 } 51 } 52 printf("%I64d\n",ans); 53 54 } 55 return 0; 56 }
分类:
ACM---数学、思维
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架