思路类似快速幂
+乘法分配律 3*5=3*101(2)=3*(2^2)+3*(2^0)
#include <iostream>
using namespace std;
int main ()
{
int a,b;
cin>>a>>b;
int res=0;
while(b)
{
if(b%2==1)res=res+a;
a=a+a;
b/=2;
}
cout<<res;
return 0;
}
取模: (a+b)%k=a%k+b%k (加法)
乘法,减法也满足取模运算分配律;
#include <iostream>
using namespace std;
int k=7;
int main ()
{
int a,b;
cin>>a>>b;
int res=0;
while(b)
{
if(b%2==1)res=(res+a)%k;
a=(a+a)%k;
b/=2;
}
cout<<res;
return 0;
}