P2303 [SDOI2012] Longge 的问题 (欧拉函数 + 迪利克雷卷积)

  • 题意
    给定整数 n n n,求 ∑ i = 1 n gcd ⁡ ( i , n ) \sum_{i=1}^n \gcd(i,n) i=1ngcd(i,n)

  • 解题思路
    直接求解肯定是不现实的,这种通常是要对公式进行变形。我们可以枚举 n n n的约数,那么 ∑ i = 1 n gcd ⁡ ( i , n ) = ∑ d ∣ n d ∑ i = 1 n [ gcd ⁡ ( i , n ) = = d ] = ∑ d ∣ n d ∑ i = 1 n d [ gcd ⁡ ( i , n d ) = = 1 ] = ∑ d ∣ n d ψ ( n d ) \sum_{i=1}^n \gcd(i,n)=\sum_{d|n}d\sum_{i=1}^n[\gcd(i,n)==d]\\=\sum_{d|n}d\sum_{i=1}^{\frac{n}{d}}[\gcd(i,\frac{n}{d})==1]\\=\sum_{d|n}d\psi(\frac{n}{d}) i=1ngcd(i,n)=dndi=1n[gcd(i,n)==d]=dndi=1dn[gcd(i,dn)==1]=dndψ(dn)
    则此题得解。

  • AC代码

/**
  *@filename:P2303
  *@author: pursuit
  *@created: 2021-09-18 09:25
**/
#include <bits/stdc++.h>
#define debug(a) cout << "debug : " << (#a)<< " = " << a << endl

using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
const int N = 1e5 + 10;
const int P = 1e9 + 7;
const int INF = 0x3f3f3f3f;

ll n;
ll eular(ll n){
    ll res = n;
    for(ll i = 2; i * i <= n; ++ i){
        if(n % i == 0){
            res -= res / i;
            while(n % i == 0){
                n /= i;
            }
        }
    }
    if(n > 1){
        res -= res / n;
    }
    return res;
}
void solve(){
    ll x = sqrt(n);
    ll res = 0;
    for(ll i = 1; i <= x; ++ i){
        if(n % i == 0){
            res += i * eular(n / i);
            if(i * i != n)res += n / i * eular(i);
        }
    }
    cout << res << endl;
}
int main(){	
    cin >> n;
    solve();
    return 0;
}
posted @   unique_pursuit  阅读(25)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示