BZOJ 1008 [HNOI2008]越狱
2015-07-12 14:45
351 查看
1008: [HNOI2008]越狱
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 5166 Solved: 2242
[Submit][Status][Discuss]
Description
监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱Input
输入两个整数M,N.1<=M<=10^8,1<=N<=10^12Output
可能越狱的状态数,模100003取余Sample Input
2 3Sample Output
6HINT
6种状态为(000)(001)(011)(100)(110)(111)Source
题解:终于捉了一道大水题!所有的可能宗教信仰方案为:M^N
不可能越狱(相邻两个房间的人的宗教信仰不同)的方案为:M*(M-1)^(N-1)
于是最终的答案: [M^N-M*(M-1)^(N-1)]%100003
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<queue> #include<cstring> #define PAU putchar(' ') #define ENT putchar('\n') using namespace std; long long mod=100003; long long pow(long long x,long long y){ long long ans=1;for(long long i=y;i;i>>=1,x=x*x%mod)if(i&1)ans=ans*x%mod;return ans%mod; } inline long long read(){ long long x=0,sig=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') sig=-1;ch=getchar();} while(isdigit(ch)) x=10*x+ch-'0',ch=getchar(); return x*=sig; } inline void write(long long x){ if(x==0){putchar('0');return;}if(x<0) putchar('-'),x=-x; int len=0;long long buf[20];while(x) buf[len++]=x%10,x/=10; for(int i=len-1;i>=0;i--) putchar(buf[i]+'0');return; } long long n,m; void init(){ m=read();n=read(); long long ans=pow(m,n); ans=(ans+mod-m*pow(m-1,n-1)%mod)%mod; write(ans); return; } void work(){ return; } void print(){ return; } int main(){ init();work();print();return 0; }
相关文章推荐
- BZOJ4184 : shallot
- 关于网上一些兼容性问题的处理总结
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree
- 大整数乘法
- Windows进程内部堆的操作
- 内幕视角揭秘那些年的微软和诺基亚
- C语言之基本算法30—数组的灵活应用(一个正整数的各位数字平方和)
- Embedded_SW_模块化]嵌入式C语言工程文件组织_保持更新
- [LeetCode] Merge k Sorted Lists
- ubuntu14.04下安装node.js
- Reverse Linked List
- java基础——Vector集合知识点
- 快速排序
- from __future__ import absolute_import
- 【最短路】poj2472 SPFA
- Elasticsearch集群无法自动集群处理
- 强制手机浏览器竖屏
- 关于mac上xcoed6删除模拟器的方法
- 中缀表达式转后缀表达式
- Gradle DSL method not found: 'runProguard()'