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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值