zoukankan      html  css  js  c++  java
  • 【POJ】2480 Longge's problem(欧拉函数)

    题目

    传送门:QWQ

    分析

    题意就是求∑gcd(i, N) 1<=i <=N.。

    显然$ gcd(i,n) = x $时,必然$x|n$。

    所以我们枚举一下n的约数,对于每个约数x,显然$ gcd(i/x,n/x)=1$

    所以我们计算一下n/x的欧拉函数就ok了。

    联赛前刷水题qwq

    代码

    // #include <bits/stdc++.h>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    const int maxn = 7000;
    ll a[maxn], top=0, p[maxn];
    int fac(ll x) {
        int n=x, flag=0;
        ll end = sqrt(x);
        for(ll i=2;i<=end;i++) {
            if(x%i==0) {
                flag=1;
                a[++top] = i;
                if(i*i!=x) a[++top] = x/i;
                while(n%i==0) n/=i;
            }
        }
        if(x==2) flag=0;
        return flag;
    }
    ll phi(ll x) {
        ll ans = x, end = sqrt(x)+1;
        for(ll i=2;i<=end;i++) {
            if(x%i==0) {
                ans = ans / i * (i-1);
                while(x%i==0) x/=i;
            }
        }
        if(x>1) ans = ans / x * (x-1);
        return ans;
    }
    void Ph() {
        for(int i=2;i<maxn-10;i++) p[i]=i;
        for(ll i=2;i<maxn-10;i++) {
            if(p[i]==i) {
                for(ll j=i;j<maxn-10;j+=i) {
                    p[j] = p[j] / i * (i-1);
                }
            }
        }
    }
    int main() {
        int n; Ph();
    
        while(scanf("%lld",&n)==1) {
            if(n==1){
                printf("1
    "); continue;
            }
            top=0; int q=fac(n);
            if(q == 0) {
                printf("%lld
    ",phi(n)+n); continue;
            }
            sort(a+1,a+1+top);
            int pp = top;
            ll ans=phi(n)+n;
            for(int i=1;i<=pp;i++) {
                ll qwq;
                if(n/a[i] < maxn-10) qwq = p[n/a[i]];
                else qwq = phi(n/a[i]);
                ans += a[i] * qwq;
            }
            printf("%lld
    ",ans);
        }
    }
  • 相关阅读:
    【springcloud alibaba】配置中心之nacos
    【springcloud alibaba】注册中心之nacos
    LeetCode计数质数Swift
    LeetCode移除链表元素Swift
    LeetCode删除重复的电子邮箱SQL
    LeetCode汉明距离Swift
    LeetCode两整数之和Swift
    LeetCode从不订购的客户SQL
    LeetCode超过经理收入的员工SQL
    LeetCode组合两个表SQL
  • 原文地址:https://www.cnblogs.com/noblex/p/9741217.html
Copyright © 2011-2022 走看看