计蒜客 Give Candies (2的高次幂取模)

【题目链接】
https://nanti.jisuanke.com/t/31716

题目意思

大数输入,求2的n-1次方

解题思路

2的n次方对mod取模会产生mod/2的循环,所以在大数数组转换成LL的时候取模要用mod/2,如果直接用mod在1e9+8时候出错。不理解的可以把mod取小点输出。

代码部分

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define LL long long

const int mod = 1e9 + 7;

char str[111111];

LL quick_mod(LL k)
{
    LL ans = 1;
    LL res = 2;
    while(k)
    {
        if(k&1) ans = (ans*res)%mod;
        res = (res*res)%mod;
        k >>= 1;
    }
    return ans;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str);
        int len = strlen(str);
        LL k = 0;
        for(int i = 0;i< len;i++)
            k = (k*10 + str[i] - '0')%500000003;
        k = (k -1 + 500000003)%500000003;
        printf("%lld\n",quick_mod(k));
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值