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;
}

 

转载于:https://www.cnblogs.com/captain1/p/9525580.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值