【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 ...
随机推荐
- java几种常见的排序算法总结
/*************几种常见的排序算法总结***************************/ package paixu; public class PaiXu { final int ...
- IO流之缓冲流
缓冲流 Java中提高了一套缓冲流,它的存在,可提高IO流的读写速度 缓冲流,根据流的分类字节缓冲流与字符缓冲流. 字节缓冲流 字节缓冲流根据流的方向,共有2个 l 写入数据到流中,字节缓冲输出流 ...
- HTML标签 链接 随笔3
4-1 <a>标签 网页链接 使用<a>标签可实现超链接,它在网页制作中可以说是无处不在,只要有链接的地方,就会有这个标签. 语法: <a href="目标网 ...
- less自动编译 VScode 开发工具配置
1.首先在vscode商店下载EasyLess插件,安装 2.在VS Code项目中,有一个.vscode的文件夹,找里面的settings.json文件(或者在文件-首选项-设置-搜索setting ...
- 闭包中的this
var name="pushline";//全局变量 var obj=new Object(); obj.name="jms"; obj.getName=fun ...
- C++ 输出菱形
输出*号组成的菱形: // print.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...
- SpringBoot-mvn插件
在maven projects中有一组springboot的插件 六个选择: 0.build-info:Generate a build-info.properties file based the ...
- Siebel应用数据结构层次
在Siebel应用里数据在多个层次上使用了不同的定义方式,每一个层次侧重于数据的不同的特征,主要分为数据用户界面层定义(UI),业务逻辑层定义(Business Layer,可以是业务含义层)以及数据 ...
- when create a table,then show error ora-00952 tablespace tsb_1 not exist
QUESTION:When create a table,then show error ora-00952 tablespace tsb_1 not exist. STEP: 1.select us ...
- May 30th 2017 Week 22nd Tuesday
Knowledge will give you power, but character respect. 知识给你力量,品格给你别人的尊敬. Good characters can help us ...