HDU--1061

Rightmost Digit

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 39389    Accepted Submission(s): 14863

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.

Author

Ignatius.L

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

long long int powermod(long long int a,long long int b,long long int c)
{
    long long int ans = 1;
    a = a%c;
    while (b > 0)
    {
        if (b% 2 ==1)
            ans = ans * a %c;
        b = b/2;
        a = a* a %c;
    }
    return ans;
}

int main()
{
    int t;
    long long int n;
    cin >> t;
    while (t--)
    {
        cin >> n;
        cout <<powermod(n,n,10) << endl;
    }
    return 0;
}

快速幂求余!

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-19 00:28:09

HDU--1061的相关文章

HDU 1061 Rightmost Digit题解

求一个大数N^N的值的最右边的数字,即最低位数字. 简单二分法求解就可以了. 不过注意会溢出,只要把N % 10之后,就不会溢出了,不用使用long long. #include <stdio.h> int rightMost(int n, int N) { if (n == 0) return 1; int t = rightMost(n / 2, N); t = t * t % 10;; if (n % 2) t *= N; return t % 10; } int main() { in

HDU 1061 [Rightmost Digit] 数学方法

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1061 题目大意:求N^N的个位数 关键思想:对1个N来说,乘方时末尾会循环.对所有N来说,结果以20为周期. 代码如下(第一个思想): //cnt为循环节长度 #include <iostream> #include <cmath> using namespace std; int main(){ int T; long long i,N,cnt; cin>>T; whil

HDU 1061 N^N (n的n次方的最后一位)

题目意思: http://acm.hdu.edu.cn/showproblem.php?pid=1061 求N^N的最后一位数. 题目分析: 此题有非常多种方法,主要是中循环节,看自己怎么找了.我的方法是找到全部个位数(0-9)数的循环节,详见代码. AC代码: /** *全部数的循环节是12 */ #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> using na

hdu 1061打表

背景:某水题.第一次感觉自己写的代码最优化了,毕竟hdu上0ms 0k. 学习:1.一看n可达1e10,显然不可硬来,乘法个位数,只和每次相乘的个位数有关,故直接把0~9打表. #include<stdio.h> //¶Ô0~9£¬Ã¿¸öÊý×ֵĿÉÄܽá¹ûд³ö£¬µÚһλ±íʾӵÓнá¹ûÖÖÊý¡£ int str[10][5]={{1,0},{1,1},{4,2,4,8,6,},{4,3,9,7,1},{2,4,6},{1,5},{1,6},{4,7,9,3,1}

HDU 1061.Rightmost Digit【数论及方法】【8月30】

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 ca

HDU 1061 Rightmost Digit

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 ca

HDU 1061 EASY

//回宿舍去了,明天做点难一点的题,今天做的都很水,感觉.没意思.#include <iostream> #include <cstdio> using namespace std; const __int64 MOD=10; __int64 Power(__int64 a,__int64 b,__int64 m){ a%=m; __int64 ans=1; while(b){ if(b&1){ ans=(ans*a)%MOD; } b=b>>1; a=(a*a

HDU 1061 Rightmost Digit(找规律)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 43732    Accepted Submission(s): 16434 Problem Description Given a positive integer N, you should output the most right digit of

hdu 1061 快速幂

求n^n的个位 Sample Input 2 3 4 Sample Output 7 6 直接快速幂了,注意要用long long 1 #include<cstdio> 2 long long quick_mod(long long a,long long b,long long m) { 3 long long ans = 1; 4 while (b) { 5 if (b&1) { 6 ans = (ans * a) % m; 7 b--; 8 } 9 b/=2; 10 a = a

HDU - 1061 - Rightmost Digit (快速幂取模!)

Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34329    Accepted Submission(s): 13091 Problem Description Given a positive integer N, you should output the most right digit of