1. 程式人生 > >HDU1097 A hard puzzle【快速模冪】

HDU1097 A hard puzzle【快速模冪】

A hard puzzle

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52171 Accepted Submission(s): 19072

Problem Description
lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.
this puzzle describes that: gave a and b,how to know the a^b's the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input
There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output
For each test case, you should output the a^b's last digit number.

Sample Input
7 66
8 800

Sample Output
9
6

Author
eddy

問題連結HDU1097 A hard puzzle
問題簡述:(略)
問題分析
    這個問題是計算a^b的最後1位,標準的快速模冪模板題。
程式說明

:(略)
參考連結:(略)
題記:(略)

AC的C語言程式如下:

/* HDU1097 A hard puzzle */

#include <stdio.h>

typedef long long LL;

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

    return result;
}

int main(void)
{
    int a, b;
    while(scanf("%d%d", &a, &b) != EOF)
        printf("%lld\n", powmod(a, b, 10));

    return 0;
}