BZOJ 3884 [欧拉定理]

Description

22222..modp

Solution

欧拉定理: ananmodφ(p) (modp) , (gcd(n,p)=1)
EXT: ananmodφ(p)+φ(p) (modp)
这道题就一直递归地求解就行了,因为一个数一直求 φ 会在 O(logn) 的时间复杂度内变成1。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 10110;
typedef long long ll;

inline char get(void) {
    static char buf[100000], *S = buf, *T = buf;
    if (S == T) {
        T = (S = buf) + fread(buf, 1, 100000, stdin);
        if (S == T) return EOF;
    }
    return *S++;
}
template<typename T>
inline void read(T &x) {
    static char c; x = 0; bool sgn = 0;
    for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
    for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
    if (sgn) x = -x;
}

int test, p;

int Phi(int x) {
    int phi = x, m = sqrt(x);
    for (int i = 2; i <= m; i++)
        if (x % i == 0) {
            while (x % i == 0) x /= i;
            phi /= i; phi *= i - 1;
        }
    if (x != 1) {
        phi /= x; phi *= x - 1;
    }
    return phi;
}
inline int Pow(int a, int b, int p) {
    int c = 1;
    while (b) {
        if (b & 1) c = (ll)c * a % p;
        b >>= 1; a = (ll)a * a % p;
    }
    return c % p;
}
inline int Pow2(int p) {
    if (p == 1) return 0;
    int k = 0, phi, res;
    while (~p & 1) {
        p >>= 1; k++;
    }
    phi = Phi(p);
    res = Pow2(phi);
    res = (res + phi - k % phi) % phi;
    res = Pow(2, res, p);
    return res << k;
}

int main(void) {
    freopen("1.in", "r", stdin);
    read(test);
    while (test--) {
        read(p);
        printf("%d\n", Pow2(p));
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值