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. Fastdfs php扩展访问

    一.安装FastDFS client php extension compiled under PHP 5.4 and PHP 7.0   1.安装php扩展,进入fastdfs源码文件夹中的  ph ...

  2. 杭电-------2098 分拆素数和(c语言写)

    #include<stdio.h> #include<math.h> ] = { , }; ;//全局变量,用来标志此时已有多少个素数 int judge(int n) {// ...

  3. Docker Compose搭建Redis一主二从三哨兵高可用集群

    一.Docker Compose介绍 https://docs.docker.com/compose/ Docker官方的网站是这样介绍Docker Compose的: Compose是用于定义和运行 ...

  4. vue 使用v-for进行循环

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Linux如何定位文件在磁盘的物理位置

    我在学习研究Linux内核结构的时候,思考过一个问题:Linux如何定位文件在磁盘的物理位置每个文件都有一个inode,inode在内核代码中的数据结构如下: 1 struct ext4_inode ...

  6. cf1294E

    题意简述:给一个矩阵,有两种操作可以进行 操作1:改变矩阵中一个元素的值 操作2:将矩阵中某一列的值循环下移 要求用最少的操作次数使得矩阵变成 题解:对于一列来说,我们肯定是先变化然后再循环下移,所以 ...

  7. java面向对象入门(3)-java构造方法

    Java构造函数是特殊的方法(没有返回类型),使您可以在应用程序内部的其他类使用对象之前完全初始化对象状态.Java中的构造方法是使用new关键字调用的.下面让我们更深入地了解构造函数. 1.什么是j ...

  8. Jmeter连接SqlServer数据库并操作

    jmeter支撑多种数据库,且均需要下载对应的驱动包,如下以SqlServer为例作为讲解,其他数据库类似. 1.下载jdbc驱动(注意下载对应版本),并放在jmeter的lib目录下,重启jmete ...

  9. 嵌入式设备sqlite库移植和使用

    1]官网下载sqlite-autoconf-3300100.tar.gz,网址https://www.sqlite.org/download.html,2]解压:tar zxvf sqlite-aut ...

  10. Anaconda 包管理与环境管理

    包管理命令 conda命令 安装包 conda install 包名称 卸载包 conda remove 包名称 更新包 conda update 包名称 模糊查询 conda search 包名称 ...