bzoj 2226 LCMSum 欧拉函数
2226: [Spoj 5971] LCMSum
Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 1123 Solved: 492
[Submit][Status][Discuss]
Description
Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n,n), where LCM(i,n) denotes the Least Common Multiple of the integers i and n.
Input
The first line contains T the number of test cases. Each of the next T lines contain an integer n.
Output
Output T lines, one for each test case, containing the required sum.
Sample Input
3
1
2
5
1
2
5
Sample Output
1
4
55
4
55
HINT
Constraints
1 <= T <= 300000
1 <= n <= 1000000
思路:时间复杂度T*sqrt(n);
小于n并且与n互质的数为phi(k)*k/2;
1的情况是特例;全部long long会超时。。。卡死我了
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 #define esp 0.00000000001 const int MAXN=1000010; int prime[MAXN],cnt; bool vis[MAXN]; int phi[MAXN]; void Prime(int n) { phi[1]=1; for(int i=2;i<n;i++) { if(!vis[i]) { prime[cnt++]=i; phi[i]=i-1; } for(int j=0;j<cnt&&i*prime[j]<n;j++) { int k=i*prime[j]; vis[k]=1; if(i%prime[j]==0) { phi[k]=phi[i]*prime[j]; break; } else phi[k]=phi[i]*(prime[j]-1); } } } ll phii(ll n) { return (ll)phi[n]*n/2; } int main() { int x,y,z,i,t; Prime(1000001); scanf("%d",&t); while(t--) { scanf("%d",&x); ll ans=0; ll gg=sqrt(x); for(i=1;i<=gg;i++) { if(x%i==0) ans+=phii(x/i),ans+=phii(i); } if(gg*gg==x) ans-=phii(gg); printf("%lld\n",(ans+1)*x); } return 0; }
分类:
数论 欧拉函数
, online judge bzoj
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
· 刚刚!百度搜索“换脑”引爆AI圈,正式接入DeepSeek R1满血版