UVA 10820 - Send a Table(欧拉函数)

题目链接 https://cn.vjudge.net/problem/UVA-10820

【题意】
输入n(n<=50000),求出有多少个二元组(x,y)满足1<=x,y<=n 且x,y互素

【思路】
在符合要求的二元组中,除了(1,1)之外其它的(x,y)中的x和y都不想等,假设满足x < y的二元组有f[n]个,那么f[n]=phi[2]+phi[3]+…+phi[n],phi为欧拉函数,最终的答案即为2*f[n]+1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 50050;

int n;
int phi[maxn];
ll f[maxn];
void phi_table(int n) {
    for (int i = 2; i <= n; ++i) phi[i] = 0;
    phi[1] = 1;
    for (int i = 2; i <= n; ++i) {
        if (0 == phi[i]) {
            for (int j = i; j <= n; j += i) {
                if (0 == phi[j]) phi[j] = j;
                phi[j] = phi[j] / i * (i - 1);
            }
        }
    }
}

int main(){
    phi_table(maxn-1);
    f[0]=f[1]=0;
    for(int i=2;i<maxn;++i) f[i]=f[i-1]+phi[i];
    while(scanf("%d",&n)==1 && n){
        ll ans=2*f[n]+1;
        printf("%lld\n",ans);
    }
    return 0;
}
posted @ 2018-08-21 23:19  不想吃WA的咸鱼  阅读(111)  评论(0编辑  收藏  举报