当前位置:网站首页>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;
}
边栏推荐
- Web Security (VII) specific process of authentication with session cookie scheme
- 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
- Integrated easy to pay secondary domain name distribution system
- [optics] vortex generation based on MATLAB [including Matlab source code 1927]
- Scrape crawler framework
- Driveseg: dynamic driving scene segmentation data set
- Summary of composition materials for 2020 high-frequency examination center of educational resources
- High concurrency architecture cache
- [academic related] how to find the innovation of top papers? Chinese universities won the CVPR Best Student Thesis Award for the first time
- Simple solution of physical backup and restore of Damon database
猜你喜欢
![Free hand account sharing in September - [cream Nebula]](/img/4f/fec31778a56886585e35be87885452.jpg)
Free hand account sharing in September - [cream Nebula]

FBI警告:有人利用AI换脸冒充他人身份进行远程面试

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
![[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD](/img/51/209e35e0b94a51b3b406a184459475.png)
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD

【光学】基于matlab介电常数计算【含Matlab源码 1926期】
![[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]](/img/aa/9980acc9839f067202d46faabbf029.png)
[water quality prediction] water quality prediction based on MATLAB Fuzzy Neural Network [including Matlab source code 1923]

【光学】基于matlab涡旋光产生【含Matlab源码 1927期】

【数学建模】基于matlab船舶三自由度MMG模型【含Matlab源码 1925期】

PyTorch中在反向传播前为什么要手动将梯度清零?
![leetcode:556. Next larger element III [simulation + change as little as possible]](/img/a0/12e5ee5d01d666acb4b75ada2e6fec.png)
leetcode:556. Next larger element III [simulation + change as little as possible]
随机推荐
SQL injection for Web Security (1)
Record: writing MySQL commands
Add control at the top of compose lazycolumn
Valentine's Day - make an exclusive digital collection for your lover
[free sharing] kotalog diary2022 plan electronic manual ledger
Webrtc[41] - Analysis of the establishment process of webrtc transmission channel
【光学】基于matlab介电常数计算【含Matlab源码 1926期】
[proteus simulation] a simple encrypted electronic password lock designed with 24C04 and 1602LCD
[optics] dielectric constant calculation based on MATLAB [including Matlab source code 1926]
In addition to the prickles that pierce your skin, there are poems and distant places that originally haunt you in plain life
Bad mentality leads to different results
Dynamic planning -- expansion topics
FBI warning: some people use AI to disguise themselves as others for remote interview
Compose LazyColumn 顶部添加控件
Pytorch introduction to deep learning practice notes 13- advanced chapter of cyclic neural network - Classification
Think of new ways
Ctrip will implement a 3+2 work system in March, with 3 days on duty and 2 days at home every week
EGO Planner代码解析bspline_optimizer部分(3)
User identity used by startup script and login script in group policy
Recommend a GIF processing artifact less than 300K - gifsicle (free download)