1. 程式人生 > >【費馬小定理+快速冪取模】ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies

【費馬小定理+快速冪取模】ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies

print using pri long long ger ssi bit one ive

G. Give Candies

There are N children in kindergarten. Miss Li bought them N candies. To make the process more interesting, Miss Li comes up with the rule: All the children line up according to their student number (1...N) and each time a child is invited, Miss Li randomly gives him some candies (at least one). The process goes on until there is no candy. Miss Li wants to know how many possible different distribution results are there.

Input

The first line contains an integer T, the number of test case.

The next T lines, each contains an integer N.

1≤T≤100

1≤N≤10^100000

Output

For each test case output the number of possible results (mod 1000000007).

樣例輸入

1
4

樣例輸出

8

有n個孩子,n個糖果,從第一個孩子開始給,任意給隨便數量的糖果,至少給一個

問你一共有多少種不同的給的方案,答案應該為2^(n-1)%1e9+7

n可以到10的100000次方,所以應當用降冪公式

費馬小定理 (a^n)%mod,如果mod為質數,a^(n%(mod-1))%mod

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll quickpow(ll a,ll b)
{
    ll r=1;
    while(b)
    {
        if(b&1) r=r*a%mod;
        a=a*a%mod;
        b/=2;
    }
    return r;
}
char s[100005];
 
int main()
{
    int T;
    scanf(
"%d",&T); while(T--) { scanf("%s",s); int len=strlen(s); ll temp=0; for(int i=0;i<len;i++) { temp=(temp*10+s[i]-0)%(mod-1); } temp--; if(temp<0) temp=mod-1; printf("%lld\n",quickpow(1LL*2,temp)); } return 0; }

轉載來自大佬 cherry:

https://blog.csdn.net/qq_41037114/article/details/82716896

【費馬小定理+快速冪取模】ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies