zoukankan      html  css  js  c++  java
  • ACM-ICPC 2018 焦作赛区网络预赛 G Give Candies

    There are NNN children in kindergarten. Miss Li bought them NNN 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)(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 TTT, the number of test case.

    The next TTT lines, each contains an integer NNN.

    1≤T≤1001 le T le 1001T100

    1≤N≤101000001 le N le 10^{100000}1N10100000

    Output

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

    样例输入

    1
    4

    样例输出

    8


    题意:
    给n个小孩发糖果,总共有n个糖果,小孩按顺序排好,把糖果分发,不必每个小孩都有糖果,但是如果不是每个孩子都有糖果,那么只能是在后面的小孩没有糖果。问有多少种方案。

    题解:
    找到规律,对于每一个n,方案数为2^n,这里应用快速幂,但是n太大了,想办法减小它,这里要应用一下费马小定理(假如p是质数,且gcd(a,p)=1,那么 a(p-1)≡1(mod p)),即(a^n)%mod,如果mod为质数,a^(n%(mod-1))%mod。(本题中mod为1e9+7)


    代码:
    #include <bits/stdc++.h>
    
    using namespace std;
    typedef long long ll;
    const ll mod=1e9+7;
    
    ll Pow(ll a,ll b)
    {
        ll ans=1;
        a%=mod;
        while(b){
            if(b&1) ans=ans*a%mod;
            a=a*a%mod;
            b=b>>1;
        }
        return ans;
    }
    
    int main()
    {
        int t;
        ll i,n;
        char a[100100];
        cin>>t;
        while(t--){
            scanf("%s",a);
            int len=strlen(a);
            n=0;
            for(i=0;i<len;i++){
                n=(n*10+(a[i]-'0'))%(mod-1);
            }
            printf("%lld
    ",Pow(2,n-1)%mod);
        }
        return 0;
    }
  • 相关阅读:
    css2D转换和3D转换
    css过渡和动画
    Sublime Text、webstorm等编译器快速编写HTML/CSS代码的技巧
    纯CSS制作各种各样的网页图标(三角形、暂停按钮、下载箭头、加号等)
    Vue:渲染、指令、事件、组件、Props
    CSS Grid 网格布局全解析
    flex 布局的深入研究
    数组的迭代方法(every、filter、forEach、map、some)
    You-need-to-know-css
    两列布局
  • 原文地址:https://www.cnblogs.com/y1040511302/p/9656135.html
Copyright © 2011-2022 走看看