码迷,mamicode.com
首页 > 其他好文 > 详细

筛法求欧拉函数(poj2478

时间:2019-08-05 09:15:30      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:欧拉   tchar   pac   namespace   筛法   +=   getc   cst   return   

求1~n的欧拉函数的值

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstring>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)

using namespace std;
long long n;
long long a[N];
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<0||c>9){if(c==-)y=-1;c=getchar();}
    while(c<=9&&c>=0){ x=(x<<1)+(x<<3)+c-0;c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p(-);x=-x;}
    if(x>9)o(x/10);
    p(x%10+0);
}

int main(){
    a[1]=1;
    For(i,2,1e6)
        if(!a[i])
            for(long long j=i;j<=1e6;j+=i){
                if(!a[j]) a[j]=j;
                a[j]=a[j]/i*(i-1);
            }   
    For(i,1,1e6)
        a[i]+=a[i-1];
    while(cin>>n&&n){
        o(a[n]-1);p(\n);
    }
    return 0;
}

 

筛法求欧拉函数(poj2478

标签:欧拉   tchar   pac   namespace   筛法   +=   getc   cst   return   

原文地址:https://www.cnblogs.com/war1111/p/11301031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!