3990 G. I guess the gift is a bag! II

来源:互联网 发布:淘宝优惠券去哪里领 编辑:程序博客网 时间:2024/05/16 18:03

Coach Yu has lots of bags, every bag has its number. And what is more, he wants to send these bags to acm team-member. But he requires that if you get the bag and its number is b, you need to count how many number smaller than b and co-prime with b. If you can’t count it, he won’t send the bag to you.
However, G.D.R and Hacb learn the The Euler function to solve the problem. And they are so friendly that they like to share the function to you, so you can get the bag.
The Euler function is:
F(b) = b *( 1 - 1/p1) ( 1 - 1/p2)……(1-1/pn)
Notes: pi is the ith Prime factors of b, and there will be no such i and j which i<>j but pi=pj.
Input

A number b as describe . 0

#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>const int N=1e6+1;bool is_prime[N];int prime[N];using namespace std;int  sieve (int n){    int p=0;    for(int i=0;i<=n;i++)    {        is_prime[i]=true;    }    is_prime[0]=is_prime[1]=false;    for(int i=2;i<=n;i++)    {        if(is_prime[i])        {            prime[p++]=i;            for(int j=2*i;j<=n;j+=i)            {                is_prime[j]=false;            }        }    }    return p;}int main (void){    int num=1e6;    int p=sieve(num);    long long b;    while(~scanf("%lld",&b)&&b!=0)    {        if(b==1)        {            printf("0\n");            continue;        }        long long ans=b;        for(int i=0;i<p&&prime[i]<=b;i++)        {            if(b%prime[i]==0)            {                ans=ans/prime[i]*(prime[i]-1);                //1-(1/p1)=((p1-1)/p1)                while(b%prime[i]==0)                {                    b=b/prime[i];//把1e6之内的先筛掉                }            }        }        if(b>1)//还有一个在1e6之外的数        {            ans=ans/b*(b-1);        }        printf("%lld\n",ans);    }    return 0;}
0 0