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 题解的更多相关文章

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

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

  2. 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 ...

  3. ACM-ICPC 2018 焦作赛区网络预赛

    这场打得还是比较爽的,但是队友差一点就再过一题,还是难受啊. 每天都有新的难过 A. Magic Mirror Jessie has a magic mirror. Every morning she ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. ACM-ICPC 2018 焦作赛区网络预赛 G题 Give Candies

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

随机推荐

  1. light oj 1102 - Problem Makes Problem组合数学(隔板法)

    1102 - Problem Makes Problem As I am fond of making easier problems, I discovered a problem. Actuall ...

  2. 实践:使用了CompletableFuture之后,程序性能提升了三倍

    CompletableFuture 相比于jdk5所提出的future概念,future在执行的时候支持异步处理,但是在回调的过程中依旧是难免会遇到需要等待的情况. 在jdk8里面,出现了Comple ...

  3. linux的分区和文件系统

    一.分区类型 主分区 总共最多能分四个 扩展分区 只能有一个,也算做主分区的一种,也就是说主分区加扩展分区最多有4个. 但是扩展分区不能存储数据和格式化,必须在划分成逻辑分区才能使用. 逻辑分区 逻辑 ...

  4. AMD R2600+微星B450迫击炮配置的新工作机,分享给大家

    上个月,突然觉得自己总做用的电脑有点老了,虽然很不舍陪自己战斗了3,4年的老战士,下了很大的决心,才决定搞一台新的吧,虽然新电脑的配置也不算非常高,但是用于开发的话,也算不错的选择了,特此分享一下.又 ...

  5. 配置 Apache James 邮件服务器以使用加密邮件通讯协议

    可先参照: 使用 Apache James 3.3.0(开源免费) 搭建内网电子邮件服务器(基于 Windows + Amazon Corretto 8)https://www.cnblogs.com ...

  6. Android.bp文件简介

    Android.bp是用来替换Android.mk的配置文件,它使用Blueprint框架来解析.Blueprint是生成.解析Android.bp的工具,是Soong的一部分.Soong则是专为An ...

  7. redis 5.0.7 源码阅读——双向链表

    redis中双向链表相关的文件为:adlist.h与adlist.c 一.数据结构 redis里定义的双向链表,与普通双向链表大致相同 单个节点: typedef struct listNode { ...

  8. javaweb垃圾分类查询系统源码 ssm+mysql

    需求 基于SSM实现一个垃圾分类查询管理系统, 用户可以根据自定义查询分类信息, 管理员可以对分类信息, 垃圾详情信息进行增删改查的管理 运行环境 jdk1.8,tomcat8.5,mysql5.6, ...

  9. Bash脚本编程学习笔记06:条件结构体

    简介 在bash脚本编程中,条件结构体使用if语句和case语句两种句式. if语句 单分支if语句 if TEST; then CMD fi TEST:条件判断,多数情况下可使用test命令来实现, ...

  10. 用Excel排值班表用到的几个公式

    用Excel排值班表用到的几个公式 最近,疫情得到了一定的缓解,但还不能放松.所以,各单位都加强值班.那就得排值班表.提到的表当然要用Excel,为什么?因为Excel中的公式真得能让我们提高工作效率 ...