1008: [HNOI2008]越狱

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 10503  Solved: 4558
[Submit][Status][Discuss]

Description

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

Input

  输入两个整数M,N.1<=M<=10^8,1<=N<=10^12

Output

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

Sample Input

2 3

Sample Output

6

HINT

  6种状态为(000)(001)(011)(100)(110)(111)

Source

析:很容易知道,一共有 m^n 种方案,然后我们可以求全部都不一样的,也就是m*(m-1)*(m-1)..*(m-1) 也就是m * (m-1)^(n-1)。由于 m 和 n 比较大,所以要用快速幂。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 2e5 + 10;
const int maxm = 3e5 + 10;
const int mod = 100003;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} LL fast_pow(LL a, LL n){
LL res = 1;
a %= mod;
while(n){
if(n&1) res = res * a % mod;
n >>= 1;
a = a * a % mod;
}
return res;
} int main(){
LL n, m;
scanf("%lld %lld", &m, &n);
LL ans = fast_pow(m, n) - m * fast_pow(m-1, n-1) % mod;
ans = (ans % mod + mod) % mod;
printf("%lld\n", ans);
return 0;
}

  

BZOJ 1008 [HNOI2008]越狱 (简单排列组合 + 快速幂)的更多相关文章

  1. BZOJ 1008 [HNOI2008]越狱 排列组合

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4788  Solved: 2060[Submit][Status] ...

  2. BZOJ 1008: [HNOI2008]越狱-快速幂/取模

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

  3. BZOJ 1008: [HNOI2008]越狱 快速幂

    1008: [HNOI2008]越狱 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生 ...

  4. BZOJ 1008 [HNOI2008]越狱

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5166  Solved: 2242[Submit][Status] ...

  5. bzoj 1008: [HNOI2008]越狱 数学

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description 监狱有连 ...

  6. BZOJ 1008: [HNOI2008]越狱【组合】

    很少有的思路秒解.题意可以描述成对长度为n的格子有m种染色方案,问存在相邻两个格子同色的方案数,正难则反易,考虑问题的背面任意两个相邻的格子都不同色,第一个格子可以涂任意一种颜色m种可能,剩下的n-1 ...

  7. A - Alice and the List of Presents (排列组合+快速幂取模)

    https://codeforces.com/contest/1236/problem/B Alice got many presents these days. So she decided to ...

  8. BZOJ 1009: [HNOI2008]GT考试( dp + 矩阵快速幂 + kmp )

    写了一个早上...就因为把长度为m的也算进去了... dp(i, j)表示准考证号前i个字符匹配了不吉利数字前j个的方案数. kmp预处理, 然后对于j进行枚举, 对数字0~9也枚举算出f(i, j) ...

  9. BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)

    1009: [HNOI2008]GT考试 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4266  Solved: 2616[Submit][Statu ...

随机推荐

  1. Annoying “Remote System Explorer Operation” causing freeze for couple of seconds

    Eclipse -> Preferences -> General -> Startup and Shutdown. -Uncheck RSE UI. Eclipse -> P ...

  2. Nginx 反向代理、后端检测模块

    简介: Nginx 反向代理模块:ngx_http_proxy_module.ngx_http_upstream_module 后端检测模块:nginx_http_upstream_check_mod ...

  3. avalon做的抽奖效果

    .sweepstake { color: orange; font-size: 24px; font-weight: bold; } 先来一个简单的 <style> .sweepstake ...

  4. HttpSession的关键属性和方法

    1.当一个用户向服务器发送第一个请求时,服务器为其建立一个session,并为此session创建一个标识号:2.这个用户随后的所有请求都应包括这个标识号.服务器会校对这个标识号以判断请求属于哪个se ...

  5. 第一个struct2程序

    [第1步] 安装Struts2 这一步对于Struts1.x和Struts2都是必须的,只是安装的方法不同.Struts1的入口点是一个Servlet,而Struts2的入口点是一个过滤器(Filte ...

  6. VS Code常用快捷键总结

    目录: 1.主命令框 2.常用快捷键   (1) 编辑器与窗口管理   (2) 代码编辑     <1> 格式调整     <2> 光标相关     <3> 重构代 ...

  7. linux kernel 的配置及编译

    1. 执行make menuconfig 配置内核 2. 执行make zImage 编译内核 3. 执行make modules 编译模块 4. 内核源代码的配置及编译系统 Makefile Kco ...

  8. chrome 调试参数(鼠标事件)

    1.监听鼠标事件: monitorEvents(document.body, 'mouse') 取消监听: unmonitorEvents(document.body) 原文链接: https://b ...

  9. Docker Dockerfile 定制镜像(转)

    转自: https://yeasy.gitbooks.io/docker_practice/ 及 https://blog.csdn.net/wo18237095579/article/details ...

  10. 349. Intersection of Two Arrays 是否重复

    不重复的: 方法一 [一句话思路]:排序之后用归并. [一刷]: 根据返回方法的类型来判断corner case.判断空.0数组的corner case还不一样,得分开写 由于先排序了,nums1[i ...