[2018 ACM-ICPC 焦作赛区网络赛] 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)(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个糖果,老师从第一个学生开始发糖果,老师所经过的学生至少要被分到一个糖果,求有多少种分法,比如这里有三个学生,老师有三个糖果,有四种分法:{3,0,0},{2,1,0},{1,2,0},{1,1,1}。思路:这个题和HDU - 5703类似,其实就是拆数问题,一个数的拆法其实就是2^(N-1),具体证明过程可以直接搜刚才杭电那道题的题解,所以这道题其实就是让你算2^(N-1),但是题目给的N特别大,可以达到10^100000,我们只能用字符串读这个数,这样的话我们肯定不能直接用快速幂算,其实有一个性质,2^N模一个质数,它的结果是具有周期性的,周期长度为mod-1,这道题就利用这个周期性,具体步骤就是:先把N转化成模mod-1下的的数,然后用这个数计算快速幂,得到的结果是和原数相同的。

#include <iostream>
using namespace std;
#define ll long long
int mod = 1000000007;
ll qmul(ll x, ll y, ll mod)   // 乘法防止溢出, 如果p * p不爆LL的话可以直接乘; O(1)乘法或者转化成二进制加法(快速加)
{
    ll ret = 0;
    while(y) {
        if(y & 1)
            ret = (ret + x) % mod;
        x = x * 2 % mod;
        y >>= 1;
    }
    return ret;
}
ll qpow(ll a, ll n, ll mod)
{
    ll ret = 1;
    while(n)
    {
        if(n & 1) ret = qmul(ret, a, mod);
        a = qmul(a, a, mod);
        n >>= 1;
    }
    return ret;
}
int main(){
    int T;
    cin>>T;
    while(T--){
        string s;
        cin>>s;
        mod-=1;
        long long ans = 1,tmp = 0;
        for(int i=0;i<s.length();i++){
            tmp = (tmp * 10 + s[i] - ‘0‘)%mod;
        }
        if(tmp==0)  //注意0要特判
            tmp=mod;
        tmp = (tmp-1+mod)%(mod);
        mod+=1;
        ans = qpow(2,tmp,mod);
        cout<<ans<<endl;
    }
}

原文地址:https://www.cnblogs.com/jiaqi666/p/9655238.html

时间: 2024-08-30 00:45:55

[2018 ACM-ICPC 焦作赛区网络赛] G - Give Candies(找规律+快速幂)的相关文章

【费马小定理+快速幂取模】ACM-ICPC 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 inv

2014 ACM/ICPC 鞍山赛区网络赛(清华命题)

为迎接10月17号清华命题的鞍山现场赛 杭电上的题目 Biconnected(hdu4997)      Rotate(hdu4998)     Overt(hdu4999)   Clone(hdu5000)   Walk(hdu5001)   LianLianKan   Rescue   Spy's Work   Color the Tree   The Ghost Blows Light   USACO ORZ   2013/8/27

ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(欧拉降幂)

题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 :所以3共有4种),n最多有1e5位,答案取模p = 1e9+7 思路:就是往n个东西中间插任意个板子,所以最多能插n - 1个,所以答案为2^(n - 1) % p.直接套用模板 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<cmath

ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies (打表找规律+快速幂)

题目链接:https://nanti.jisuanke.com/t/31716 题目大意:有n个孩子和n个糖果,现在让n个孩子排成一列,一个一个发糖果,每个孩子随机挑选x个糖果给他,x>=1,直到无糖果剩余为止.给出数字n,问有多少种分发糖果的方法. 样例输入 复制 1 4 样例输出 复制 8 解题思路:我们可以这样想,一个糖果的话,应该是只有1种方法记为x1,如果是两个糖果的话,有两种方法即为x2,分别为(1,1)和(2),从中我们可以想到如果n个糖果的话,就可以分为第n个人取1个的话就有x(

焦作网络赛L-Poor God Water【矩阵快速幂】

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonous. Every hour, God Water will eat one kind of food among meat, fish and chocolate. If there are 33 

HDU 5875 Function -2016 ICPC 大连赛区网络赛

题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了zzz.去年有一场现场赛也是n=1000,n^3过了,看来关键时刻实在做不出来就得大胆暴力啊. #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=1e5+5; int a[maxn]

2017 acm icpc 沈阳(网络赛)5/12 题解

比赛中较...能做的5道题 hdoj6195. cable cable cable 题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6195 题目大意 : 略 规律 : 答案 = k+(m-k)*k hdoj6198. number number number 题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6198 题目大意  : 给你一个整数n.问你n个斐波那契数(可重复)不能构成哪些数,输出

[2018 ACM-ICPC 焦作赛区网络赛] H - String and Times(后缀自动机)

Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring wonderful substring when the times it appears in that string is between AA and BB (A \le times \le BA≤times≤B). Can you calculate the number of wonderful

2014 ACM/ICPC 牡丹江赛区现场赛

最近突然感觉状态不佳,可能是天冷的缘故? 赛后来做牡丹江赛区的题目 [A]3819 Average Score -- 签到题 [B] [C] [D] [E] [F] [G] [H] [I] [J] [K]3829 Known Notation -- 贪心 + 模拟 [A]3819 Average Score -- 签到题 Average Score Time Limit: 2 Seconds                                     Memory Limit: 65