hdu---2035 人见人爱A^B

来源:互联网 发布:和室友网络冲突怎么办 编辑:程序博客网 时间:2024/05/18 04:28

人见人爱A^B

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25745    Accepted Submission(s): 17597


Problem Description
求A^B的最后三位数表示的整数。
说明:A^B的含义是“A的B次方”
 


Input
输入数据包含多个测试实例,每个实例占一行,由两个正整数A和B组成(1<=A,B<=10000),如果A=0, B=0,则表示输入数据的结束,不做处理。
 


Output
对于每个测试实例,请输出A^B的最后三位表示的整数,每个输出占一行。
 


Sample Input
2 312 66789 100000 0
 


Sample Output
89841


简单的快速幂取模:

/*
1.取后三位: Mod 取 1000
2.快速幂: 每次对指数b判断最后一位是否为1, 之后 b /= 2;
3.a = a*a%Mod;

共三步,计算出a^b对Mod取余,最后剩余的为三位数!!

*/

#include <iostream>#include <algorithm>#include <string.h>#include <math.h>#include <stdio.h>#include <queue>using namespace std;#define N 1000010#define LL long long#define Mod 1000int main(){//freopen("in.in","r",stdin);int a,b;while(scanf("%d %d",&a,&b) && a && b){int ans=1;while(b){if(b & 1){ans = ans*a%Mod;}a = a*a%Mod;b /= 2;}printf("%d\n",ans);}return 0;}



0 0
原创粉丝点击