• fzu 1759 Super A^B mod C 大数幂取模


    /*
    * 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;
    }


     1000MS

    /*
    * FZU1759.cpp
    *
    * Created on: 2010-9-17
    * Author: bjfuwangzhu
    */
    #include<iostream>
    #include<cmath>
    #include<string.h>
    using namespace std;
    const int Val = 50001;
    int prime[Val];
    void mkPrime() {
    int i, j;
    memset(prime, 1, sizeof(prime));
    for (i = 2; i < Val; i++) {
    if (prime[i]) {
    for (j = i + i; j < Val; j += i)
    prime[j] = 0;
    }
    }
    for (i = 2, j = 0; i < Val; i++)
    if (prime[i])
    prime[j++] = i;
    }
    long long numPhi(long long n) {
    int i, te;
    long long phi;
    te = static_cast<int>(sqrt(n * 1.0));phi
    = n;
    for (i = 0; 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 numPow(long long n, long long m, long long r) {
    long long p = 1, a = static_cast<long long>(n);
    while (m) {
    if (m & 1)
    p = p * a % r;
    m >>= 1;
    a = a * a % r;
    }
    return static_cast<int>(p);
    }
    int getSum(char *b) {
    int sum = 0;
    int len = strlen(b);
    for (int i = 0; i < len; i++)
    sum = sum * 10 + b[i] - '0';
    return sum;
    }
    long long Solve(char *b, long long phi) {
    long long sum = 0;
    int len = strlen(b);
    for (int i = 0; i < len; i++) {
    sum = sum * 10 + b[i] - '0';
    sum %= phi;
    }
    return sum;
    }
    bool numCmp(char *b, long long phi) {
    long long sum = 0;
    int len = strlen(b);
    for (int i = 0; i < len; i++) {
    sum = sum * 10 + b[i] - '0';
    if (sum > phi)
    return true;
    }
    return false;
    }
    int main() {
    mkPrime();
    char r[1000010];
    long long a, b, phi, num;
    while (cin >> a >> r >> b) {
    phi = numPhi(b);
    a %= b;
    if (numCmp(r, phi))
    num = Solve(r, phi) + phi;
    else
    num = getSum(r);
    cout << numPow(a, num, b) << endl;
    }
    return 0;
    }

    125MS

    /*
    * 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 cmpCphi(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 getCP(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 (cmpCphi(phi, ch)) {
    b = getCP(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 ch[nnum];
    mkprime();
    while (~scanf("%d %s %d", &a, ch, &c)) {
    solve(a % c, c, ch);
    }
    return 0;
    }


    当A 、B很大时

  • 相关阅读:
    T3java核心API基础类
    java字符编码
    Servlet 1
    T2java面向对象
    T1java语言基础
    Mac OS mysql数据库安装与初始化
    java多线程中注入Spring对象问题
    T4java核心API集合类
    The first day Teddy
    Spring第二节 注入依赖
  • 原文地址:https://www.cnblogs.com/xiaoxian1369/p/2207488.html
Copyright © 2020-2023  润新知