高精度、大整数幂取模
2013-08-22 19:22
99 查看
格式为:
cha^chb%c
cha、chb均为字符串
cha^chb%c
cha、chb均为字符串
/* * FZU1759.cpp * * Created on: 2011-10-11 * Author: bjfuwangzhu */ #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #define LL long long #define nnum 1000005 #define nmax 31625 int flag[nmax], prime[nmax]; int plen; void mkprime() { int i, j; memset(flag, -1, sizeof(flag)); for (i = 2, plen = 0; i < nmax; i++) { if (flag[i]) { prime[plen++] = i; } for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) { flag[i * prime[j]] = 0; if (i % prime[j] == 0) { break; } } } } int getPhi(int n) { int i, te, phi; te = (int) sqrt(n * 1.0); for (i = 0, phi = n; (i < plen) && (prime[i] <= te); i++) { if (n % prime[i] == 0) { phi = phi / prime[i] * (prime[i] - 1); while (n % prime[i] == 0) { n /= prime[i]; } } } if (n > 1) { phi = phi / n * (n - 1); } return phi; } int cmpBigNum(int p, char *ch) { int i, len; LL res; len = strlen(ch); for (i = 0, res = 0; i < len; i++) { res = (res * 10 + (ch[i] - '0')); if (res > p) { return 1; } } return 0; } int getModBigNum(int p, char *ch) { int i, len; LL res; len = strlen(ch); for (i = 0, res = 0; i < len; i++) { res = (res * 10 + (ch[i] - '0')) % p; } return (int) res; } int modular_exp(int a, int b, int c) { LL res, temp; res = 1 % c, temp = a % c; while (b) { if (b & 1) { res = res * temp % c; } temp = temp * temp % c; b >>= 1; } return (int) res; } void solve(int a, int c, char *ch) { int phi, res, b; phi = getPhi(c); if (cmpBigNum(phi, ch)) { b = getModBigNum(phi, ch) + phi; } else { b = atoi(ch); } res = modular_exp(a, b, c); printf("%d\n", res); } int main() { #ifndef ONLINE_JUDGE freopen("data.in", "r", stdin); #endif int a, c; char cha[nnum], chb[nnum]; mkprime(); while (~scanf("%s %s %d", cha, chb, &c)) { a = getModBigNum(c, cha); solve(a, c, chb); } return 0; }
相关文章推荐
- 模板--高精度、大整数幂取模
- c语言高精度大整数加法运算
- JAVA模板 大整数模板 高精度
- 大整数加法(高精度)
- 高精度整数除法
- 1403高精度整数去位去最小问题
- 【高精度计算 02】 大整数乘法
- 高精度整数 1
- BigInteger大整数类高精度
- 华为机试在线训练-牛客网(27)无线OSS-高精度整数加法
- 高精度整数的四则运算
- 小程序(十四)高精度整数加法
- 任意长度的高精度大整数加法
- 高精度整数加法(大整数加法)
- uva 424(Integer Inquiry)高精度大整数加法
- leetCode 43.Multiply Strings(高精度整数相乘) 解题思路和方法
- OpenJudge百炼-2981-大整数加法-C语言-高精度计算
- 高精度大整数加法
- 高精度整数模板
- poj 1503 大整数高精度 加法