P2158 [SDOI2008] 仪仗队 - Description
给定正整数 n,求
i=1∑n−1j=1∑n−1[gcd(i,j)=1]
1≤n≤5×104。
P2158 [SDOI2008] 仪仗队 - Solution
i=1∑n−1j=1∑n−1[gcd(i,j)=1]=2×i=1∑n−1j=1∑i[gcd(i,j)=1]−1=2×i=1∑n−1φ(i)−1
最后答案要加上第一行和第一列的 2,记得特判 n=1。
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
long long n,ans;
long long tot,Prime[100005],vis[100005],phi[100005];
inline void init(){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){
Prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot&&i*Prime[j]<=n;j++){
vis[i*Prime[j]]=1;
if(i%Prime[j]==0){
phi[i*Prime[j]]=phi[i]*Prime[j];
break;
}
else{
phi[i*Prime[j]]=phi[i]*phi[Prime[j]];
}
}
}
return;
}
signed main(){
cin>>n;
if(n==1){
cout<<0<<endl;
return 0;
}
init();
for(int i=1;i<n;i++){
ans+=phi[i];
}
cout<<ans*2+1<<endl;
return 0;
}
P2568 GCD - Description
给定正整数 n,求
i=1∑nj=1∑n[gcd(i,j)∈Prime]
1≤n≤107。
P2568 GCD - Solution
i=1∑nj=1∑n[gcd(i,j)∈Prime]=p∈P∑i=1∑nj=1∑n[gcd(i,j)=p]=p∈P∑i=1∑⌊pn⌋j=1∑⌊pn⌋[gcd(i,j)=1]=p∈P∑i=1∑⌊pn⌋[2×(j=1∑i[gcd(i,j)=1])−1]=p∈P∑i=1∑⌊pn⌋(2×φ(i)−1)
此处应有前缀和/整除分块,但是我懒,不写了,反正能过
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
long long n,Prime[10000005],tot,vis[10000005],phi[10000005],ans;
inline void init(){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){
Prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot&&i*Prime[j]<=n;j++){
vis[i*Prime[j]]=1;
if(i%Prime[j]==0){
phi[i*Prime[j]]=phi[i]*Prime[j];
break;
}
else{
phi[i*Prime[j]]=phi[i]*phi[Prime[j]];
}
}
}
return;
}
signed main(){
cin>>n;
init();
// for(int i=1;i<=n;i++){
// cout<<phi[i]<<" ";
// }
// cout<<endl;
for(int i=1;Prime[i]<=n&&i<=tot;i++){
int cnt=0;
for(int j=1;j<=n/Prime[i];j++){
cnt+=2*phi[j];
}
ans+=(cnt-1);
}
cout<<ans<<endl;
return 0;
}
P2398 GCD SUM - Description
给定正整数 n,求
i=1∑nj=1∑ngcd(i,j)
1≤n≤105。
P2398 GCD SUM - Solution
i=1∑nj=1∑ngcd(i,j)=d=1∑n(d×i=1∑nj=1∑n[gcd(i,j)=d])=d=1∑nd×i=1∑⌊dn⌋j=1∑⌊dn⌋[gcd(i,j)=1]=d=1∑nd×i=1∑⌊dn⌋2(j=1∑i[gcd(i,j)=1])−1=d=1∑nd×i=1∑⌊dn⌋2×φ(i)−1
C++#include<bits/stdc++.h>
#define int long long
using namespace std;
long long n,ans;
long long tot,Prime[100005],vis[100005],phi[100005];
inline void init(){
phi[1]=1;
for(int i=2;i<=n;i++){
if(!vis[i]){
Prime[++tot]=i;
phi[i]=i-1;
}
for(int j=1;j<=tot&&i*Prime[j]<=n;j++){
vis[i*Prime[j]]=1;
if(i%Prime[j]==0){
phi[i*Prime[j]]=phi[i]*Prime[j];
break;
}
else{
phi[i*Prime[j]]=phi[i]*phi[Prime[j]];
}
}
}
return;
}
signed main(){
cin>>n;
init();
for(int i=1;i<=n;i++){
// cout<<phi[i]<<" ";
int tmp=0;
for(int j=1;j<=n/i;j++){
tmp+=2*phi[j];
}
tmp--;
ans+=tmp*i;
}
cout<<ans<<endl;
return 0;
}
HDU2588 GCD - Description
多测,每组数据给定两个正整数 n 和 m,求
i=1∑n[gcd(i,n)≥m]
1≤T≤100,2≤n≤109,1≤m≤n。
HDU2588 GCD - Solution
i=1∑n[gcd(n,i)≥m]=d=m∑ni=1∑n[gcd(n,i)=d]
当 d 给定时,满足条件的 i 可以写成 i=i′d,同理有 n=n′d,其中 gcd(i′,n′)=1。而所求即为 i′d 的个数,为 φ(n′)。
d=m∑ni=1∑n[gcd(n,i)=d]=d∣n∑φ(dn)
P2303 [SDOI2012] Longge 的问题 - Description
给定正整数 n,求
i=1∑ngcd(n,i)
1≤n≤232。
P2303 [SDOI2012] Longge 的问题 - Solution
i=1∑ngcd(n,i)=d=1∑n(d×i=1∑n[gcd(n,i)=d])=d∣n∑(d×φ(dn))
拿这题讲讲怎么 O(n) 在线处理 φ(x)。
Lemma1
若 p 是质数,则有 φ(pn)=pn−1(p−1)
Prove1
在小于等于 pn 的正整数中,只有 p,2p,3p⋯,pn−1p 不与 pn 互质,这样的数有 pn−1 个。
因此 φ(pn)=pn−pn−1=pn−1(p−1)
Lemma2
若 a,b 互质,则有 φ(a)×φ(b)=φ(a×b)。(证明 φ 是积性函数)
Prove2
我们考虑把 x 质因数分解,对于每一组 piki 都给答案乘上 piki−1(pi−1)。但因为里面包含了一个 x,所以还可以接着化简。
φ(x)=x×p1p2⋯pn(p1−1)(p2−2)⋯(pn−1)
单次复杂度是质因数分解的 O(n)。
ps:如果你 85pts 记得判是不是完全平方数啊啊啊调了 3h
C++#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
unsigned long long n,yinzi[100005],tot,ans;
inline int phi(int x){
int tmp=x;
for(int i=2;i<=sqrt(x);i++){
if(x%i==0){
tmp=tmp/i*(i-1);
}
while(x%i==0){
x/=i;
}
}
if(x>1){
tmp=tmp/x*(x-1);
}
return tmp;
}
signed main(){
cin>>n;
for(int i=1;i<=sqrt(n);i++){
if(n%i==0){
yinzi[++tot]=i;
if(i*i==n){
continue;
}
yinzi[++tot]=n/i;
}
}
for(int i=1;i<=tot;i++){
ans+=yinzi[i]*phi(n/yinzi[i]);
}
cout<<ans<<endl;
return 0;
}