S5來襲4/11旗艦上市老王現身說法關鍵字魅力!法人:14檔今年本業虧定了學生退場 NHK:馬面...
2012-01-12 17:21:08 人氣(563) | 回應(0) | 推薦(0) | 收藏(0) 上一篇 | 下一篇

[UVA] 374 - Big Mod

0
收藏
0
推薦


 Big Mod 

Calculate

displaymath25

for large values of B, P, and M using an efficient algorithm. (That's right, this problem has a time dependency !!!.)

Input

Three integer values (in the order B, P, M) will be read one number per line. B and P are integers in the range 0 to 2147483647 inclusive. M is an integer in the range 1 to 46340 inclusive.

Output

The result of the computation. A single integer.

Sample Input

3
18132
17

17
1765
3

2374859
3029382
36123

Sample Output

13
2
13195


作法 : D&C


#include<stdio.h>
long long Mod(long long x, long long y, long long m) {
    long long ans = 1, tmp = x;
    while(y) {
        if(y&1) {
            ans *= tmp;
            ans %= m;
        }
        y >>= 1;
        tmp *= tmp, tmp %= m;
    }
    return ans;
}
int main() {
    int B, P, M;
    while(scanf("%d %d %d", &B, &P, &M) == 3) {
        printf("%lld\n", Mod(B, P, M));
    }
    return 0;
}

374Big Mod
台長:Morris
人氣(563) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 458 - The Decoder
此分類上一篇:[UVA] 369 - Combinations

我要回應
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入以下數字 (ex:123)

(有*為必填)
詳全文