UVA1230 LA4104 MODEX【快速模幂】

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 = x y   m o d   n z = x^y  mod  n 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

问题链接UVA1230 LA4104 MODEX
问题简述:(略)
问题分析:快速模幂问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA1230 LA4104 MODEX */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

// 快速模幂
LL powmod(LL x, LL n, LL m)
{
    LL ret = 1;
    for(; n; n >>= 1) {
        if(n & 1) {
            ret *= x;
            ret %= m;
        }
        x *= x;
        x %= m;
    }

    return ret;
}

int main()
{
    int t, x, y, n;
    while (~scanf("%d", &t) && t) {
        while (t--) {
            scanf("%d%d%d", &x, &y, &n);
            printf("%lld\n", powmod(x, y, n));
        }
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值