hdu1452整数因子和与模平方计算方法

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int GetRemainder(int baseNum, int power, int modelNum)
{
//判断各个值正确性
baseNum %= modelNum;
power %= (modelNum - 1);//小费马定理
int tempNum = 1;
int remainder = 0;
while (power >= 1)
{
//当为1的时候,得出结果
if (power == 1)
{
remainder = (tempNum * baseNum) % modelNum;
break;
}
else
{
//如果指数是偶数,将基数平方,指数除以2
if ((power&1)==0)
{
baseNum = (baseNum * baseNum) % modelNum;
power>>=1;
}
//如果是奇数,将tempNum乘以基数 并取模,指数减1
else
{
tempNum = (tempNum*baseNum)%modelNum;
power -= 1;
}
}
}
return remainder;
}
long ppp[3];
int main()
{
int g[3] = {2,3,167};
int op[3] = {1,15,18};
int ans,y;
long x;
while(scanf("%ld",&x) != EOF )
{
ppp[0] = 2 * x;
ppp[1] = 1 * x;
ppp[2] = 1 * x;
ans = 1;
y = GetRemainder(2,ppp[0]+1,29);
y = y - 1;
y = y * op[0];
ans *= y;
y = GetRemainder(3,ppp[1]+1,29);
y = y - 1;
y = (y * op[1]) % 29;
ans *= y;
y = GetRemainder(167,ppp[2]+1,29);
y = y - 1;
y = (y * op[2]) % 29;
ans *= y;
ans = ans % 29;
printf("%d\n",ans);
}
return 0;
}

 

你可能感兴趣的:(include,power,计算方法)