欧拉降幂公式—— BZOJ 3884 && FZU 1759
2017-08-08 17:58
477 查看
C - Super A^B mod C
Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B<=10^1000000).
Input
There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.
Output
For each testcase, output an integer, denotes the result of A^B mod C.
Sample Input
3 2 4 2 10 1000
Sample Output
1 24
只需利用欧拉公式降幂即可
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<queue> #include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; typedef long long ll; const int INF_INT = 0x3f3f3f3f; const ll INF_LL = 1e18; const int MOD=1e9 + 7; const int N = 100005; //int phi ; //vector<int> prime; //void init() //{ // phi[1] = 1; // for(int i=2;i<N;i++) // { // if(phi[i]==0) // { // prime.push_back(i); // phi[i] = i-1; // } // for(int j=0;j<prime.size()&&prime[j]*i<N;j++) // { // if(i%prime[j]==0) // { // phi[i*prime[j]] = phi[i]*prime[j]; // break; // } // else // { // phi[i*prime[j]] = phi[i]*(prime[j]-1); // } // } // } //} //计算欧拉函数O(sqrt(n)) 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; } ll Quick_Power(ll x,ll y,ll p) { ll re=1; while(y) { if(y&1) (re*=x)%=p; (x*=x)%=p; y>>=1; } return re; } int Solve(int p) { if(p==1) return 0; int phi_p = Phi(p); return Quick_Power(2,Solve(phi_p)+phi_p,p); } int T,n,a,c; string b; int main() { ios_base::sync_with_stdio(false); // freopen("data.txt","r",stdin); while(cin>>a>>b>>c) { int phi_c = Phi(c); ll sum = 0; for(int i=0;i<b.size();i++) { sum = (sum*10+b[i]-'0')%(phi_c); } cout <<Quick_Power(a,sum+phi_c,c)<<endl; } }
BZOJ 3884 上帝与集合的正确用法
根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”。 第二天, 上帝创造了一个新的元素,称作“α”。“α”被定义为“元”构成的集合。容易发现,一共有两种不同的“α”。 第三天, 上帝又创造了一个新的元素,称作“β”。“β”被定义为“α”构成的集合。容易发现,一共有四种不同的“β”。 第四天, 上帝创造了新的元素“γ”,“γ”被定义为“β”的集合。显然,一共会有16种不同的“γ”。 如果按照这样下去,上帝创造的第四种元素将会有65536种,第五种元素将会有2^65536种。这将会是一个天文数字。 然而,上帝并没有预料到元素种类数的增长是如此的迅速。他想要让世界的元素丰富起来,因此,日复一日,年复一年,他重复地创造着新的元素…… 然而不久,当上帝创造出最后一种元素“θ”时,他发现这世界的元素实在是太多了,以致于世界的容量不足,无法承受。因此在这一天,上帝毁灭了世界。 至今,上帝仍记得那次失败的创世经历,现在他想问问你,他最后一次创造的元素“θ”一共有多少种? 上帝觉得这个数字可能过于巨大而无法表示出来,因此你只需要回答这个数对p取模后的值即可。 你可以认为上帝从“α”到“θ”一共创造了10^9次元素,或10^18次,或者干脆∞次。
Input
接下来T行,每行一个正整数p,代表你需要取模的值
Output
T行,每行一个正整数,为答案对p取模后的值
Sample Input
3 2 3 6
Sample Output
0 1 4
Hint
对于100%的数据,T<=1000,p<=10^7
利用递归形式求解
出题人题解
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<queue> #include<cstdio> #include<cstring> #include<cmath> #include<map> using namespace std; typedef long long ll; const int INF_INT = 0x3f3f3f3f; const ll INF_LL = 1e18; const int MOD=1e9 + 7; const int N = 100005; //int phi ; //vector<int> prime; //void init() //{ // phi[1] = 1; // for(int i=2;i<N;i++) // { // if(phi[i]==0) // { // prime.push_back(i); // phi[i] = i-1; // } // for(int j=0;j<prime.size()&&prime[j]*i<N;j++) // { // if(i%prime[j]==0) // { // phi[i*prime[j]] = phi[i]*prime[j]; // break; // } // else // { // phi[i*prime[j]] = phi[i]*(prime[j]-1); // } // } // } //} //计算欧拉函数O(sqrt(n)) 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; } ll Quick_Power(ll x,ll y,ll p) { ll re=1; while(y) { if(y&1) (re*=x)%=p; (x*=x)%=p; y>>=1; } return re; } int Solve(int p) { if(p==1) return 0; int phi_p = Phi(p); return Quick_Power(2,Solve(phi_p)+phi_p,p); } int T,n; string s; int main() { ios_base::sync_with_stdio(false); // freopen("data.txt","r",stdin); cin>>T; while(T--) { cin >> n; cout <<Solve(n)<<endl; } }
相关文章推荐
- FZU - 1759_Super A^B mod C_Euler降幂公式&&求phi(n)模板题
- hdu4704 && FZU1759 && HYSBZ3884 &&POJ3090 欧拉函数 || 欧拉降幂 ||费马小定理
- 【BZOJ 3884】上帝与集合的正确用法【欧拉定理】&【剧毒题】
- [bzoj4822][Cqoi2017]老C的任务&[bzoj1935][Shoi2007]Tree 园丁的烦恼
- BZOJ 4292 [PA2015] Równanie - 数位&思维
- 可并堆--左偏树(Bzoj1367&&Bzoj1455)
- bzoj 2453 && bzoj 2120 分块
- [计数][容斥] LOJ#6065 || BZOJ4927 && 2017 山东一轮集训 Day3. 第一题
- bzoj 4548: 小奇的糖果 && bzoj 3658: Jabberwocky(双向链表+树状数组)
- [BZOJ3680]吊打XXX && 模拟退火
- [BZOJ3671][Noi2014]随机数生成器 && 贪心
- Kruskal算法及其类似原理的应用——【BZOJ 3654】tree&&【BZOJ 3624】[Apio2008]免费道路
- 树套树专题——bzoj 3110: [Zjoi2013] K大数查询 & 3236 [Ahoi2013] 作业 题解
- BZOJ4942 & UOJ314:[NOI2017]整数——题解
- bzoj 3637: Query on a tree VI 树链剖分 && AC600
- bzoj2730 [HNOI2012]矿场搭建 ( 割点 & 点双连通分量 )
- 【bzoj1602】【Usaco2008 Oct】牧场行走 (暴力) 题解&代码
- BZOJ 2125 最短路 && BZOJ 3047 Freda的传呼机【仙人掌最短路
- BZOJ1042:硬币购物(背包 & 容斥)
- [BZOJ 3156] 防御准备 · 斜率优化DP