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;}
阅读全文
'); })();
1 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 纸境奇缘 纸境下载 辟纸图 电脑璧纸 璧纸图片 手机璧纸图片 动态纸壁下载软件 手机纸壁大全 桌面纸壁图片 桌面纸壁 纸壁大全下载 动态纸壁下载 壁支 爱纸壁 手机纸壁软件 跑车纸壁 炫酷纸壁 qq纸壁 手机纸壁动态 笔记本纸壁 纸壁软件下载 ipad纸壁 好看的墙纸和锁屏图片 纸壳侠 纸壳手工 纸壳打包机 纸壳箱 纸壳手工大全 纸壳飞机做法大全图片 纸壳多少钱一斤 纸壳手工制作大全图片 纸壳多少钱一斤2019 纸壳制作飞机步骤 废纸壳回收价格 纸壳箱多少钱一斤 纸壳手工制作 纸婚 一纸婚约 纸婚多少年 纸婚厚爱总裁轻装上阵 奉纸橙婚