nyoj473 A^B Problem (快速幂)

A^B Problem

时间限制:1000 ms  |  内存限制:65535 KB

难度:2

描述
Give you two numbers a and b,how to know the a^b‘s the last digit number.It looks so easy,but everybody is too lazy to slove this problem,so they remit to you who is wise.

输入
There are mutiple test cases. Each test cases consists of two numbers a and b(0<=a,b<2^30)
输出
For each test case, you should output the a^b‘s last digit number.
样例输入
7 66
8 800
样例输出
9
6
提示
There is no such case in which a = 0 && b = 0。
来源
hdu
上传者
ACM_丁国强

#include <stdio.h>
int main()
{
	int a,b,_a,s;
	while(scanf("%d %d",&a,&b)!=EOF)
	{
		if(a==0&&b==0)
		break;
		s=1;
		while(b)
		{
			if(s>=10)
			s=s%10;
			if(a>=10)
			a=a%10;
			if(b%2==1)
			s=s*a;
			a=a*a;
			b=b/2;
		}
		if(s>=10)
		s=s%10;
		printf("%d\n",s);
	}
	return 0;
}                

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

时间: 2024-10-22 10:50:23

nyoj473 A^B Problem (快速幂)的相关文章

Water Problem(快速幂)

题目大意:原题链接 这几天的题有时间再总结...... #include<cstdio> #include<cstring> typedef long long ll; const ll mod=1e9+7; long long f[10]; int temp[4]={0,1,0,-1}; struct Mat { ll mat[4][4]; }res; Mat Mult(Mat a,Mat b) { Mat c; memset(c.mat,0,sizeof(c.mat)); fo

dutacm.club Water Problem(矩阵快速幂)

Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:1228   Accepted:121 [Submit][Status][Discuss] Description 函数 f:Z+→Z .已知 f(1),f(2) 的值,且对于任意 x>1,有 f(x+1)=f(x)+f(x?1)+sin(πx2) . 求 f(n) 的

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

fzu Problem 2198 快来快来数一数 (快速幂+优化)

题目链接: Problem  2198  快来快来数一数 题目描述: 给出n个六边形排成一排,a[i]代表i个六边形能组成的生成树个数,设定s[i]等于a[1]+a[2]+a[3]+....+a[i-1]+a[i],问s[n]为多少? 解题思路: n取值范围[1, 1018],打表内存不够,然后就要考虑快速幂咯!纳尼!!!!快速幂写出来竟然超时,敢信?果然还是见题太少了.(GG) 对于a[n] = 6*a[n-1] - a[n-2],可以很明显看出. 然后求和的时候就要化简一番了,但是并不是很难

LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

题目链接:LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 得到递推式之后就是矩阵快速幂了 注意:模2^64,定义成unsigned long long 类型,因为无符号类型超过最大范围的数与该数%最大范围 的效果是一样的. AC代码: #include<stdio.h> #include<string.h> #define LL unsigned long long struct Ma

BestCoder Round #29——A--GTY&#39;s math problem(快速幂(对数法))、B--GTY&#39;s birthday gift(矩阵快速幂)

GTY's math problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description GTY is a GodBull who will get an Au in NOI . To have more time to learn alg

HDU 2256 Problem of Precision (矩阵快速幂)

HDU 2256 Problem of Precision (矩阵快速幂) ACM 题目地址:HDU 2256 Problem of Precision 题意: 给出一个式子,求值. 分析: 推起来最后那步会比较难想. 具体过程见: 表示共轭只听说过复数的和图的... 这构题痕迹好明显... 跟基友开玩笑说:如果遇到这种题,推到Xn+Yn*sqrt(6)这步时,打表最多只能打到10就爆int了,这是输出正解和Xn,说不定眼神好能发现ans = Xn * 2 - 1呢.= =... 代码: /*

HDU 5685 Problem A | 快速幂+逆元

Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 463    Accepted Submission(s): 162 Problem Description 度熊手上有一本字典存储了大量的单词,有一次,他把所有单词组成了一个很长很长的字符串.现在麻烦来了,他忘记了原来的字符串都是什么,神奇的是他竟然记得原来那些字符串的

hdu1757 A Simple Math Problem(矩阵快速幂)

题目: A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3522    Accepted Submission(s): 2130 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f