首页 > 其他 > 详细

bzoj3884: 上帝与集合的正确用法

时间:2018-10-31 10:29:03      阅读:173      评论:0      收藏:0      [点我收藏+]

这个就是指数对phi取模啊

然而欧拉定理只在(a,p)==1的情况下成立

但是有一个很强的推论,就是当x>phi(p)的时候a^x%p=a^(x%phi(p)+phi(p))成立

那么这题就秒了

线筛phi会T T_T

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;


int quick_pow(int A,int p,int mod)
{
    int ret=1;
    while(p>0)
    {
        if(p%2==1)ret=(LL)ret*A%mod;
        A=(LL)A*A%mod;p/=2;
    }
    return ret;
}
int phi(int x)
{
    int ret=x;
    for(int i=2;i*i<=x;i++)
        if(x%i==0)
        {
            ret=ret/i*(i-1);
            while(x%i==0)x/=i;
        }
    if(x!=1)ret=ret/x*(x-1);
    return ret;
}
int solve(int p)
{
    if(p==1)return 0;
    int pp=phi(p);
    return quick_pow(2,solve(pp)+pp,p);
}

int main()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int p;
        scanf("%d",&p);
        printf("%d\n",solve(p));
    }
    return 0;
}

 

bzoj3884: 上帝与集合的正确用法

原文:https://www.cnblogs.com/AKCqhzdy/p/9880909.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!