[BZOJ2705][SDOI2012]Longge的问题(数论)

题目描述

传送门

题解

基础的数论题。
i=1n(i,n)
=i=1nd=1n[(i,n)=d]d
=d=1ni=1n[(id,nd)=1]d
令i=dj
=d=1nj=1nd[(id,nd)=1]d
=d=1nϕ(nd)
暴力枚举约数,根n求phi

代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long

LL n,ans;

inline LL phi(LL x){
    LL ans=x;
    for (LL i=2;i*i<=n;++i)
      if (x%i==0){
        ans=ans*(i-1)/i;
        while (x%i==0) x/=i;
      }
    if (x>1) ans=ans*(x-1)/x;
    return ans;
}

int main(){
    scanf("%I64d",&n);
    for (LL i=1;i*i<=n;++i)
      if (n%i==0){
        ans+=phi(n/i)*i;
        if (i!=n/i) ans+=phi(i)*(n/i);
      }
    printf("%I64d\n",ans);
}

你可能感兴趣的:(数论,bzoj,SDOI)