ACM-ICPC 2018 焦作赛区网络预赛 Give Candies 题解
ACM-ICPC 2018 焦作赛区网络预赛 Give Candies
n个糖果分给n个小朋友
从1到n个小朋友依次给,每次随机给个数,至少一个,知道没有糖果为止。
问糖果的分布情况方案数。
输出方案数mod 109+710^9+7109+7
考虑只有前i个小朋友得到糖的情况,于是等价于将n个糖果分为i堆,插板法易得方案数是(n−1i−1)\binom{n-1}{i-1}(i−1n−1)
总方案数∑i=1n(n−1i−1)=2n−1\sum_{i=1}^{n}\binom{n-1}{i-1}=2^{n-1}∑i=1n(i−1n−1)=2n−1
2n−1mod  10000000072^{n-1} \mod 10000000072n−1mod1000000007
anmod  pa^n \mod panmodp
p是质数,只是n很大
an≡anmod  ϕ(p)(modp)a^n \equiv a^{n \mod \phi(p)} \pmod{p}an≡anmodϕ(p)(modp)
依据是费马-欧拉定理
更一般的情况简记
事实上,更为一般的是:
gcd(a,c)=1⇒ab≡abmod  ϕ(c)(modc)gcd(a,c)=1 \Rightarrow a^b \equiv a^{b \mod \phi(c)} \pmod{c}gcd(a,c)=1⇒ab≡abmodϕ(c)(modc)
如果a,c不互素呢?
b>ϕ(n)⇒ab≡abmod  ϕ(c)  +  ϕ(c)(modc)b \gt \phi(n) \Rightarrow a^b \equiv a^{b \mod \phi(c)\; + \;\phi(c)} \pmod{c}b>ϕ(n)⇒ab≡abmodϕ(c)+ϕ(c)(modc)
如果b≤ϕ(n)b \leq \phi(n)b≤ϕ(n);那就不用换了。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m;
const ll mod = 1e+9+7; // is prime
const ll phi_mod = mod-1;
// pre: mod != 0, <a,n>!=<0,0> n>=0
ll mlt(ll a, ll n, ll mod) {
if (n == 0)
return 1;
ll t = 1;
a %= mod;
while (n > 1) {
if (n&1)
t = (t*a)%mod;
a = (a*a)%mod;
n >>= 1;
}
return (t*a)%mod;
}
string s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--) {
cin>>s;
n = 0;
for (auto x : s)
n = ((n*10)+x-'0')%phi_mod;
n = (n-1+phi_mod)%phi_mod;
cout<<mlt(2ll,n,mod)<<endl;
}
return 0;
}
ACM-ICPC 2018 焦作赛区网络预赛 Give Candies 题解的更多相关文章
- ACM-ICPC 2018 焦作赛区网络预赛- G:Give Candies(费马小定理,快速幂)
There are N children in kindergarten. Miss Li bought them NNN candies. To make the process more inte ...
- ACM-ICPC 2018 焦作赛区网络预赛- L:Poor God Water(BM模板/矩阵快速幂)
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 焦作赛区网络预赛
这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...
- ACM-ICPC 2018 焦作赛区网络预赛J题 Participate in E-sports
Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don't know ...
- ACM-ICPC 2018 焦作赛区网络预赛 K题 Transport Ship
There are NN different kinds of transport ships on the port. The i^{th}ith kind of ship can carry th ...
- ACM-ICPC 2018 焦作赛区网络预赛 L 题 Poor God Water
God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him t ...
- ACM-ICPC 2018 焦作赛区网络预赛 I题 Save the Room
Bob is a sorcerer. He lives in a cuboid room which has a length of AA, a width of BB and a height of ...
- ACM-ICPC 2018 焦作赛区网络预赛 H题 String and Times(SAM)
Now you have a string consists of uppercase letters, two integers AA and BB. We call a substring won ...
- ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies
There are NN children in kindergarten. Miss Li bought them NN candies. To make the process more inte ...
随机推荐
- Python字符串字母大小写变换
说明: 字符串就是一系列字符,在Python中用引号括起来的都是字符串,引号可以是单引号,也可以是双引号,比如:“This is a book.” ‘This is an apple.’ 接下来简单 ...
- v-charts x轴字体斜显示
如下图,因为X轴内容太多,放不下,插件默认间隔显示需求:X轴内容要全部显示出来(只有斜显示或固定宽多余的用省略代替,本来需要就是想显示全部内容,所以只能取斜显示的方案) 先看看v-charts的文档: ...
- AOP in .NET
AOP in .NET AOP是所有现代OOP语言开发框架中的基础功能,随着Spring框架的普及,对于AOP的使用已经像喝水一样普通.可是知其然还要其所以然.本文将基于.NET环境探讨实现AOP的底 ...
- SparkShuffle机制
在早期版本的Spark中,shuffle过程没有磁盘读写操作,是纯内存操作,后来发现效率较低,且极易引发OOME,较新版本的Shuffle操作都加入了磁盘读写进行了改进. 1.未经优化的HashShu ...
- CentOS、Ubuntu的安装
Linux使用最广泛的2个发行版:CentOS.Ubuntu. CentOS安全性高,常用作企业的服务器,Ubuntu常用作个人桌面. 常见的虚拟机有2个: VM VirtualBox,这个是Orac ...
- linux下安装lxml包
爬虫项目需要用到lxml包,解析html文件,但是linux服务器没有lxml包, 服务器中python版本是3.8. 直接使用命令安装: pip install lxml 中途会报错,错误提示我没有 ...
- instanceof读解
function instance(l,r){ let 0 = r.prototype; let v = l.__proto__; while(true){ if(v === null){ retur ...
- JAVA中级开发应该掌握的小知识点
一.悲观锁.乐观锁的区别: 悲观锁:一段执行逻辑加上悲观锁,不同线程同时执行,只有一个线程可以执行,其他线程在入口处等待,直到锁被释放.乐观锁:一段执行逻辑加上乐观锁,不同线程同时执行,可以同时进入执 ...
- 分布式任务调度XXL-JOB初体验
简介 XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用. 官方文档很完善,不多赘述.本文主要是搭建XX ...
- navicate premium连接sqlserver时报08001错误的解决方法
---恢复内容开始--- navicate premium连接sqlserver时报08001错误的解决方法 1.自己一直使用navicate连接sqlserver,但是自从自己的电脑安装了sqlse ...