2018焦作网络赛 G.Give Candies (找规律+十进制快速幂)

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≤10100000

Output

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

样例输入

1
4

样例输出

8

题目来源

ACM-ICPC 2018 焦作赛区网络预赛

题意:

n个人站成一排,开始有n颗糖果,老师从左往右走,给当前学生发糖直到发完
可以发任意颗但是至少一颗

样例解释:

4个人4个糖,8种分法:
4 0 0 0
3 1 0 0 / 1 3 0 0 (这里是2! = 2)
2 2 0 0
2 1 1 0 / 1 2 1 0 / 1 1 2 0 (这里是 3! / 2! = 3)
1 1 1 1

思路:

手算一波:
1的时候方案数:1
2的时候方案数:2
3的时候方案数:4
4的时候方案数:8
5的时候方案数:16

规律:2n-1

由于题目给的n很大,我用了十进制倍增快速幂直接算。

ps:
看了一下别人的计算方法:
费马小定理:an%mod,如果mod为质数,则等于a(n%(mod-1))%mod

pps:
补一下推导:
显然答案是把n个1分成k堆的方案数
隔板法,n个1,n-1个空位,插入k(0<=k<=n-1)个隔板将n个1分为k+1份。
0个隔板则拆成1份,方案数是C(n-1,0)
1个隔板则拆成2份,方案数是C(n-1,1)

n-1个隔板则拆成n份,方案数是C(n-1,n-1)
总方案数即为上面全部的和,由二项式定理得
C(n-1,0)+C(n-1,1)+…+C(n-1,n-1)=2n-1.

code:
#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
using namespace std;
#define int long long
const int maxm=1e5+5;
const int mod=1e9+7;
char s[maxm];
int ppow(int a,int b){
    int ans=1;
    while(b){
        if(b&1){
            ans=ans*a%mod;
        }
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
signed main(){
    int T;
    cin>>T;
    while(T--){
        scanf("%s",s);
        int len=strlen(s);
        for(int i=len-1;i>=0;i--){//n要-1
            if(s[i]=='0'){//如果是0则还需要借位
                s[i]='9';
            }else{//不为0就减1退出
                s[i]--;
                break;
            }
        }
        int ans=1;
        int a=2;
        for(int i=len-1;i>=0;i--){//十进制倍增快速幂
            int t=s[i]-'0';
            ans=ans*ppow(a,t)%mod;
            a=ppow(a,10);
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值