zoukankan      html  css  js  c++  java
  • bzoj3884 上帝与集合的正确用法

    题意:求2^2^2^2^2.......^2%p的值,T组询问。

    欧拉降幂多用几次就好了。

    顺便试了下fwrite输出优化 ,效果显著。

    #include<cstring>
    #include<iostream>
    #include<cctype>
    #include<cstdio>
    #define writ(x,c) write(x),push(c);
    using namespace std;
    inline char nc()
    {
        static char buf[100000],*p1=buf,*p2=buf;
        return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int read()
    {
        char c;int x=0;bool f=0;
        for(; !isdigit(c); c=nc()) if(c=='-') f=1;
        for(; isdigit(c); c=nc()) x=(x<<1)+(x<<3)+c-48;
        return (f ? -x : x);
    }
    char pbuf[100000],*pp=pbuf;
    void push(const char c)
    {
        if(pp-pbuf==100000) fwrite(pbuf,1,100000,stdout),pp=pbuf;
        *pp++=c;
    }
    void write(int x)
    {
        static int sta[35];
        int top=0;
        do{sta[top++]=x%10,x/=10;}while(x);
        while(top) push(sta[--top]+'0');
    }
    const int M=1001001;
    int phi[M],prime[M],tot;
    bool not_prime[M];
    inline int Phi(int x)
    {
        int i,re=x;
        for(i=2; i*i<=x; i++)
            if(x%i==0)
            {
                re/=i;
                re*=i-1;
                while(x%i==0)
                    x/=i;
            }
        if(x^1) re/=x,re*=x-1;
        return re;
    }
    inline int KSM(long long x,int y,int p)
    {
        long long res=1;
        while(y)
        {
            if(y&1) (res*=x)%=p;
            (x*=x)%=p;
            y>>=1;
        }
        return res%p;
    }
    inline int calc(int p)
    {
        if(p==1) return 0;
        int tmp=0,cur,res;
        while(~p&1) p>>=1,++tmp;
        cur=Phi(p);
        res=calc(cur);
        (res+=cur-tmp%cur)%=cur,res=KSM(2,res,p);
        return res<<tmp;
    }
    int main()
    {
        register int T=read(),p;
        while(T--)
        {
            p=read();
            writ(calc(p),'
    ');
        }
        fwrite(pbuf,1,pp-pbuf,stdout);
    }
  • 相关阅读:
    YOLO V5
    YOLO系列(单阶段目标检测)
    优化算法
    算法总结
    图像分类算法
    ResNet网络(里程碑)
    GoogleNet网络(纵横交错)
    VGGNet网络(走向深度)
    AlexNet网络(开山之作)
    案例
  • 原文地址:https://www.cnblogs.com/mordor/p/9776495.html
Copyright © 2011-2022 走看看