快速幂1003

来源:互联网 发布:数据库编程语言 编辑:程序博客网 时间:2024/05/17 07:25

Rightmost Digit

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 44   Accepted Submission(s) : 26

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

Given a positive integer N, you should output the most right digit of N^N.

Input

The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single positive integer N(1<=N<=1,000,000,000).

Output

For each test case, you should output the rightmost digit of N^N.

Sample Input

234

Sample Output

76
思路:这题可以用快速幂来做,只要每次都整除10,就不会变成大数了。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#include <vector>using namespace std;int poww(long long int a,long long int b){     int m,n;     m=1%10;     n=a%10;     while(b)     {         if(b&1)         {             m=(m*n)%10;         }         n=(n*n)%10;         b>>=1;     }     return m;}int main(){   long long int T,n;    cin>>T;    while(T--)    {        cin>>n;        cout<<poww(n,n)<<endl;    }    return 0;}