HDU-1061,Rightmost Digit(快速幂)

简介: HDU-1061,Rightmost Digit(快速幂)

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:

2

3

4


Sample Output:


7


6

Hint

In the first case, 3 * 3 * 3 = 27, so the rightmost digit is 7. In the second case, 4 * 4 * 4 * 4 = 256, so the rightmost digit is 6.


程序代码:


#include<bits/stdc++.h>
using namespace std;
int quickmul(int n)
{
  int ans=1;
  int temp=n%10;
  while(n)
  {
    if(n&1)//等价于n%2==1 
      ans=(ans*temp)%10;
    temp=(temp*temp)%10;
    n>>=1;//等价于n=n/2 
  }
  return ans;
}
int main()
{
  int a,n,count;
  cin>>n;
  while(n--)
  {
    cin>>a;
    cout<<quickmul(a)<<endl;
  }
  return 0;
}


相关文章
|
8月前
|
算法
华为机试HJ108:求最小公倍数
华为机试HJ108:求最小公倍数
|
7月前
|
机器学习/深度学习
hdu 1061 Rightmost Digit
hdu 1061 Rightmost Digit
18 0
|
7月前
hdu1406 完数 (水题)
hdu1406 完数 (水题)
33 0
|
机器学习/深度学习 网络架构
题解 UVA10212 【The Last Non-zero Digit.】
题目链接 这题在学长讲完之后和看完题解之后才明白函数怎么构造。这题构造一个$f(n)$$f(n)$ $=$ $n$除以 $2^{a}$ $*$ $5^{b}$ ,$a$ , $b$ 分别是 $n$ 质因数分解后$2,5$的个数。
1214 0
|
机器学习/深度学习
|
人工智能
【HDU 4451 Dressing】水题,组合数
有衣服、裤子、鞋数量分别为n,m,k,给出p对不和谐的衣-裤或裤-鞋搭配,问一共有多少种和谐的衣裤鞋的搭配。 全部的组合有Cn1Cm1Ck1种。 设p对中有p1对衣-裤,p2对裤-鞋,则不和谐的搭配共有p1*Ck1+p2*Cn1种,但有被重复计算两次的搭配共p3对,它们引用了同一裤。
892 0