2014 xi'an Problem F. Color (Gym 100548F) 容斥原理
来源:互联网 发布:淘宝上有名的女装店 编辑:程序博客网 时间:2024/05/16 19:40
题意:给出n个需要涂色的格子排成一列,有m种颜色,需要从中选出k种颜色,使得任意两个相邻格子不同色。
(n、m<=1e9,k<=1e6)
解法:
容斥原理,首先能想到计算C(m,k),之后考虑的问题是现在用恰好k种颜色去涂色。
首先k*pow(k-1,n)表示的是至多用k种颜色的种数,然后如果想得到恰好用k种颜色的方案数,就用容斥原理,奇加偶减。
#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<vector>using namespace std;#define all(x) (x).begin(), (x).end()#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)#define mes(a,x,s) memset(a,x,(s)*sizeof a[0])#define mem(a,x) memset(a,x,sizeof a)#define ysk(x) (1<<(x))typedef long long ll;typedef pair<int, int> pii;const int INF =0x3f3f3f3f;const int maxK=1e6+0.5 ;const ll mod=1e9+7;ll N,M,K;ll inv[maxK+10],C[maxK+10];ll qb(ll a,ll n){ ll ans=1; while(n) { if(n&1) ans=ans*a%mod; a=a*a%mod; n>>=1; } return ans;}void pre(){ C[0]=1; for1(i,K)//C(K,i) { C[i]=C[i-1]*(K-i+1)%mod*inv[i]%mod; }}ll calC(ll n,ll m){ ll ans=1; for1(i,m) { ans=ans*(n-i+1)%mod*inv[i]%mod; } return ans;}void solve(){ ll ans=0,ret=1; pre(); for(int i=K;i>=1;i--) { ans+=ret*C[i]*i%mod*qb(i-1,N-1)%mod; ans=(ans%mod+mod)%mod; ret*=-1 ; } ans*=calC(M,K); ans=(ans%mod+mod)%mod; printf("%lld\n",ans);}void init(){ inv[0]=1; for1(i,maxK) inv[i]=qb(i,mod-2);}int main(){ std::ios::sync_with_stdio(false); init(); int T,kase=0;cin>>T; while(T--) { cin>>N>>M>>K; printf("Case #%d: ",++kase); pre(); solve(); } return 0;}
0 0
- 2014 xi'an Problem F. Color (Gym 100548F) 容斥原理
- cf/Codeforces Gym 100548F Color (容斥原理)
- cf/Codeforces Gym 100548F Color (2014年西安站F题) (容斥原理)
- Codeforces Gym 100548F Color (组合数+容斥)
- Codeforces Gym 100548F Color (组合数+容斥)
- CF GYM 100548 Color(2014ACM西安现场赛Problem F)
- CF GYM 100548 Color(2014ACM西安现场赛Problem F)
- Gym - 100625F Count Ways 快速幂+容斥原理
- uvalive7040 / cf gym 100548 Color(2014 ICPC 西安 F)
- 2014ACM/ICPC亚洲区西安站 F题 color (组合数学,容斥原理)
- Codeforces GYM 100548 F - Color (2014年西安站F题)
- Codeforces GYM 100548 F - Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest
- uvalive7040 / cf gym 100548 Color(2014西安区域赛F题)
- GDUT 2016 Problem F 我是好人4 (容斥原理 + DFS)
- 2014 xi'an K Last Defence Gym 100548K
- 2014ACM/ICPC亚洲区西安站现场赛 F color(转换角度+容斥)
- [Gym-101350F] F
- Codeforces Gym 100623F Problem F. Fenwick Tree
- nginx对比haproxy 的反向代理
- 【原】东方耀REACT-NATIVE学习43,ios日期组件 ActionSheetIOS
- linux(centos7) 命令
- 图像处理之积分图算法
- java获取客户端ip地址
- 2014 xi'an Problem F. Color (Gym 100548F) 容斥原理
- Apktool 源码分析
- 51Nod-1605-棋盘问题
- C Primer Plus学习 五 研究和利用printf ()和scanf ()
- ubuntu14.04 +nginx+php5-fpm
- POJ2836-Rectangular Covering-状态dp
- Java序列化对象的存储和读取
- How to create AK in PowerDesigner
- UVA 11078 Open Credit System(空间优化+时间优化)