HDU ACM 1061 Rightmost Digit

1、每次计算只要取最后一位即可。

2、使用快速幂提高效率。

#include<iostream>
using namespace std;

int pow(int a,int n)
{
	if(n==1)
		return a;
	else if(n%2==1)
		return (a*pow(a,n-1))%10;
	else
	{
		int tmp;

		tmp=pow(a,n>>1);
		return (tmp*tmp)%10;
	}
}

int main()
{
	int T,n;

	cin>>T;
	while(T--)
	{
		cin>>n;
		cout<<pow(n%10,n)<<endl;
	}
    return 0;
}
时间: 2024-08-02 05:59:08

HDU ACM 1061 Rightmost Digit的相关文章

杭电 HDU ACM 1061 Rightmost Digit

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

hdu oj 1061 Rightmost Digit (快速幂算法)

这里首先要讲解一下快速幂算法: 快速幂取模算法 在网站上一直没有找到有关于快速幂算法的一个详细的描述和解释,这里,我给出快速幂算法的完整解释,用的是C语言,不同语言的读者只好换个位啦,毕竟读C的人较多~ 所谓的快速幂,实际上是快速幂取模的缩写,简单的说,就是快速的求一个幂式的模(余).在程序设计过程中,经常要去求一些大数对于某个数的余数,为了得到更快.计算范围更大的算法,产生了快速幂取模算法.[有读者反映在讲快速幂部分时有点含糊,所以在这里对本文进行了修改,作了更详细的补充,争取让更多的读者一目

(HDU)1061 --Rightmost Digit( 最右边的数字)

题目链接:http://vjudge.net/problem/HDU-1061 这个题目要求出N个N相乘的个位,直接求结果肯定数据溢出. 其实只要每次得出一个数字保留个位和N相乘就可以了, 因为A*B=C,对于个位而言,A(个位)*B(个位)=C(个位)始终成立. 1<=N<=1,000,000,000,这样写还是TLE了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 us

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

HDOJ 1061 Rightmost Digit

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

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 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 - 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