64,654
社区成员
发帖
与我相关
我的任务
分享
#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;
}
#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;
}