• 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
    
    
  • 相关阅读:
    Linux文件结构
    磁盘分区
    BASH简介
    磁盘的基本概念
    Linux文件操作
    创建文件系统
    文件系统挂载
    一些常用命令
    asp.net创建PPT
    asp.net创建、删除、移动文件夹 文件
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/12218707.html
Copyright © 2020-2023  润新知