快速幂取模

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 t
t%ca%c;
}
}
代码(非递归)
ll quickmod(ll a,ll b,ll c)
{
int ret=1;
a=a%c;
while(b)
{
if(b&1)/判断b是否为奇数/
ret=ret
a%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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值