2018焦作网络赛G题(规律+费马小定理)

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,直到糖分发完为止,问有多少种方案(这个游戏一点也不好玩(╯‵□′)╯︵┻━┻,这啥辣鸡老师,每个孩纸一颗糖不然他们会哭的)

思路:首先方案有2^{n-1}种,直接快速指数幂的话logn的时间复杂度也不OK,这个时候就要用到费马小定理了

a^{p-1} \equiv 1\ mod p

a^{n-1}=a^{k*(p-1)+t} =a^{k*(p-1)}*a^t\equiv\ a^t\ modp

所以只要获得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;

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值