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. NOIP2012-------跳石头(C语言)

    #include<stdio.h> ]; int check(long mid, long n, long m) { long last, i, ans; last = a[]; ans ...

  2. Java源码系列1——ArrayList

    本文简单介绍了 ArrayList,并对扩容,添加,删除操作的源代码做分析.能力有限,欢迎指正. ArrayList是什么? ArrayList 就是数组列表,主要用来装载数据.底层实现是数组 Obj ...

  3. 珠峰-6-node

    1. js主线程是单线程的. 2. path.resolve 传('/')解析出一个绝对路径.

  4. sublime text3 安装详解+前端插件

    1,下载sublime 3,地址:http://www.sublimetext.com/ 2,注册码:(在网上找的,感谢前辈)打开sublime3, help----add license---复制下 ...

  5. Mumbai:1 Vulnhub Walkthrough

    靶机地址: https://www.vulnhub.com/entry/mumbai-1,372/ 主机探测: 主机端口扫描: FTP 下载Note文件 TODO: Move these multip ...

  6. pikachu-SQL注入漏洞

    一.SQL Inject 漏洞原理概述 1.1 什么是数据库注入漏洞     数据库注入漏洞,主要是开发人员在构建代码的时候,没有对用户输入的值的边界进行安全的考虑,导致攻击者可以通过合法的输入点提交 ...

  7. 解决visual studio 2013编译过程中存在的无法打开kernel.lib问题

    1. 出现此类问题的原因 由于原visual studio文件中的安装中出现问题,所以原有的SDK(soft development kits)文件出现缺失: 2. 解决方法1 重新下载SDK工具,安 ...

  8. java的jdbc连接数据库,读取表中数据

    连接数据库操作步骤如下 架包 jar 建表 源码解析 1:架包 jar根据自己本版本选择正确jar包,本例采用maven 管理局,在pom.xml 中添加如下代码,自动下载架包 <depende ...

  9. 码云配合git入门命令总结学习

    目录 码云配合git入门命令总结学习 基本设置 基本命令总结学习 准备工作以及基本思路 基本命令 码云搭建仓库步骤 准备前工作 具体操作方法 远程仓库基本命令 标签相关命令 所有命令总结 基本命令总结 ...

  10. 有关配置网站时安装ZipArache的方法。

    在配置网站初始化过程中,发现ZipArache需要启动,上网搜索了一番,发现安装ZipArache的步骤十分繁琐. 换一种思路,ZipArache作为PHP的拓展类,其名字首部有ZIP字样,那么可否直 ...