SGU102 欧拉函数 傻逼题

此题就是利用欧拉函数的性质求快速欧拉函数值 Woc,辣鸡数据直接枚举都Accept啊

#include 

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template<typename T> inline bool is_digit(const T &a) { return a >= '0' && a <= '9'; }

template<class T> inline T read()
{
    T sum = 0, fg = 1, c = getchar();
    while (!is_digit(c)) { if (c == '-') fg = -1; c = getchar(); }
    while (is_digit(c)) sum = sum * 10 + c - '0', c = getchar();
    return sum * fg;
}

typedef long long LL;

const int dmax = 300100, oo = 0x3f3f3f3f;

int N, M;

int prime[dmax], phi[dmax], cnt = 0;

inline void init_prime(const int &N)
{
    static bool p[dmax];
    memset(p, 0, sizeof p);
    p[1] = 1;
    phi[1] = 1;
    REP(i, 2, N)
    {
        if (!p[i])
        {
            prime[++cnt] = i;
            phi[i] = i - 1;
        }
        for (int j = 1; i * prime[j] <= N && j <= cnt; ++j)
        {
            p[i * prime[j]] = 1;
            if (!(i % prime[j]))
            {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            phi[i * prime[j]] = phi[i] * (prime[j] - 1);
        }
    }
}

int main()
{
#ifndef ONLINE_JDUGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    init_prime(N = read<int>());
    printf("%d\n", phi[N]);
    return 0;
}

你可能感兴趣的:(算法)