Send a Table UVA - 10820

问题

分析

题目的本质是:输入n,有多少个二元组(x,y)满足 1 x , y n 1\le x,y \le n ,且x和y互素,不难发现除了(1,1)之外,其他二元组(x,y)中的x,y都不等,设满足x<y的二元组有f(n)个,那么总的有2f(n)+1个
使用欧拉函数,可以得到 f ( n ) = i = 2 n ϕ ( i ) f(n)=\sum_{i=2}^n\phi(i)

#include <cstring>
#include <cstdio>
#include <vector>
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int maxn=50000+5;
int phi[maxn];  //欧拉函数phi的函数值
int ans[maxn];
void phi_table(){
    memset(phi,0, sizeof(phi));
//    phi[1]=1;
    for(int i=2;i<maxn;++i){
        if(!phi[i]){
            for(int j=i;j<maxn;j+=i){
                if(!phi[j]) phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
    for(int i=2;i<maxn;++i) ans[i]=ans[i-1]+phi[i];
}
int n;
int main(void){
    phi_table();
    while(cin>>n && n){
        printf("%d\n",(ans[n]<<1)+1);
    }
}
发布了50 篇原创文章 · 获赞 0 · 访问量 675

猜你喜欢

转载自blog.csdn.net/zpf1998/article/details/104237433