当前位置:网站首页>P1891 crazy LCM (Euler function)
P1891 crazy LCM (Euler function)
2022-07-03 19:14:00 【It's Alpaca duck】
Topic link : Click here
The main idea of the topic :
seek ∑ i = 1 n l c m ( i , n ) \sum_{i=1}^nlcm(i,n) ∑i=1nlcm(i,n)
Topic analysis :
The same way of thinking Yueyue gives Hua Hua a question
We know l c m ( i , n ) = i ⋅ n gcd ( i , n ) lcm(i,n)=\frac{i·n}{\gcd(i,n)} lcm(i,n)=gcd(i,n)i⋅n
So the question is transformed into ∑ i = 1 n i ⋅ n gcd ( i , n ) = n ⋅ ∑ i = 1 n i gcd ( i , n ) \sum_{i=1}^n\frac{i·n}{\gcd(i,n)}=n·\sum_{i=1}^n\frac{i}{\gcd(i,n)} ∑i=1ngcd(i,n)i⋅n=n⋅∑i=1ngcd(i,n)i
and
∑ i = 1 n i gcd ( i , n ) \sum_{i=1}^n\frac{i}{\gcd(i,n)} i=1∑ngcd(i,n)i
= ∑ d ∣ n 1 d ∑ i = 1 n i [ gcd ( i , n ) = d ] =\sum_{d|n}\frac{1}{d}\sum_{i=1}^n i[\gcd(i,n)=d] =d∣n∑d1i=1∑ni[gcd(i,n)=d]
= ∑ d ∣ n 1 d ∑ i = 1 ⌊ n d ⌋ i d [ gcd ( i , n / d ) = 1 ] =\sum_{d|n}\frac{1}{d}\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} id[\gcd(i,n/d)=1] =d∣n∑d1i=1∑⌊dn⌋id[gcd(i,n/d)=1]
= ∑ d ∣ n n / d ⋅ φ ( n / d ) + [ n / d = 1 ] 2 =\sum_{d|n}\frac{n/d·\varphi(n/d)+[n/d=1]}{2} =d∣n∑2n/d⋅φ(n/d)+[n/d=1]
= ∑ d ∣ n d ⋅ φ ( d ) + [ d = 1 ] 2 =\sum_{d|n}\frac{d·\varphi(d)+[d=1]}{2} =d∣n∑2d⋅φ(d)+[d=1]
Sift the Euler function in advance , Then, according to the above formula, the time complexity of harmonic series can be used to enumerate the enumeration factors to calculate the contribution
Then finally, for each group of input, use n n n Multiply by the previously calculated value
The overall time complexity is O ( n log n + t ) O(n\log n+t) O(nlogn+t)
See code for details :
// Problem: P1891 insane LCM
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1891
// Memory Limit: 125 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
int n,cnt,pri[maxn],phi[maxn],ans[maxn];
bool vis[maxn];
void init(int n)
{
vis[1] = phi[1] = 1;
for(int i = 2;i <= n;i++)
{
if(!vis[i])
{
pri[++cnt] = i;
phi[i] = i-1;
}
for(int j = 1;j<=cnt && i*pri[j]<=n;j++)
{
vis[i*pri[j]] = true;
if(i%pri[j] == 0)
{
phi[i*pri[j]] = phi[i]*pri[j];
break;
}
phi[i*pri[j]] = phi[i]*(pri[j]-1);
}
}
}
signed main()
{
n = maxn-1;
init(n);
for(int i = 1;i <= n;i++)
for(int j = 1;j*i <= n;j++) ans[i*j] += phi[j]*j/2;
// for(int i = 1;i <= n;i++) cout<<ans[i]+1<<endl;
int t = read();
while(t--)
{
n = read();
printf("%lld\n",1LL*n*(ans[n]+1));
}
return 0;
}
边栏推荐
- Record: install MySQL on ubuntu18.04
- TFs and SVN [closed] - TFs vs SVN [closed]
- During MySQL installation, the download interface is empty, and the components to be downloaded are not displayed. MySQL installer 8.0.28.0 download interface is empty solution
- Floating source code comment (38) parallel job processor
- SSM integration - joint debugging of front and rear protocols (list function, add function, add function status processing, modify function, delete function)
- 235. The nearest common ancestor of the binary search tree [LCA template + same search path]
- Find the median of two positive arrays
- Thinking about festivals
- Record: writing MySQL commands
- What does a really excellent CTO look like in my eyes
猜你喜欢
DriveSeg:动态驾驶场景分割数据集
01. Preparation for automated office (free guidance, only three steps)
知其然,而知其所以然,JS 对象创建与继承【汇总梳理】
Record: pymysql is used in pycharm to connect to the database
A green plug-in that allows you to stay focused, live and work hard
FBI warning: some people use AI to disguise themselves as others for remote interview
Pan for in-depth understanding of the attention mechanism in CV
235. The nearest common ancestor of the binary search tree [LCA template + same search path]
我们做了一个智能零售结算平台
How to read the source code [debug and observe the source code]
随机推荐
php-fpm的max_chindren的一些误区
235. 二叉搜索樹的最近公共祖先【lca模板 + 找路徑相同】
Scrapy爬虫框架
2022.02.11
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
Add control at the top of compose lazycolumn
Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
Sustainable service business models
The necessity of lean production and management in sheet metal industry
The online customer service system developed by PHP is fully open source without encryption, and supports wechat customer service docking
01. Preparation for automated office (free guidance, only three steps)
The way to treat feelings
Web3 credential network project galaxy is better than nym?
“google is not defined” when using Google Maps V3 in Firefox remotely
硬盘监控和分析工具:Smartctl
leetcode:556. 下一个更大元素 III【模拟 + 尽可能少变更】
Foundation of ActiveMQ
Driveseg: dynamic driving scene segmentation data set
Day18 - basis of interface testing
Find the median of two positive arrays