uvalive7040 / cf gym 100548 Color(2014 ICPC 西安 F)

来源:互联网 发布:linux内核设计艺术pdf 编辑:程序博客网 时间:2024/06/05 20:58

题意:n个位置m种颜色进行染色,要求相邻位置颜色不相同,切正好使用k种不同颜色

题解:


#include <iostream>#include <cstdio>#include <cctype>#include <algorithm>#include <cstring>#include <string>#include <cmath>#include <vector>#include <set>#include <stack>#include <sstream>#include <queue>#include <map>#include <functional>#include <bitset>//#include <unordered_map>//#include <unordered_set>using namespace std;#define pb push_back#define mk make_pair#define ll long long#define ull unsigned long long#define pii pair<int, int>#define fi first#define se second#define mid ((l+r)/2)#define ALL(A) A.begin(), A.end()#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)#define repr(i, n) for(int (i)=(int)(n);(i)>=0;(i)--)#define repab(i,a,b) for(int (i)=(int)(a);(i)<=(int)(b);(i)++)#define reprab(i,a,b) for(int (i)=(int)(a);(i)>=(int)(b);(i)--)#define sc(x) scanf("%d", &x)#define pr(x) printf(#x":%d\n", x)#define fastio ios::sync_with_stdio(0), cin.tie(0)#define frein freopen("in.txt", "r", stdin)#define freout freopen("out.txt", "w", stdout)#define freout1 freopen("out1.txt", "w", stdout)#define lson (rt*2+1)#define rson (rt*2+2)#define lb puts("")#define PI M_PI#define debug cout<<"???"<<endlconst ll mod = 1e9+7;const int INF = 2e9+5;//int INF = 0x3f3f3f3f;const double eps = 1e-6;template<class T> T gcd(T a, T b){if(!b)return a;return gcd(b,a%b);}const int maxn = 1e6+100;int n,m,k;ll fac[maxn], iv[maxn], cnt[maxn], ans;void extgcd(ll aa, ll bb, ll& dd, ll& xx, ll& yy){    if(!bb){        dd = aa;        xx = 1;        yy = 0;    }else{        extgcd(bb, aa%bb, dd, yy, xx);        yy -= xx*(aa/bb);    }}ll inv(ll aa, ll mm){    ll dd,  xx, yy;    extgcd(aa, mm, dd, xx, yy);    return dd==1?(xx+mm)%mm:-1;}void init(){    fac[0] = 1;    iv[0] = inv(fac[0], mod);    for(int i = 1; i < maxn; i++){        fac[i] = (i*fac[i-1])%mod;        iv[i] = inv(fac[i], mod);    }}ll comb(int x, int y){    ll res = (fac[x]*iv[x-y])%mod;    res = (res*iv[y])%mod;    return res;}ll q_pow(ll x, int y){    ll res = 1;    while(y > 0){        if(y&1) res = (res*x)%mod;        x = (x*x)%mod;        y >>= 1;    }    return res;}ll f(ll x){    return (x*q_pow(x-1, n-1))%mod;}ll cal(){    ll res = 0;    int xishu = 1;    for(int i = k; i >= 1; i--){        ll tmp = (comb(k,i)*i)%mod;        tmp = (tmp*q_pow(i-1, n-1))%mod;        //cout << tmp << endl;        res = (res+xishu*tmp+mod)%mod;        xishu *= -1;    }    //printf("res:%lld\n", res);    return res;}ll c(){    ll res = 1;    for(int i = 0; i < k; i++){        res = (res*(m-i))%mod;    }    res = (res*inv(fac[k], mod))%mod;    //printf("res:%lld\n", res);    return res;}int main(){    init();    int t; sc(t);    int kase = 1;    while(t--){        sc(n); sc(m); sc(k);        ans = 0;        ll ans = (c()*cal())%mod;        printf("Case #%d: %lld\n", kase++, ans);    }    return 0;}


阅读全文
0 0
原创粉丝点击