1008: [HNOI2008]越狱[简单题]

本文探讨了一种关于监狱中犯人宗教信仰导致越狱风险的数学问题,通过计算不同宗教信仰犯人在相邻房间时的越狱可能性,利用组合数学原理,介绍了如何计算总方案数与不发生越狱的方案数,进而得出越狱风险的状态数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


题解:我们考虑用总的方案减掉不能越狱的方案。也就是相邻两个不同的方案数。


拓展:如果题目改为恰好使用M种宗教的话那么做法为二项式反演了,因为可以反演出至多用M种宗教的方案。


ac code:ac\ code:ac code:

/**************************************************************
    Problem: 1008
    User: ReJ
    Language: C++
    Result: Accepted
    Time:44 ms
    Memory:1288 kb
****************************************************************/
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define g getchar()
#define met(a, b) memset(a, b, sizeof(a))
#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 fi first
#define se second
const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 100003;
 
inline ll read() {
    ll x = 0, f = 1;
    char ch = g;
    while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = g;}
    while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = g;}
    //cout << f * x << endl;
    return f * x;
}
 
ll qp(ll base, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1) res = res * base % mod;
        base = base * base % mod;
        n >>= 1;
    }
    return res;
}
 
int main() {
    ll m, n;
//    scanf("%lld %lld", &m, &n);
    m = read(); n = read();
    printf("%lld\n", (qp(m, n) % mod - m * qp(m - 1, n - 1) % mod + mod) % mod);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值