杭电 HDU 1097 A hard puzzle

A hard puzzle

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

Total Submission(s): 32851    Accepted Submission(s): 11754

Problem Description

lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius:  gave a and b,how to know the a^b.everybody objects to this BT problem,so lcy makes the problem easier than begin.

this puzzle describes that: gave a and b,how to know the a^b‘s the last digit number.But everybody is too lazy to slove this problem,so they remit to you who is wise.

Input

There are mutiple test cases. Each test cases consists of two numbers a and b(0<a,b<=2^30)

Output

For each test case, you should output the a^b‘s  last digit number.

Sample Input

7 66
8 800

Sample Output

9
6

Author

eddy

一开始常规方法,时间超时。之后~

我也不知道 啥原理 他妈的一下子就水过去了 靠!服了,只不过在求快速幂的时候加了个%=10而已 原理以后深究

#include<iostream>
using namespace std;

int quickmi(int a,int b)
{
	int r=1,base=a;
	while(b)
	{
	if(b&1)
	{
		r*=base;
		r%=10;
	}
	base%=10;base*=base;
	b>>=1;
	}
	return r;
}
int main()
{
	int a,b;
	while(cin>>a>>b)
	{
		cout<<quickmi(a,b)<<endl;
	}
	return 0;
}

网上搜罗了一下 有个更好理解的方法:每个底数的数字的n次方都是四位循环数字 4*n+1幂的尾数是底数的1次幂的尾数,注意可能出现幂能整除4的情况;

#include<iostream>
using namespace std;
int main()

{
	int a,b;
	while(cin>>a>>b)
	{
		a%=10;
		int ls=a*a*a*a;//防止b==0,先初始化ls
		b%=4;
		for(int i=0;i<b;i++)//如果b本身就是小于4的,此时的幂b乘以a的四次方(ls)后相当于不乘
		{
			ls*=a;
			ls%=10;
		}
		cout<<ls%10<<endl;
	}
	return 0;
}

时间: 2024-12-16 14:32:39

杭电 HDU 1097 A hard puzzle的相关文章

杭电 HDU 1098 Ignatius&#39;s puzzle

Ignatius's puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7068    Accepted Submission(s): 4883 Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

杭电 HDU 1038 Biker&#39;s Trip Odometer

Biker's Trip Odometer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4745    Accepted Submission(s): 3144 Problem Description Most bicycle speedometers work by using a Hall Effect sensor faste

[2016-02-05][HDU][1097][A hard puzzle]

[2016-02-05][HDU][1097][A hard puzzle] HDU - 1097 A hard puzzle Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a and b,how to know

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

杭电hdu 4861 Couple doubi

杭电 2014多校联训第一场   1001   Couple doubi   逗比夫妇 这标题我就不多说什么了. 题意:有K个球在桌上,每个球都有价值,第i个球的价值是1^i+2^i+...+(p-1)^i (mod p).其中p是一个素数,之后逗比男先选球,最后所有球总分高的获胜.如果逗比男获胜,那么输出“YES”否则输出“NO”.(逗比男和逗比女都采取最有策略). 当然这也p是奇素数的一个重要公式.曾有题是这个公式求和.当然如果你知道就很简单了.如果不知道,就打表找规律吧. 根据这一重要的公

杭电 HDU 1037 Keep on Truckin&#39;

Keep on Truckin' Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9881    Accepted Submission(s): 6859 Problem Description Boudreaux and Thibodeaux are on the road again . . . "Boudreaux, we hav

杭电 HDU ACM 1397 Goldbach&#39;s Conjecture

Goldbach's Conjecture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4976    Accepted Submission(s): 1901 Problem Description Goldbach's Conjecture: For any even number n greater than or equal

杭电 HDU ACM 5186 zhx&#39;s submissions

zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1892    Accepted Submission(s): 507 Problem Description As one of the most powerful brushes, zhx submits a lot of code on many