计蒜客 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
#include
#include
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;
}

你可能感兴趣的:(数学)