1.基本性质
性质1:(a+b)%m=(a%m+b%m)%m
性质2:(ab)%m=(a%mb%m)%m
2.代码(递归)
ll quickmod(ll a,ll b,ll c)
{
if(b==1)
return a;
if(!(b&1))
{
ll t=quickmod(a,b/2,c);
return tt%c;
}
else
{
ll t=quickmod(a,b/2,c);
return tt%ca%c;
}
}
代码(非递归)
ll quickmod(ll a,ll b,ll c)
{
int ret=1;
a=a%c;
while(b)
{
if(b&1)/判断b是否为奇数/
ret=reta%c;
a=a*a%c;
b/=2;
}
return ret;
}
再经多次循环可得到非递归公式。
题目
problem A:快速幂取模
nefu 601
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int res;
long long c,a,b;
while(scanf("%lld%lld%lld",&a,&b,&c)!=EOF)
{res=1;a=a%c;
while(b)
{
if(b&1)
res=(res*a)%c;
b=b>>1;
a=(a*a)%c;
}
printf("%d\n",res);
}
return 0;
}
problemB:库特的数学题
nefu 1666
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long m=1e9+7;
long long mode(long long a,long long b)
{
long long res=1;
a=a%m;
while(b)
{
if(b&1)
res=(res*a)%m;
b=b>>1;
a=(a*a)%m;
}
return res;
}
int main()
{
long long n,s;
scanf("%lld",&n);
s=mode(3,n);
s=(2*s)%m;
cout<<s<<endl;
return 0;
}
problem C:异或方程解的个数
nedu1834
#include <iostream>
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int pow(int a,int b)
{
int res=1;
while(b)
{
if(b&1)res=res*a;
b=b/2;
a=a*a;
}
return res;
}
int main()
{
int n,flag;
while(scanf("%d",&n)!=EOF)
{
flag=0;
while(n)
{
if(n&1)
flag++;
n=n/2;
}
printf("%d\n",pow(2,flag));
}
return 0;
}