今天下午参加了华东交大邀请我们学校的acm友谊网络赛。题目还可以,但是上面支持的编译器是G++,GCC而不是VC的编译器。虽然差别看似不大,但是提交总出问题(如不能用float型数据,只能用double型的。做了一下午也就做出了三个(共十三个个),对比了一下别人的差距真实岗岗的,最高的做了10个真是牛人啊。这里给大家分享一个题,因为这个题想了好久都想不出来有什么好方法,希望各位高手指点:
- A^B
- Time Limit:1000MS Memory Limit:65536K
- Total Submit:208 Accepted:50
- Description
- A^B means the Bth power of A, We know that 2^4=16 4^3=64, now your task is to calulate A^B ,but Zhou sir only wants to konw the last three digits of the answer .
- Input
- The input will contain one or more lines. Each test case consists of two integers A and B(1<=A<=10000,B<=2000000000),The last test case is followed by two zeroes
- Note that B is so large!
- Output
- print the last three digits of A^B
- Sample Input
- 2 3
- 12 6
- 6789 10000
- 6789 1234567890
- 0 0
- Sample Output
- 8
- 984
- 1
- 401
- Source
- zhousc
我做了一下,一下是我的代码:
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- while(1)
- {
- int a,b,i,sum=1;
- scanf("%d %d",&a,&b);
- if(a==0&&b==0) break;
- a=a%1000;
- if(b%10000!=0)
- {
- b=b%10000;
- }
- for(i=0;i<b;i++)
- {
- sum*=a;
- sum=sum%1000;
- }
- printf("%d/n",sum);
- }
- return 0;
- }
我的代码交上去后不能通过,原因是超时了。再结合题目推测超时是因为b的值太大了。那么现在的问题就是如何找到一个与b等价的b',这个b'不能太大而且效果要与b的效果一样。有哪位高手能够给个方法么,谢谢!
原题链接:http://acm.ecjtu.jx.cn:8080/JudgeOnline/showproblem?problem_id=1162