• BZOJ 1008: [HNOI2008]越狱


    传送门

    基础容斥

    不合法方案数 = 总方案数 - 合法方案数

    合法方案数很好求

    第一个位置有 m 种选法

    第二个位置有 m-1 种选法(不能与第一个位置冲突)

    第三个位置有 m-1 种选法(不能与第二个位置冲突)

    ......

    除了第一个位置,其他每个位置有有 m-1 种选法

    那么就是 $m*(n-1)^{(m-1)}$

    总方案数显然为 $n^m$

    注意,先读入 m 再读入 n ,坑了我好久

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    inline ll read()
    {
        ll x=0,f=1; char ch=getchar();
        while(ch<'0'||ch>'9') { if(ch=='-') f=-1; ch=getchar(); }
        while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); }
        return x*f;
    }
    const int mo=100003;
    ll n,m;
    inline ll ksm(ll x,ll y)
    {
        ll res=1;
        while(y)
        {
            if(y&1) res=res*x%mo;
            x=x*x%mo; y>>=1;
        }
        return res;
    }
    int main()
    {
        m=read(); n=read();
        printf("%lld",(ksm(m,n)-(m*ksm(m-1,n-1))%mo+mo)%mo);
        return 0;
    }
  • 相关阅读:
    hdoj1856
    hdoj1009
    hdoj2191
    hdoj1203
    hdoj1053
    hdoj1529
    hdoj1829
    Flex2 Tree从XML文件中加载数据
    RoR:Ruby On Rails 的 Web Service
    Flex2 数据的验证方法以及如何改变错误提示的CSS
  • 原文地址:https://www.cnblogs.com/LLTYYC/p/10104816.html
Copyright © 2020-2023  润新知