FZU 1759 Super A^B mod C (欧拉降幂)
来源:互联网 发布:mysql汉字转拼音函数 编辑:程序博客网 时间:2024/05/29 17:59
Problem:
给一个很大的B,求A^B mod C的值。
Solution:
通过欧拉降幂把B缩小,其中要用到高精度取余和快速幂取余。
欧拉降幂:
#include<cstdio>#include<iostream>#include<sstream>#include<cstdlib>#include<cmath>#include<cctype>#include<string>#include<cstring>#include<algorithm>#include<stack>#include<queue>#include<set>#include<map>#include<ctime>#include<vector>#include<fstream>#include<list>#include<iomanip>using namespace std;typedef long long ll;typedef unsigned long long ull;#define ms(s) memset(s,0,sizeof(s))const double PI = 3.141592653589;const int INF = 0x3fffffff;long long euler_phi(long long n) { long long m = sqrt(n); long long ans = n; for(long long i = 2; i <= m; i++) { if(n%i == 0) { //用变量保存i即最大的素因数 ans = ans / i * (i-1); while(n%i == 0) n /= i; } if(n == 1) break; } if(n > 1) ans = ans / n * (n-1); return ans;}long long q_mod(long long a, long long b, long long m){ long long ans = 1; for( ; b; b >>= 1){ if(b & 1) ans = ans*a % m; a = a*a % m; } return ans;}long long long_mod(string a, long long b){ int flag = 1, idx = 0; if(a[0] == '-'){ flag = -1; idx++; } long long r = a[idx++] - 48; while(idx < a.length()){ r = (r*10 + a[idx]-48) % b; idx++; } return r*flag;}int main() {// freopen("/Users/really/Documents/code/input","r",stdin);// freopen("/Users/really/Documents/code/output","w",stdout); ios_base::sync_with_stdio(false); cin.tie(0); ll a, b, c, pc; string b_in; while(cin >> a >> b_in >> c) { pc = euler_phi(c); b = long_mod(b_in, pc) + pc; cout << q_mod(a, b, c) << endl; } return 0;}
阅读全文