【2018 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), 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(费马小定理+快速幂取模)的更多相关文章
- 【费马小定理+快速幂取模】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 ...
- 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 由负数取模的性质 ...
- 2018 焦作网络赛 G Give Candies ( 欧拉降幂 )
题目链接 题意 : 给出 N 个糖果.老师按顺序给 1~N 编号的学生分配糖果.每个学生要么不分.要么最少分一个.且由于是按顺序发放.那么对于某个有分到糖果的编号为 i 的学生.则 1~(i-1) 这 ...
- 【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 ...
- 2018 ICPC 沈阳网络赛
2018 ICPC 沈阳网络赛 Call of Accepted 题目描述:求一个算式的最大值与最小值. solution 按普通算式计算方法做,只不过要同时记住最大值和最小值而已. Convex H ...
- 2018 ICPC 徐州网络赛
2018 ICPC 徐州网络赛 A. Hard to prepare 题目描述:\(n\)个数围成一个环,每个数是\(0\)~\(2^k-1\),相邻两个数的同或值不为零,问方案数. solution ...
- hdu6440 Dream 2018CCPC网络赛C 费马小定理+构造
题目传送门 题目大意: 给定一个素数p,让你重载加法运算和乘法运算,使(m+n)p=mp+np,并且 存在一个小于p的q,使集合{qk|0<k<p,k∈Z} 等于集合{k|0<k&l ...
- The 2018 ACM-ICPC China JiangSu Provincial Programming Contest快速幂取模及求逆元
题目来源 The 2018 ACM-ICPC China JiangSu Provincial Programming Contest 35.4% 1000ms 65536K Persona5 Per ...
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
随机推荐
- springboot--数据库操作
1.注意: 使用get,post提交时,使用form-data; 使用put提交方式,使用x-www-form-url-encoded,这是http的一种格式;
- JS多级树结构写法
效果: 一.布局: <div class="three_tree"> <div class="tree_title_cut"> < ...
- js匿名函数使用&传参
function () { alert("error"); } //报错:匿名函数不能直接调用 使用 0x01 (function () { alert("Success ...
- ppt写作的注意事项
PPT推荐字体及大小: 宋体严谨,适合正文,显示最清晰 黑体庄重,适合标题,或者强调区 隶书楷体,艺术性强,不适合投影 如果通过文字排版突出重点:加粗.加大字号.变色 PPT文字太多怎么办? 1.抽象 ...
- GIS中的坐标系定义与转换
GIS中的坐标系定义与转换 青岛海洋地质研究所 戴勤奋 2002-3-27 14:22:47 ----------------------------------------------------- ...
- Android应用开发基础之八:广播与服务(二)
服务两种启动方式 startService:服务被启动之后,跟启动它的组件没有一毛钱关系 bindService:跟启动它的组件同生共死 绑定服务和解绑服务的生命周期方法:onCreate->o ...
- asyncio标准库2 Hello Clock
如何调度协程,并发运行 asyncio.gather方法可以聚合协程or future def gather(*coros_or_futures, loop=None, return_exceptio ...
- 在浏览器的市场上,IE依然是放弃了,firefox还在继续~~
自从微软失败后,发布了Edge:但是依然是没有多少市场的,毕竟各种难受: 单从开发者工具,我是没有其它选择了, ie的不能用,https://www.microsoft.com/en-us/welco ...
- WAKE-SPM-综述
1,SPM 1,1source paper:http://lear.inrialpes.fr/pubs/2007/ZMLS07/ZhangMarszalekLazebnikSchmid-IJCV07- ...
- python入门12 列表list
列表使用率较高,方法也多. 列表的定义 #coding:utf-8 #/usr/bin/python """ 2018-11-10 dinghanhua 列表 " ...