【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 ...
随机推荐
- 【学习笔记】String进阶:StringBuffer类(线程安全)和StringBuilder类
一.除了使用String类存储字符串之外,还可以使用StringBuffer类存储字符串.而且它是比String类更高效的存储字符串的一种引用数据类型. 优点: 对字符串进行连接操作时,使用Strin ...
- Android4.4源码学习笔记
1.StatusBar和Recents是如何建立联系的 在BaseStatusBar的start()函数通过getComponent(RecentsComponent.class)得到了Recents ...
- typeScript入门(一)构建环境和数据类型
最近入坑v-cli 3.0,发现ts越来越常用了,于是开始入坑学习. 1.构建ts环境 npm install -g typescript Mac和vscode用户可以用以下方式构建tsdemo项目 ...
- Topcoder SRM 698 Div1 250 RepeatString(dp)
题意 [题目链接]这怎么发链接啊..... Sol 枚举一个断点,然后类似于LIS一样dp一波 这个边界条件有点迷啊..fst了两遍... #include<bits/stdc++.h> ...
- 在C#中生成GUID的方法
var guid = Guid.NewGuid();Debug.WriteLine(guid.ToString()); //1f3c6041-c68f-4ab3-ae19-f66f541e3209 ...
- 节点nodeName与nodeValue表
- mfc自动创建按钮消息处理并清除
以前参加一次面试有这道题,当时没有网络没有做出来,今天在网上整理了一下,实现如下. .h中增加下面代码 //生成的消息映射函数 virtual BOOL OnInitDialo ...
- SharePoint - JavaScript Variable & Functions
1. MSOWebPartPageFormName 获取当前form的名称,然后可用document.forms[MSOWebPartPageFormName]来得到当前form: 2. _spPag ...
- 在Android Studio上进行OpenCV 3.1开发环境配置
开发环境: Windows 7 x 64 家庭版 Android Studio 1.5.1(Gradle版本2.8) JDK 1.8.0 Android 6.0(API 23) OpenCV 3.1. ...
- Entity Framework:“无法加载指定的元数据资源
System.Data.Entity.Core.MetadataException:“无法加载指定的元数据资源 CodeFirst方式使用EF,写入数据时报错.System.Data.Entity.C ...