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. bootstrap的自适应 和细节点

    bootstrap的自适应尽量少用绝对宽度px来定义大小,这会导致缩小屏幕宽度时相冲突,多使用百分比来改变位置.. 遇到不能用margin和padding来改变位置时,首先应该想到绝对定位和相对定位( ...

  2. sublime3 快捷键总结

    配置环境:win7 sublime3 1,快速html5模板页(已安装Emmet插件) 输入html:5后,然后 ctrl+e 2,快速复制 ctrl+shift+d 复制光标所在当前行 3,向上/向 ...

  3. Android中TimePicker时间选择器的使用和获取选择的时和分

    场景 实现效果如下 注: 博客: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 将布局改 ...

  4. .net core 中如何运用 appsettings.json 进行配置开发、生产不同配置

    .net core 默认会有 appsettings.Development.json 文件,这是根据ASPNETCORE_ENVIRONMENT来读取的. 新建架构appsettings.Produ ...

  5. PMP--1.7 项目治理

    治理凌驾于管理之上 组织治理用于影响项目治理. 组织治理需要组织根据组织文化.项目类型和组织需求裁剪治理框架,适用于当前组织. 其实组织治理的内容,在项目管理初期不需要详细了解,组织治理的内容都是高层 ...

  6. go 面向对象

    结构体 创建结构体变量和访问结构体字段 package main import "fmt" //创建结构体变量和访问结构体字段 type Person struct { Name ...

  7. urlencode($url):把url转义,当字符串数据以url的形式传递给web服务器时,字符串中是不允许出现空格和特殊字符串的

    1.对url进行编码转义

  8. QingTing.Fm-WPF是调用蜻蜓FMAPI 查询API内容展示,进行播放

    QingTing.Fm 是调用蜻蜓FM   API 查询界面内容,进行在线播放. Release地址下载 环境 Visual Studio 2019,dotNet Framework 4.6.1 SD ...

  9. Spring Mvc Http 400 Bad Request问题排查

    如果遇到了Spring MVC报错400,而且没有返回任何信息的情况下该如何排查问题? 问题描述 一直都没毛病的接口,今天测试的时候突然报错400 Bad Request,而且Response没有返回 ...

  10. linux find命令mtime/atime/ctime +n -n n 全网最正确的总结

    1.man find 解释: find - search for files in a directory hierarchy,即:在目录下查找文件 2.按文件被修改的时间查询文件 命令格式: fin ...