题意:有m种字符,要求构造两段长度为n的字符串,其中这两段不能有相同的字符

枚举左边选了i种字符,右边可以选1,2....min(n,m-i)种字符

这样就把问题转化为用k种字符构造n长度的字符串的种类有多少种

容斥:单独考虑每一位上的字符都有k种选择,k^n,但会有不够k种的情况,所以要减去只有k-1种颜色,只有k-2种颜色。。。。的情况

这里是用容斥处理一下

好,上面都是copy过来的东西,现在是自己写的了。。 首先,这道题目比赛的时候没写出来,虽然当时想到了用容斥处理,但是对容斥的情况没有理解清楚。

复习一下容斥,写容斥的时候,我们先要理出最小集合,这些集合的并能够得到我们想到的结果;在在就是集合的交有了一个新的认识,以前的理解太肤浅了。

对于这道题目,我们定理f(x)为x个字母全部用上的方案数,利用容斥,一般要考虑逆问题。f(x)的逆问题———k个元素中,至少有一个字母没用上的方案数。

那么最小集合为有一个字母没用上,其他字母可用可不用的情况,之后的容斥就可以了。

附上一个骚气的代码(自己写的老是超时,有点烦,贴了个其他大佬的):

#include <cstdio>
#include <iostream>
typedef long long LL;
using namespace std;
const int maxn =;
const int mod = 1e9+; 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=,aa=a,bb=b;
a%=mod;
while(b)
{
if(b&)ans=(ans*a)%mod;
a=(a*a)%mod;
b>>=;
}
mul[aa][bb]=ans;
return ans;
} void init()
{
C[][]=;
for(int i=; i<maxn; i++)
for(int j=; j<=i; j++)
C[i][j]=(C[i-][j-]+C[i-][j])%mod;
} LL getget(LL n,LL m)
{
LL cnt=;
for(int k=; k<=m; k++)
{
if(k&)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 = ;
while ((c = getchar()) < '' || c > '');
while (c >= '' && c <= '')
ret = ret * + (c - ''), c = getchar();
} void Out(LL a)
{
if (a < )
{
putchar('-');
a = -a;
}
if (a >= )
Out(a / );
putchar(a % + '');
} int main()
{
init();
int T;
scan_d(T);
while(T--)
{
memset(res,,sizeof(res));
int n,m;
LL ans=;
scan_d(n);
scan_d(m);
for(int i=; i<m; i++)
res[i]=getget(n,i);
for(int i=; 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 ;
}

hdu 6143的更多相关文章

  1. HDU 6143 - Killer Names | 2017 Multi-University Training Contest 8

    /* HDU 6143 - Killer Names [ DP ] | 2017 Multi-University Training Contest 8 题意: m个字母组成两个长为n的序列,两序列中 ...

  2. HDU 6143 Killer Names(容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意: 用m个字母去取名字,名字分为前后两部分,各由n个字符组成,前后两部分不能出现相同字符,问合法的组成 ...

  3. 2017多校第8场 HDU 6143 Killer Names 容斥,组合计数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6143 题意:m种颜色需要为两段长度为n的格子染色,且这两段之间不能出现相同的颜色,问总共有多少种情况. ...

  4. HDU 6143 Killer Names

    Killer Names Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  6. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  7. HDU 6143 Killer Names DP+快速密

    Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human apprentice ...

  8. hdu 6143第二类striling

    题意:有m种字符,要求构造两段长度为n的字符串,其中这两段不能有相同的字符 枚举左边选了i种字符,右边可以选1,2....min(n,m-i)种字符 这样就把问题转化为用k种字符构造n长度的字符串的种 ...

  9. hdu 6143: Killer Names (2017 多校第八场 1011)

    题目链接 题意,有m种颜色,给2n个位置染色,使左边n个和右边n个没有共同的颜色. 可以先递推求出恰用i种颜色染n个位置的方案数,然后枚举两边的染色数就可以了,代码很简单. #include<b ...

随机推荐

  1. Echarts常用API(echarts和echartsInstance)

    一.echarts上的方法 一般在项目中引入echarts之后,可以获得一个全局的echarts对象. 1.下面是几个比较常用的echarts方法 echarts.init() 创建一个echarts ...

  2. GO -- 遍历删除 数组 slice

    删的继续, 没删的i++

  3. Wamp 升级php7.3报错

    电脑系统:win10 Wamp版本: WampServer Version 3.0.4 32bit Apache 2.4.18 - PHP 7.3.7 - MySQL 5.7.11 PHP 5.6.1 ...

  4. opencv常见示例

    1.批量转换灰度图并保存 #include <iostream> #include <opencv2/opencv.hpp> #include <string> u ...

  5. 001-poi-excel-基础、单元格使用操作

    一.概述 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...

  6. PAT 甲级 1056 Mice and Rice (25 分) (队列,读不懂题,读懂了一遍过)

    1056 Mice and Rice (25 分)   Mice and Rice is the name of a programming contest in which each program ...

  7. mybatis/tk mybatis下实体字段是关键字/保留字,执行报错

    实体如下: import com.fasterxml.jackson.annotation.JsonFormat; import com.xxx.web.bean.PagesStatic; impor ...

  8. ClientDataSet中修改,删除,添加数据和Delta属性

    ClientDataSet中使用Post提交变更的数据时,实际上并没有更新到后端数据库中,而是提交到了由DataSnap管理的数据缓冲区中.当使用了ClientDataSet.ApplyUpDates ...

  9. Go 自定义包引入报错

    配置文件 GO111MODULE=on 设置为on时,go命令行会使用modules,而一点也不会去GOPATH目录下查找.但自定义包在 $GOPATH/github.com/winyh/strrev ...

  10. MySQL建表时添加备注以及查看某一张表的备注信息

    建表的时候对列和表明添加备注: DROP TABLE IF EXISTS test_table; CREATE TABLE test_table ( ID INTEGER AUTO_INCREMENT ...