【欧拉函数】POJ 2407 Relatives

POJ 2407 Relatives

模板题

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define P(x) x>0 ? x : 0
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define lowbit(x) x & (-x)

using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const ll maxN=1e7;

bool Is_Prime(ll x)
{
    if( x == 2 ) return true;
    for(ll i = 2 ; i * i <= x ; i++)
    {
        if( x % i == 0)
            return false;
    }
    return true;
}

set<ll> st;

void prime_factor(ll x)
{
    for(ll i = 2 ; i <= x ; i++)
    {
        if(Is_Prime(x))
        {
            st.insert(x) ;
            return ;
        }
        if(Is_Prime(i))
        {
            while(x % i == 0)
            {
                x /= i;
                st.insert(i);
            }
        }
    }
}

ll Euler(ll x)
{
    if(x == 1) return 1;
    ll up = x, down = 1;
    prime_factor(x);
    while(! st.empty())
    {
        int tmp = *st.begin();
        st.erase(st.begin());
        up *= (tmp - 1);
        down *= tmp;
    }
    return up / down;
}

int main()
{
    ll n;
    while(~ scanf("%lld", &n) && n)
    {
        printf("%lld\n", Euler(n));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值