A^B%C 要用C++写 我只会C++

tsshouhu 2010-04-10 10:19:47
(A^B)%C

Time Limit:1000MS Memory Limit:65536K
Total Submit:263 Accepted:35

Description

Naruto' faith is never give up, so I hope you can persist in ACM. ACM is not a shortcut to success, there are many obstacles you should overcome, but only overcome them you can become more powerful !
First, you need to overcome English. Now this problem.


Input

The input consist of three integers A,B,C; 1<= A <=10000; 1<= B <=10^9; 1<=C<=10000; three 0 sign the end of the input.

Output

The output will be a single integer——(A^B)%C. For example,A=2,B=4,C=5, then the result is (2^4)%5 = (2*2*2*2)%5 = 1.

Sample Input


2 2 3
3 3 4
4389 99999999 1303
0 0 0


Sample Output


1
3
698
...全文
135 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinosinux 2010-04-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 arong1234 的回复:]
这种ACM之类的练习题都不自己做,搞ACM还有什么意义啊:)
而且,就这么简单的题目都不会,能说会C++么?
[/Quote]

同意
arong1234 2010-04-10
  • 打赏
  • 举报
回复
这种ACM之类的练习题都不自己做,搞ACM还有什么意义啊:)
而且,就这么简单的题目都不会,能说会C++么?
卡卡篷 2010-04-10
  • 打赏
  • 举报
回复
上面的错了,下面的经测式正确
#include <iostream>
using namespace std;

typedef long unsigned int LUINT;

LUINT remainder(LUINT a, LUINT b, LUINT c)
{
a %= c;
if (!a)
{
return 0;
}

LUINT tmp = 1;
for (LUINT i=b; i>=2; i/=2)
{
if (i%2)
{
tmp = tmp*a%c;
--i;
}
a = a*a%c;
}

return a*tmp%c;
}

int main()
{
cout<<remainder(2, 2, 3)<<endl;
cout<<remainder(3, 3, 4)<<endl;
cout<<remainder(4389, 99999999, 1303)<<endl;

return 0;
}
卡卡篷 2010-04-10
  • 打赏
  • 举报
回复
前两个对上了,第三个没对上,代码应该没有问题才对
试了很多数据,LZ看下你提供的数据和答案都没问题吗?
#include <iostream>
using namespace std:

typedef long unsigned int LUINT;

LUINT remainder(LUINT a, LUINT b, LUINT c)
{
a %= c;
bool bEven = (0==b%2);
LUINT tmp = a;
for (LUINT i=bEven?b:b-1; i>=2; i/=2)
{
a = (a*a % c);
}
return bEven? a%c : (a*tmp)%c;
}

int main()
{
cout<<remainder(2, 2, 3)<<endl;
cout<<remainder(3, 3, 4)<<endl;
cout<<remainder(4389, 99999999, 1303)<<endl;

return 0;
}

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧