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

题意:

将n个糖果依次分给n个孩子,每次最少分1个糖果,分完即止,请问共有多少种分法。

思路:

通过找打表找规律可知,答案为2n-1,但是n过大,直接计算会超时。

根据费马小定理可知:an%mod = an%(mod-1)%mod【ps:mod为质数】

所以可以推导出公式:2n-1%mod = an%(mod-1)-1%mod

#include<cstdio>
#define MAX 100005
#define mod 1000000007
using namespace std;
typedef long long ll;
char st[MAX];
ll qpm(ll x,ll p)
{
ll ans=;
while(p)
{
if(p&)
ans=ans*x%mod;
p>>=;
x=x*x%mod;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll p=;
int i=,j;
scanf("%s",st);
while(st[i]!='\0')
{
p*=;
p+=(st[i++]-'');
p%=(mod-);
}
p=p-;
printf("%lld\n",qpm(,p));
}
return ;
}

【2018 ICPC焦作网络赛 G】Give Candies(费马小定理+快速幂取模)的更多相关文章

  1. 【费马小定理+快速幂取模】ACM-ICPC 2018 焦作赛区网络预赛 G. Give Candies

    G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To make the pro ...

  2. 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat

    题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...

  3. 2018 焦作网络赛 G Give Candies ( 欧拉降幂 )

    题目链接 题意 : 给出 N 个糖果.老师按顺序给 1~N 编号的学生分配糖果.每个学生要么不分.要么最少分一个.且由于是按顺序发放.那么对于某个有分到糖果的编号为 i 的学生.则 1~(i-1) 这 ...

  4. 【2018 ICPC焦作网络赛 K】Transport Ship(多重背包二进制优化)

    There are N different kinds of transport ships on the port. The ith kind of ship can carry the weigh ...

  5. 2018 ICPC 沈阳网络赛

    2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...

  6. 2018 ICPC 徐州网络赛

    2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...

  7. hdu6440 Dream 2018CCPC网络赛C 费马小定理+构造

    题目传送门 题目大意: 给定一个素数p,让你重载加法运算和乘法运算,使(m+n)p=mp+np,并且 存在一个小于p的q,使集合{qk|0<k<p,k∈Z} 等于集合{k|0<k&l ...

  8. The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元

    题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...

  9. ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)

    There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...

随机推荐

  1. hdu 3642 覆盖3次以上体积

    http://www.cnblogs.com/kane0526/archive/2013/03/06/2947118.html 题目大意:给你n个立方体,求相交区域大于等于三次的体积和. 这题需要前面 ...

  2. shiro 配置导图

    1 web.xml配置:shiro filter必须放在其他filter之前 <filter> <filter-name>shiroFilter</filter-name ...

  3. PAT 1056 Mice and Rice

    #include <cstdio> #include <climits> #include <cstdlib> #include <vector> #i ...

  4. SharePoint 2013 - Upgrade

    1. 升级到SP2013时,需要对data connection文件(UDCX文件)进行修改: 1. Mark all UDCX File (Ctrl + A) and open them. 2. F ...

  5. Weka:call for the EM algorithm to achieve clustering.(EM算法)

    EM算法: 在Eclipse中写出读取文件的代码然后调用EM算法计算输出结果: package EMAlg; import java.io.*; import weka.core.*; import ...

  6. Daemon 自更新

    NSTask *task = [[NSTask alloc] init]; [task setLaunchPath:@"usr/bin/dpkg"]; [task setArgum ...

  7. python 函数,内置函数

    1.函数 1.1 定义函数 ·函数代码块以 def 关键词开头,后接函数标识符名称和圆括号 (). ·任何传入参数和自变量必须放在圆括号中间,圆括号之间可以用于定义参数. ·函数的第一行语句可以选择性 ...

  8. 开发者为何对Service Fabric爱不释手?值得关注!

    有了它,人人都可开发高可用高伸缩应用.今天小编就为大家介绍一款开发者的“利器”——Service Fabric . 在介绍它之前,先来了解一下它的背景. Service Fabric 是一款应用程序平 ...

  9. linux下使用第三方商店安装应用

    安装 snap store 进行下载,相当与第三方应用商店,但是往往比某一个官方软件源里面的应用要丰富或更实用 到 snap docs 中选择你的 linux 版本进入安装文档,根据指示一步一步安装即 ...

  10. React学习笔记 - 元素渲染

    React Learn Note 3 React学习笔记(三) 标签(空格分隔): React JavaScript 二.元素渲染 元素是构成react应用的最小单位. 元素是普通的对象. 元素是构成 ...