HYSBZ - 3884_上帝与集合的正确用法_欧拉降幂

题意

这里写图片描述

思路

递归地利用欧拉降幂公式,直到模数<= 3。
这里有一个结论是,对 p 求欧拉数直到 1 最多需要求 log(p) 次。

链接

https://cn.vjudge.net/contest/176263#status/nwpu201630314/C/0/

代码

#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;

typedef long long LL;

const int maxn = 1e7 + 10;

int ans[maxn];
int n, t;

LL quick_pow(int a, int b, int c)
{
    LL res = 1, t = a;

    while(b > 0)
    {
        if(b & 1) res = (res * t) % c;
        t = (t * t) % c;
        b >>= 1;
    }

    return res;
}

LL get_phi(LL C)
{
    LL res = C;

    for(int i= 2; i * i <= C; i++)
    {
        if(C % i == 0)
        {
            res = res / i * (i - 1);
            while(C % i == 0) C /= i;
        }
    }

    if(C > 1) res = res / C * (C - 1);

    return res;
}

int solve(int t)
{
    if(ans[t] >= 0) return ans[t];

    int phit = get_phi(t);
    int res = quick_pow(2, solve(phit) + phit, t);

    return res;
}

int main()
{
    //freopen("in.txt", "r", stdin);

    memset(ans, -1, sizeof ans);
    ans[1] = ans[2] = 0, ans[3] = 1;

    scanf("%d", &n);

    while(n --)
    {
        scanf("%d", &t);
        cout << solve(t) << endl;
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值