zoukankan      html  css  js  c++  java
  • Day7

    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 4
    2 10 1000
    

    Sample Output

    1
    24

    思路:欧拉降幂板子
    typedef long long LL;
    
    LL A, C;
    char B[10000005];
    
    LL getEuler(LL x) {
        LL ans = x;
        for(LL i = 2; i*i <= x; ++i) {
            if(x % i == 0) {
                ans = ans / i * (i-1);
                while(x % i == 0) x /= i;
            }
        }
        if(x > 1) ans = ans / x * (x-1);
        return ans;
    }
    
    LL quickPow(LL a, LL b, LL p) {  // a^b (modp)
        LL ret = 1;
        while(b) {
            if(b & 1) ret = (ret * a) % p;
            a = (a * a) % p;
            b >>= 1; 
        }
        return ret;
    }
    
    
    int main() {
        ios::sync_with_stdio(false), cin.tie(NULL);
        while(cin >> A >> B >> C) {
            LL phi = getEuler(C);
            LL num = 0;
            int len = strlen(B);
            for(int i = 0; i < len; ++i) {
                num = (num * 10 + B[i] - '0');
                if(num >= phi) break;
            }
            if(num >= phi) {
                num = 0;
                for(int i = 0; i < len; ++i)
                    num = (num * 10 + B[i] - '0') % phi;
                cout << quickPow(A, num+phi, C) << "
    ";
            } else {
                cout << quickPow(A, num, C) << "
    ";
            }
        }
        
        return 0;
    }
    View Code
    
    
  • 相关阅读:
    IO模型
    Redis的cluster模式
    #4789. 啊孤独感放辣椒
    #4754. 旅途
    #3189. 八纵八横(c)
    洛谷P2624 [HNOI2008]明明的烦恼
    CF938G Shortest Path Queries
    [CTSC2010]珠宝商
    洛谷P1903 [国家集训队]数颜色 / 维护队列
    AtCoder3611 Tree MST
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12218707.html
Copyright © 2011-2022 走看看