[bzoj Solution Notice Code

给你n个空格,每个格子可以填1~m的数,问有多少种存在两个相邻数相等的方案。

Solution

这是一道[HNOI2008]的题。
我们发现求有两个相邻的数相等比较难求,于是我们使用容斥,用总方案数减去没有相邻数相等的方案数。
所以答案就是 m ^ n - m * (m - 1) ^ n.

Notice

注意要开long long

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define travel(i, u) for (int i = head[u]; ~i; i = edge[i].next)

const ll INF = 1e9, Mo = 100003;
const int N = 2000000;
const double eps = 1e-6;
namespace slow_IO
{
ll ()
{
ll x = 0; int zf = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
return x * zf;
}
void write(ll y)
{
if (y < 0) putchar('-'), y = -y;
if (y > 9) write(y / 10);
putchar(y % 10 + '0');
}
}
using namespace slow_IO;

ll pow(ll a, ll b, ll Mo)
{
ll ans = 1;
while (b)
{
if (b & 1) ans = ans * a % Mo;
a = a * a % Mo;
b >>= 1;
}
return ans;
}
int sqz()
{
ll m = read(), n = read();
printf("%lldn", (pow(m, n, Mo) - m * pow(m - 1, n - 1, Mo) % Mo + Mo) % Mo);
return 0;
}