枚举要为这两段分配的颜色数目分别为 i,ji,j ,则在第一段总共有 (mi)(mi) 种选取方案,在第二段总共有 (m−ij)(m−ij) 种选取方案。
而在每段内部,我们设 F(n,x)F(n,x) 为长度为 nn 的格子使用 xx 种颜色(等于 xx )染色的方案数。
则根据容斥原理F(n,x)=xn−(m1)(x−1)n+(m2)(x−2)n−(m3)(x−3)n+...F(n,x)=xn−(m1)(x−1)n+(m2)(x−2)n−(m3)(x−3)n+...
//注意细节,优化很重要,很重要,在1s多点一直在卡,卡到boom#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int maxn =2005;
const int mod = 1e9+7;
LL res[maxn];
LL C[maxn][maxn];
LL mul[maxn][maxn];
LL mult(LL a,LL b)
{
if(mul[a][b])return mul[a][b];
LL ans=1,aa=a,bb=b;
a%=mod;
while(b)
{
if(b&1)ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=1;
}
mul[aa][bb]=ans;
return ans;
}
void init()
{
C[0][0]=1;
for(int i=1; i<maxn; i++)
for(int j=0; j<=i; j++)
C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
}
LL getget(LL n,LL m)
{
LL cnt=0;
for(int k=0; k<=m; k++)
{
if(k&1)cnt=(cnt-C[m][k]*mult(m-k,n)%mod)%mod;
else cnt=(cnt+C[m][k]*mult(m-k,n)%mod+mod)%mod;
}
return cnt;
}
template <class T>
inline void scan_d(T &ret)
{
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9');
while (c >= '0' && c <= '9')
ret = ret * 10 + (c - '0'), c = getchar();
}
void Out(LL a)
{
if (a < 0)
{
putchar('-');
a = -a;
}
if (a >= 10)
Out(a / 10);
putchar(a % 10 + '0');
}
int main()
{
init();
int T;
scan_d(T);
while(T--)
{
memset(res,0,sizeof(res));
int n,m;
LL ans=0;
scan_d(n);
scan_d(m);
for(int i=1; i<m; i++)
res[i]=getget(n,i);
for(int i=1; i<m; i++)
for(int j=i; j<=m-i; j++)
{
LL aa=C[m][i]*res[i]%mod;
LL bb=C[m-i][j]*res[j]%mod;
LL cnt = (aa*bb)%mod;
if(i!=j) cnt = (cnt+cnt)%mod;
ans=(cnt+ans)%mod;
}
Out(ans);
putchar('\n');
}
return 0;
}F(n,x)=
本文探讨了如何利用组合数学中的容斥原理来计算特定条件下序列的染色方案数量。通过对序列分段并限定每段使用的颜色种类,采用递归公式求解不同颜色配置的数量。
1万+

被折叠的 条评论
为什么被折叠?



