HNOI2008 越狱 (组合数学)

传送门

应该是HNOI2008年最简单的一道题了吧……简单的组合数题,不过要换个思路。

我们直接考虑发生越狱的情况似乎有点复杂,那我们换个思路,考虑不发生越狱的情况,也就是两个有相同宗教的人不会坐在一起。

第一个人有m种宗教可以信仰,那么第2个就只有m-1种了,不过我们发现,之后,第3个人其实还可以信仰m-1种宗教……只要不和第2个人相同,第4个人和以后的人也同理。那么方案就是m * (m-1)^(n-1)

总的方案数是m^n,相减即为所求,注意取模的时候不要有负数。

看一下代码。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')

using namespace std;
typedef long long ll;
const int M = 200005;
const ll P = 100003;


ll read()
{
    ll ans = 0,op = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-') op = -1;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        ans *= 10;
        ans += ch - '0';
        ch = getchar();
    }
    return ans * op;
}
ll m,n;
ll qpow(ll a,ll b)
{
    ll q = 1;
    while(b)
    {
        if(b&1) q *= a,q %= P;
        a *= a,a %= P;
        b >>= 1;
    }
    return q % P; 
}
int main()
{
    m = read(),n = read(),m %= P;
    printf("%lld\n",(qpow(m,n) - m * qpow(m-1,n-1) % P + P) % P);
    return 0;
}

 

posted @ 2018-08-23 18:39  CaptainLi  阅读(134)  评论(0编辑  收藏  举报