CodeForces
来源:互联网 发布:ipad如何安装淘宝app 编辑:程序博客网 时间:2024/05/17 04:55
题目:给你一个n行m列的网格,再给你k中颜色,问让你对其染色有多少种方法。要求对于任意1<=i<=m,都有1~i的颜色种数等于i+1~m的颜色种数。
n,m<=1000,k<=1e6,答案对1e9+7取模
思路:我们可以发现第1列和第m列的颜色种数一定要一样,否则不符合要求。而且2~m-1列中出现的颜色一定要是第1列和第m列的共有的颜色。
dp[i]表示用i种颜色染n格的方法数
dp[i]=i^n-sigma(C(j,i)*dp[j])
然后枚举共有的颜色i,异色j,累加
代码:
#pragma comment(linker, "/STACK:1024000000,1024000000")#include<iostream>#include<algorithm>#include<ctime>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<list>#include<numeric>using namespace std;#define LL long long#define ULL unsigned long long#define INF 0x3f3f3f3f#define mm(a,b) memset(a,b,sizeof(a))#define PP puts("*********************");template<class T> T f_abs(T a){ return a > 0 ? a : -a; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}// 0x3f3f3f3f3f3f3f3f// 0x3f3f3f3fconst LL MOD=1e9+7;const int maxn=1e6+50;LL fac[maxn],Inv[maxn],dp[1050];LL pow_mod(LL a,LL b){ LL res=1; while(b){ if(b&1) res=res*a%MOD; a=a*a%MOD; b/=2; } return res;}LL C(int n,int m){ return fac[n]*Inv[m]%MOD*Inv[n-m]%MOD;}int main(){ int n,m,k; fac[0]=1; Inv[0]=1; for(int i=1;i<=1000000;i++){ fac[i]=fac[i-1]*(LL)i%MOD; Inv[i]=pow_mod(fac[i],MOD-2); } while(~scanf("%d%d%d",&n,&m,&k)){ if(m==1){ printf("%lld\n",pow_mod((LL)k,(LL)n)); continue; } for(int i=1;i<=k&&i<=n;i++){ dp[i]=pow_mod((LL)i,(LL)n); for(int j=1;j<i;j++){ dp[i]=(dp[i]-(C(i,j)*dp[j]%MOD))%MOD; dp[i]=(dp[i]+MOD)%MOD; } } LL ans=0; if(m==2){ for(int i=1;i<=n&&i<=k;i++){ LL tmp=dp[i]*dp[i]%MOD; tmp=tmp*C(k,i)%MOD*C(k,i)%MOD; ans=(ans+tmp)%MOD; } } else{ for(int i=1;i<=n;i++) for(int j=0;j<=n;j++){ if(i+2*j>k||i+j>n) break; LL tmp=dp[i+j]*dp[i+j]%MOD; tmp=tmp*C(k,i)%MOD*C(k-i,j)%MOD*C(k-i-j,j)%MOD*pow_mod((LL)i,(LL)n*(m-2)); ans=(ans+tmp)%MOD; } } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- react native学习资料以及连接的保存
- linux编译动态库和静态库的makefile示例
- file 图片上传
- Android开发蓝牙篇之蓝牙配对、连接与接收数据
- 集训队的LH(简单DP)
- CodeForces
- DragVideo,一种在播放视频时,可以任意拖拽视频的方案
- 面向对象
- Java序列化的作用和反序列化
- 使用Android编写一个2048小游戏(一)
- httpClient学习
- 文章标题
- Android应用开发性能优化完全分析
- 0823 Java异常处理