欧拉降幂公式—— BZOJ 3884 && FZU 1759
来源:互联网 发布:销售手机的软件 编辑:程序博客网 时间:2024/06/06 20:37
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 42 10 1000
Sample Output
124
只需利用欧拉公式降幂即可
#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[N];//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
3236
Sample Output
014
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[N];//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; }}
阅读全文
0 0
- 欧拉降幂公式—— BZOJ 3884 && FZU 1759
- FZU - 1759 Super A^B mod C 降幂公式
- FZU——上车
- fzu —Salty Fish
- FZU 1759 Super A^B mod C (欧拉函数,快速幂,降幂公式)
- FZU 1759-Super A^B mod C (欧拉函数+降幂公式)
- FZU 1759 Super A^B mod C (欧拉函数,降幂公式)
- FZU 2121神庙逃亡(物理速度公式)
- FZU——数字游戏
- fzu——单词问题
- FZU—2150 Fire Game
- fzu——Problem 2088 最长队名
- fzu——Problem 2089 数字游戏
- fzu——Problem 2091 播放器
- fzu——旅行社的烦恼
- fzu——Problem 2034 Password table
- fzu 2105——Digits Count
- 状压DP——FZU 2218
- android中得到系统时间
- TextAsset,直接拉取文本对象
- RxJava+Retrofit+OkHttp3+Dagger2+MVP构建Android项目简单例子
- Mysql 数据库几种引擎的区别比较
- 左手坐标系
- 欧拉降幂公式—— BZOJ 3884 && FZU 1759
- [LeetCode]520. Detect Capital
- Java静态
- Java中Runnable和Thread的区别
- Vuforia+Hololens定位问题
- Java日期和时间
- s3c2440led驱动分析
- float类型变量之间的相加减存在一些误差
- linux系统基础知识——面试