bzoj3884 上帝与集合的正确用法
生活随笔
收集整理的這篇文章主要介紹了
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),'\n');}fwrite(pbuf,1,pp-pbuf,stdout); }?
轉載于:https://www.cnblogs.com/mordor/p/9776495.html
總結
以上是生活随笔為你收集整理的bzoj3884 上帝与集合的正确用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 006python路--深浅拷贝
- 下一篇: 初赛问题求解及选择题数学相关整理