swust.oj.303

    xiaoxiao2021-04-12  39

    取模(0303)

    Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 1658 Accepted: 534 Accepted 15级卓越班选拔C 16级卓越班选拔C 14级卓越班选拔C 14级卓越班选拔C

    给你三个数X(1<=X<=10^100)、Y(1<=Y<=10^8)、Z(1<=Z<=10^4),你能计算出X^Y%Z的值吗?

    Description

    输入三个如上所描述的数X、Y、Z。多组输入。

    Input

    输出X^Y%Z的值。

    Output 2 3 5 12345 2345 345 123456789123456789 19234321 2341 Sample Input 3 240 1825 #include <iostream> #include <stdio.h> #include<string.h> using namespace std; int main() { char x[100]; int y, z; while (cin >> x >> y >> z) { int t = 0; for (int i = 0; i < strlen(x); i++) { t = (t * 10 + x[i] - '0') % z; }//我也很迷啊,不知道这里是什么原理 int ans = 1; while (y) { if (y % 2) ans = (ans*t) % z; y = y / 2; t = (t*t) % z; }//快速幂 cout << ans << endl; } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-667282.html

    最新回复(0)