【BZOJ3884】【欧拉函数】上帝与集合的正确用法 题解

10 篇文章 0 订阅

这里写图片描述

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2147483647
#define LL long long
#define clr(x) memset(x, 0, sizeof x)
#define digit (ch <  '0' || ch >  '9')
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

template <class T> inline void read(T &x) {
    int flag = 1; x = 0;
    register char ch = getchar();
    while( digit) { if(ch == '-')  flag = -1; ch = getchar(); }
    while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
    x *= flag;
}

const int maxm = 1e7+9;
int phi[maxm],t,p;

int Phi(int x) {
    int re = x;
    for(int i = 2; i*i <= x; i++) if(!(x%i)) {
        re /= i; re *= i-1;
        while(!(x%i)) x /= i;
    }
    if(x^1) re /= x, re *= x-1;
    return re;
}

int powermod(int y, int p) {
    LL tmp = 2, ans = 1;
    while(y) {
        if(y&1) ans = (ans*tmp)%p;
        tmp = (tmp*tmp)%p; y >>= 1;
    }
    return ans;
}

int solve(int p) {
    if(p == 1) return 0;
    int tmp = 0;
    while(~p&1) p >>= 1, ++tmp;
    int phi_p = Phi(p), ans = solve(phi_p);
    ans = (ans+phi_p-tmp%phi_p)%phi_p;
    ans = powermod(ans, p)%p;
    return ans<<tmp;
}

int main() {
    read(t);
    while(t--) read(p), printf("%d\n", solve(p));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值