1. 程式人生 > >@ACM-ICPC 2018 焦作賽區網路預賽 : G: Give Candies (大數模)

@ACM-ICPC 2018 焦作賽區網路預賽 : G: Give Candies (大數模)

求 ​​​​​​​ ​​​​​​​2^(n-1) % mod2^{n-1}%mod ;  

劉汝佳紫書上.

程式碼:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll mod=1e9+7;
ll quc(ll a,ll b)
{
    ll res=1;
    while (b)
    {
        if (b&1)
            res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
int main()
{
    string str;
    int t;
    scanf("%d",&t);
    while (t--)
    {
        cin>>str;
        ll sum=0;
        for (int i=0;i<str.length();i++)
            sum=(1ll*sum*10%(mod-1)+1ll*(str[i]-'0')%(mod-1))%(mod-1);
        ll res=quc(1ll*2,sum)*quc(2,mod-2)%mod;
        printf("%lld\n",res);
    }
    return 0;
}