BZOJ1008: [HNOI2008]越狱(组合数)
题目描述
监狱有连续编号为 1…N1…N 的 NN 个房间,每个房间关押一个犯人,有 MM 种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱。
输入输出格式
输入格式:
输入两个整数 $M,N$
输出格式:
可能越狱的状态数,模 100003100003 取余
输入输出样例
说明
6种状态为(000)(001)(011)(100)(110)(111)
1 \le M \le 10^81≤M≤108
1 \le N \le 10^{12}1≤N≤1012
很zz的数数题。
发现直接数不好数,那就容斥一下吧,。
所有的情况是$M^N$,两两不能相邻,那么第一个可以选$M$个,第二个可以选$M - 1$个,以此类推都能选$M - 1$个
因此答案为$M^N - M * (N - 1)^{M - 1}$
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<map>
#define int long long
using namespace std;
const int MAXN = * 1e5 + , mod = ;
inline int read() {
char c = getchar(); int x = , f = ;
while(c < '' || c > '') {if(c == '-') f = -; c = getchar();}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x * f;
}
int M, N;
int fastpow(int a, int p) {
int base = ;
while(p) {
if(p & ) base = (base % mod * a % mod) % mod;
a = (a % mod * a % mod) % mod; p >>= ;
}
return base % mod;
}
main() {
M = read(); N = read();
//M %= mod; N %= mod;
printf("%lld", (fastpow(M, N) - (M % mod) * (fastpow(M - , N - )) % mod + mod) % mod);
}
BZOJ1008: [HNOI2008]越狱(组合数)的更多相关文章
- bzoj1008: [HNOI2008]越狱 数学公式+快速幂
bzoj1008: [HNOI2008]越狱 O(log N)---------------------------------------------------------------- ...
- bzoj1008 [HNOI2008]越狱
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5099 Solved: 2207 Description 监狱有 ...
- BZOJ1008: [HNOI2008]越狱-快速幂+取模
1008: [HNOI2008]越狱 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 8689 Solved: 3748 Description 监狱有 ...
- BZOJ1008 [HNOI2008]越狱 快速幂
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1008 题意概括 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可 ...
- [BZOJ1008] [HNOI2008] 越狱 (数学)
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
- [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
- bzoj1008 [HNOI2008]越狱——快速幂
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 (这样一道水题还因为忘记写 %lld WA了那么多遍) 发生越狱的状态数,就是全部状态 ...
- [Bzoj1008][HNOI2008]越狱(组合计数)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 组合计数的简单题,可能越狱的方案数等于总方案数-不可能越狱的方案数,则: 总方案数 ...
- 【数论】【快速幂】bzoj1008 [HNOI2008]越狱
根据 高中的数学知识 即可推出 ans=m^n-m*(m-1)^(n-1) .快速幂取模搞一下即可. #include<cstdio> using namespace std; typed ...
随机推荐
- [转]jQuery: get table column/row index remove table column (by column number)
本文转自:http://www.xinotes.org/notes/note/1087/ <!DOCTYPE html><html><head> <title ...
- (转)shell脚本输出带颜色字体
shell脚本输出带颜色字体 原文:http://blog.csdn.net/andylauren/article/details/60873400 输出特效格式控制:\033[0m 关闭所有属性 ...
- pat1020. Tree Traversals (25)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- php乱码的解决方法
一般如果会出现乱码,基本都是因为编码不统一造成的,所以我们只需要把编码改成一样的就好了. 我们编码一般都是以utf-8为主: 文件编码都是UTF-8无BOM,如果文件不是这个编码,可以使用文件编辑器转 ...
- ssh设置别名
通常我们在 Termianl 下用 ssh 链接远程主机的时候,每次都需要输入一长串的用户名加主机地址,是不是觉得很麻烦? 我们知道在 /etc/ssh/ 目录下通常都会有 ssh_config 和 ...
- app启动黑屏
由于在手机上多次删除应用,导致加载app时加载缓存图片缺失,进行如下操作: 删除Xcode DerivedData目录释放空间: 1.首先切换到 Finder 程序,打开“前往”菜单. 2.然后按住 ...
- jQuery中的节点操作(二)
html代码如下 <p title="武汉长乐教育PHP系列教程" name="hello" class="blue"> < ...
- ArcGIS 10.2之地图服务的发布、使用
2.发布地图服务 2.1 地图服务器的建立 打开ArcCatalog,在左侧的GIS Servers下,双击Add ArcGIS Server, 弹出添加界面,选择,Administer GIS服务项 ...
- SQL Stored Procedure and Function
Anything can be programmable with defined syntax and common lib. )) -- Add the parameters for the st ...
- react-native与原生界面相互跳转
一.添加MyIntentModule类,并继承ReactContextBaseJavaModule实现其方法和构造函数.在该类中添加方法,注意:方法头要加@ReactMethod public cla ...