bzoj1008 [HNOI2008]越狱
1008: [HNOI2008]越狱
Time Limit: 1 Sec Memory Limit: 162 MB
Submit: 5099 Solved: 2207
Description
监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱
Input
输入两个整数M,N.1<=M<=10^8,1<=N<=10^12
Output
可能越狱的状态数,模100003取余
Sample Input
Sample Output
HINT
6种状态为(000)(001)(011)(100)(110)(111)
Source
大意:给N个格子,M种颜色,给这些格子染色,问使这N个格子,存在相邻两个格子颜色相同的方案数
分析:简单明了,简单异常
首先一共有M^N种方式染色
使相邻格子颜色不同的方案有M*(M-1)^(N-1)种(第一位有M种选择,后面每位都只有M-1种选择)
然后相邻格子颜色不相同的方案:M^N - M*(M-1)^(N-1)
最后提醒:注意因为是快速幂取模,所以答案有可能为负数,要修正为正数
综上所述,本题得解
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((bnt) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name) {
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} const LL Mod = ;
LL N, M;
LL All, Impossible, Possible; inline void Input() {
cin>>M>>N;
} inline void Multiply(LL &A, LL B) {
A = (A*B)%Mod;
} inline LL Power(LL Basic, LL Tim) {
LL Ret = ;
Basic %= Mod;
while(Tim) {
if(Tim&) Multiply(Ret, Basic);
Multiply(Basic, Basic), Tim >>= ;
}
return Ret;
} inline void Solve() {
if(N == ) {
cout<<M<<endl;
return;
} All = Power(M, N);
Impossible = Power(M-, N-);
Multiply(Impossible, M);
Possible = All-Impossible;
Possible = ((Possible%Mod)+Mod)%Mod; cout<<Possible<<endl;
} int main() {
SetIO("");
Input();
Solve();
return ;
}
bzoj1008 [HNOI2008]越狱的更多相关文章
- bzoj1008: [HNOI2008]越狱 数学公式+快速幂
bzoj1008: [HNOI2008]越狱 O(log N)---------------------------------------------------------------- ...
- 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]越狱(组合数)
题目描述 监狱有连续编号为 1…N1…N 的 NN 个房间,每个房间关押一个犯人,有 MM 种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. ...
- 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 ...
随机推荐
- valgrind检查C++内存泄漏
valgrind --tool=memcheck --leak-check=full ./httptest Valgrind 使用 用法: valgrind [options] prog-and-ar ...
- mysqli扩展库的预处理技术 mysqli stmt
//预编译演示 //1,创建mysqli对象 $mysqli=new mysqli("localhost","root",""," ...
- 开发一款完备的android应用所必备的知识
原文:http://blog.csdn.net/xyz_lmn/article/details/17575709
- Ubuntu / Win7 安装db2 v10.5
抓紧下载v10.5fp1_linuxx64_expc.tar.gz到~/Downloads/java_softcd java_softtar xf v10.5fp1_linuxx64_expc.tar ...
- 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍
一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...
- android 入门-android属性介绍
android:visibility="gone" 不保留view控件所占有的空间 隐藏 android:visibility="invisible" 保留 ...
- jquery获取radio和select选中值
//jquery 获取radio选中值 <input type="radio" name="c_type" value="a" > ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- JSON资料汇总
网络入门学习资料 1.W3School的JSON教程:http://www.w3school.com.cn/json/index.asp 2.Introducing JSON[介绍JSON]:http ...
- 使用SOUI开发的界面集锦
仿QQ管家界面