码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1061

时间:2017-11-26 15:56:56      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:clu   cond   非递归   ble   space   hdu 1061   using   pos   htm   

1、链接

http://acm.hdu.edu.cn/showproblem.php?pid=1061

2、题目

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.

3、解题分析

快速幂取模(否则会超时)

4、Code

#include<bits/stdc++.h>
using namespace std;

/*递归*/
//int quick_pow(int base, int n, int mod)
//{
//    if(n == 0)
//        return 1;
//    if(n == 1)
//        return base%mod;
//    int res = quick_pow(base,n>>1,mod);
//    res = res * res % mod;
//    if(n&1)
//    {
//        res = res*base%mod;
//    }
//    return res;
//}

/*非递归*/
int quick_pow(int base, int n, int mod)
{
    int res = 1;
    while(n)
    {
        if(n&1)
        {
            res = res * base%mod;
        }
        base = base * base % mod;
        n>>=1;
    }
    return res;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        printf("%d\n",quick_pow(n%10,n,10));
    }
    return 0;
}

 

hdu 1061

标签:clu   cond   非递归   ble   space   hdu 1061   using   pos   htm   

原文地址:http://www.cnblogs.com/hhkobeww/p/7898988.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!