题目描述

监狱有连续编号为 1…N1…N 的 NN 个房间,每个房间关押一个犯人,有 MM 种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱。

输入输出格式

输入格式:

输入两个整数 $M,N$

输出格式:

可能越狱的状态数,模 100003100003 取余

输入输出样例

输入样例#1: 复制

2 3
输出样例#1: 复制

6

说明

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]越狱(组合数)的更多相关文章

  1. bzoj1008: [HNOI2008]越狱 数学公式+快速幂

    bzoj1008: [HNOI2008]越狱      O(log N)---------------------------------------------------------------- ...

  2. bzoj1008 [HNOI2008]越狱

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5099  Solved: 2207 Description 监狱有 ...

  3. BZOJ1008: [HNOI2008]越狱-快速幂+取模

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8689  Solved: 3748 Description 监狱有 ...

  4. BZOJ1008 [HNOI2008]越狱 快速幂

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1008 题意概括 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可 ...

  5. [BZOJ1008] [HNOI2008] 越狱 (数学)

    Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...

  6. [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)

    Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...

  7. bzoj1008 [HNOI2008]越狱——快速幂

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 (这样一道水题还因为忘记写 %lld WA了那么多遍) 发生越狱的状态数,就是全部状态 ...

  8. [Bzoj1008][HNOI2008]越狱(组合计数)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1008 组合计数的简单题,可能越狱的方案数等于总方案数-不可能越狱的方案数,则: 总方案数 ...

  9. 【数论】【快速幂】bzoj1008 [HNOI2008]越狱

    根据 高中的数学知识 即可推出 ans=m^n-m*(m-1)^(n-1) .快速幂取模搞一下即可. #include<cstdio> using namespace std; typed ...

随机推荐

  1. python3.4 x86_64-linux-gnu-gcc Error

    running install    running build    running build_py    creating build    creating build/lib.linux-x ...

  2. LeetCode 981.基于时间的键值存储(C++)

    创建一个基于时间的键值存储类 TimeMap,它支持下面两个操作: 1. set(string key, string value, int timestamp) 存储键 key.值 value,以及 ...

  3. js循环里进行回调,引用循环里的变量,发现只是最后值的问题

    做项目的时候,栽在一个小地方,是这样的 我有很多个坐标点,我想把这些坐标点都绑定一个事件,当点击了这个坐标点之后,发送一个ajax 请求,将坐标点的id 发出去,等待显示返回的数据 但是实际当中,无论 ...

  4. 利用jquery给指定的table动态添加一行、删除一行,复制,值不重复等操作

    $("#mytable tr").find("td:nth-child(1)") 1表示获取每行的第一列$("#mytable tr").f ...

  5. 拼json对象批量向后台添加数据

    网站中如果遇到批量提交格式相同数据,可以使用json来传输 $("#catalogSave").click(function(){ var array=[]; $("[n ...

  6. Java 利用poi读取excel

    import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.Inpu ...

  7. hdu 4123 树形DP+单调队列

    http://acm.hust.edu.cn/vjudge/problem/25790 这题基本同poj 3162 要注意mx,mx2,vx,vx2每次都要初始化 #include <iostr ...

  8. node-sass 安装报错解决办法

    npm install安装node-sass时出现以下问题: Cannot download https://github.com/sass/node-sass/releases/download/v ...

  9. css多行文本溢出显示省略号

    HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <met ...

  10. 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser

    Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...