Give Candies
1000ms
65536K
There are NN children in kindergarten. Miss Li bought them NN 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)(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 TT, the number of test case.
The next TT lines, each contains an integer NN.
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个学生,她想到一个好玩的优秀:学生按照学号排序,每个学生随机获得x颗糖,x>0,直到糖分发完为止,问有多少种方案(这个游戏一点也不好玩(╯‵□′)╯︵┻━┻,这啥辣鸡老师,每个孩纸一颗糖不然他们会哭的)
思路:首先方案有种,直接快速指数幂的话logn的时间复杂度也不OK,这个时候就要用到费马小定理了
所以只要获得t=(n-1)% (p-1)的值再快速幂就行辽
然后由于n太大了,可以用字符串的方式进行处理
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
typedef long long LL;
const int mod=1e9+7;
string s;
LL quick_mod(LL a,LL p)
{
LL ans=1;
while(p)
{
if(p&1)
ans=ans*a%mod;
a=a*a%mod;
p=p>>1;
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>s;
LL k=0;
int l=s.length();
for(int i=0;i<l;++i)
k=(k*10+s[i]-'0')%(mod-1);
// k=k-1;
k=(k-1+mod-1)%(mod-1);
cout<<quick_mod(2,k)<<endl;
}
}