Codeforces 1111D(退背包、排列组合)
要点
- 优质题解
- 因为只有某type坏人全部分布在同一撇时,才能一次消灭。所以题目安排完毕后一定是type(x)和type(y)占一半,其余占另一半。
- 实际情况只有52*52种,则预处理答案
- 枚举某两种,并连续两次使用退背包得到无排列的方案数,真·答案是有排列的,乘上一个排列数即可,而根据式子,排列数恰好与方案细节无关,是个与\(|s|\)和全部\(cnt[i]\)有关的定值
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
string s;
int q, len, x, y;
int cnt[55];
ll f[maxn >> 1], g[maxn >> 1], h[maxn >> 1], Ans[55][55];
ll fac[maxn], finv[maxn];
inline int id(char c) {
return c >= 'A' && c <= 'Z' ? c - 'A' + 26 : c - 'a';
}
ll ksm(ll a, int b) {
ll res = 1LL;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll pre() {
fac[0] = 1;
for (int i = 1; i <= len; i++) {
fac[i] = fac[i - 1] * i % mod;
}
finv[len] = ksm(fac[len], mod - 2);
for (int i = len - 1; i; --i) {
finv[i] = finv[i + 1] * (i + 1) % mod;
}
ll res = 2LL * fac[len / 2] % mod * fac[len / 2] % mod;
for (int i = 0; i < 52; i++) {
if (!cnt[i]) continue;
res = res * finv[cnt[i]] % mod;
}
return res;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> s;
for (auto i : s) {
cnt[id(i)]++;
}
len = s.length();
ll base = pre();
f[0] = 1;
for (int i = 0; i < 52; i++) {
if (!cnt[i]) continue;
for (int j = len / 2; j >= cnt[i]; --j) {
f[j] = (f[j] + f[j - cnt[i]]) % mod;
}
}
for (int i = 0; i < 52; i++) {
if (!cnt[i]) continue;
for (int j = 0; j <= len / 2; j++) {
if (j < cnt[i]) g[j] = f[j];
else g[j] = (f[j] - g[j - cnt[i]] + mod) % mod;
}
Ans[i][i] = base * g[len / 2] % mod;
for (int j = i + 1; j < 52; j++) {
if (!cnt[j]) continue;
for (int k = 0; k <= len / 2; k++) {
if (k < cnt[j]) h[k] = g[k];
else h[k] = (g[k] - h[k - cnt[j]] + mod) % mod;
}
Ans[i][j] = Ans[j][i] = base * h[len / 2] % mod;
}
}
for (cin >> q; q; q--) {
cin >> x >> y;
cout << Ans[id(s[x - 1])][id(s[y - 1])] << '\n';
}
return 0;
}
Codeforces 1111D(退背包、排列组合)的更多相关文章
- POJ1285 Combinations, Once Again(背包 排列组合)
背包解组合数学问题,n种物品,每种num[i]个,求取r个的方法数. 背包思想,f[j]表示当前取j个数的方法数,则状态转移方程为 f[j] += f[k](max(j - num[i], 0) &l ...
- Destroy the Colony CodeForces - 1111D (可逆背包,计数)
大意:给定字符串$s$, 保证长度为偶数, 给定q个询问, 每次询问给定两个位置$x$,$y$, 可以任意交换字符, 要求所有字符$s[x],s[y]$在同一半边, 剩余所有同种字符在同一半边的方案数 ...
- CodeForces 452C Magic Trick (排列组合)
#include <iostream> #include <cstdio> #include<cmath> #include<algorithm> us ...
- Codeforces Gym 100187D D. Holidays 排列组合
D. Holidays Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/D ...
- Codeforces Round #309 (Div. 2) C. Kyoya and Colored Balls 排列组合
C. Kyoya and Colored Balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contes ...
- [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)
[Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...
- [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)
[Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) ...
- 【CodeForces】914 H. Ember and Storm's Tree Game 动态规划+排列组合
[题目]H. Ember and Storm's Tree Game [题意]Zsnuoの博客 [算法]动态规划+排列组合 [题解]题目本身其实并不难,但是大量干扰因素让题目显得很神秘. 参考:Zsn ...
- 【CodeForces】889 C. Maximum Element 排列组合+动态规划
[题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...
随机推荐
- jQuery Tab选项卡切换代码
jQuery Tab选项卡切换代码是一款简单的jquery tab选项卡切换网页特效代码样式,可以修改tab选项卡相关样式. 代码下载:http://www.huiyi8.com/sc/10863.h ...
- 分享知识-快乐自己:SpringMvc中的四种数据源及相关配置(整合快速集成开发)
数据库连接: jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://39.105.105.186:3306/SpringMybatis?us ...
- fiddler篡改请求数据
有时需要修改请求或返回结果来验证网站存在的漏洞,因此需要使用到fiddler的断点功能. 如何修改请求前数据? 1.设置请求前断点 Rules--Automatic breakpoints--befo ...
- codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)
题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...
- ACM学习历程—HDU1719 Friend(数论)
Description Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; ...
- ACM学习历程—CodeForces 176B Word Cut(字符串匹配 && dp && 递推)
Description Let's consider one interesting word game. In this game you should transform one word int ...
- 在VS中对WCF服务进行更新,但是配置文件没有更新解决办法
取消下面的勾选框
- Crypto Challenge Set 1解题报告
1.Convert hex to base64 题意:给出一个hex编码过的字符串,将它进行base64加密 解题关键:直接利用base64库函数实现 import base64 str1=" ...
- [poj2151]Check the difficulty of problems概率dp
解题关键:主要就是概率的推导以及至少的转化,至少的转化是需要有前提条件的. 转移方程:$dp[i][j][k] = dp[i][j - 1][k - 1]*p + dp[i][j - 1][k]*(1 ...
- 教程:动手用自己电脑搭建一个网站 (nat123 花生壳 动态域名 个人电脑做服务器)
先ps一下..今晚试了N种方法,终于找到一个靠谱 免费 好用的方法,来改装自己电脑成为服务器,在外网也能通过域名访问了!!! 需要准备的东西: Tomcat:这个是web容器,其实有了这个就已经让电脑 ...