221. 龙哥的问题

题目链接

221. 龙哥的问题

龙哥现在有一道题,要考考大家。

给定一个整数 N,请你求出 1iNgcd(iN)的值。

输入格式

一个整数 N

输出格式

一个整数表示结果。

数据范围

1<N<231

输入样例:

6

输出样例:

15

解题思路

欧拉函数

gcd(i,n)=d,则有 gcd(i/d,n/d)=1,原问题即转化为有多少个这样的 i,其贡献都为 d,显然满足 gcd(i/d,n/d)iϕ(n/d) 个,答案即为 d|nd×ϕ(n/d)=d|nn/d×ϕ(d),这时直接暴力可过,但是还可以继续优化,$\sum_{d|n}n/d\times \phi(d)=n\times \sum_{d|n}(1-1/p_{1d})\times (1-1/p_{2d})\times \dots \times (1-p_{kd}) n=p_1^{\alpha_1}\times p_2^{\alpha_2}\times \dots \times p_t^{\alpha_t}$,继续对 d 进行分析:d(1+p1+p12++p1α1)×(1+p2+p22++p2α2)××(1+pt+pt2++ptαt) 展开后的任意一项,而任意一个 d 都对应一个 (11/pid),故 n×d|n(11/p1d)×(11/p2d)××(1pkd)=n×(1+(11/p1)+(11/p1)++(11/p1))×(1+(11/p2)+(11/p2)++(11/p2))××(1+(11/pt)+(11/pt)++(11/pt))=n×(1+α1×(11/p1))×(1+α2×(11/p2))××(1+αt×(11/pt))

  • 时间复杂度:O(n)

代码

// Problem: 龙哥的问题 // Contest: AcWing // URL: https://www.acwing.com/problem/content/223/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } int n; int main() { cin>>n; LL res=n; for(int i=2;i<=n/i;i++) if(n%i==0) { int s=0; while(n%i==0)s++,n/=i; res/=i,res*=(LL)i+s*i-s; } if(n>1)res/=n,res*=(LL)n+n-1; cout<<res<<'\n'; return 0; }
  • 暴力
// Problem: 龙哥的问题 // Contest: AcWing // URL: https://www.acwing.com/problem/content/223/ // Memory Limit: 64 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } int n; int phi(int x) { int res=x; for(int i=2;i<=x/i;i++) if(x%i==0) { res/=i; res*=(i-1); while(x%i==0)x/=i; } if(x>1)res/=x,res*=(x-1); return res; } int main() { cin>>n; LL res=0; for(int i=1;i<=n/i;i++) { if(n%i==0) { res+=i*phi(n/i); if(i!=n/i)res+=n/i*phi(i); } } cout<<res<<'\n'; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16782632.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(46)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示