24h購物| | PChome| 登入
2012-07-14 07:07:56| 人氣431| 回應0 | 上一篇 | 下一篇

[UVA][D&C] 1230 - MODEX

推薦 0 收藏 0 轉貼0 訂閱站台

Many well-known cryptographic operations require modular exponentiation. That is, given integers x , y and n , compute xy mod n . In this question, you are tasked to program an efficient way to execute this calculation.

Input 

The input consists of a line containing the number c of datasets, followed by c datasets, followed by a line containing the number `0'.

Each dataset consists of a single line containing three positive integers, x , y , and n , separated by blanks. You can assume that 1 < x , n < 215 = 32768 , and 0 < y < 231 = 2147483648 .

Output 

The output consists of one line for each dataset. The i -th line contains a single positive integer z such that

z = xy mod n

for the numbers x , y , z given in the i -th input dataset.

Sample Input 

2 
2 3 5 
2 2147483647 13 
0

Sample Output 

3 
11


#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;
}
tmp = tmp*tmp, tmp %= m;
y >>= 1;
}
return ans;
}
int main() {
int t, x, y, n;
scanf("%d", &t);
while(t--) {
scanf("%d %d %d", &x, &y, &n);
printf("%lld\n", Mod(x, y, n));
}
return 0;
}
 


台長: Morris
人氣(431) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Java] 11821 - High-Precision Number
此分類上一篇:[UVA][JAVA] 10464 - Big Big Real Numbers

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文