1. 程式人生 > >a^b問題(南陽oj473)

a^b問題(南陽oj473)

A^B Problem

時間限制:1000 ms  |  記憶體限制:65535 KB

難度:2

描述

Give you two numbers a and b,how to know the a^b's the last digit number.It looks so easy,but everybody is too lazy to slove this problem,so they remit to you who is wise.

輸入

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

輸出

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

樣例輸入

7 66
8 800

樣例輸出

9
6

提示

There is no such case in which a = 0 && b = 0。

來源

hdu

上傳者

ACM_丁國強

這道題目 輸入a,b,求a的b次方,b通過對1-9的次方規律可以通過(b-1)%4+1來減小;

而a,因為題目要的是末尾位數,只需要a的個位即可;

多組資料,當a=0,b=0時可以繼續執行而不是終止。

#include <iostream>
#include<stdio.h>
using namespace std;
int main()
{
    long long int a,b;
    int i,n,c;
    while(scanf("%lld %lld",&a,&b)!=EOF)
    {
        if(b==0)
        {
            printf("%d\n",1);
            continue;
        }
            b=(b-1)%4+1;
            n=1;
            c=a%10;
            for(i=1;i<=b;i++)
            {
                n=n*c;
            }
            printf("%d\n",n%10);

    }
    return 0;
}