要点

  • 优质题解
  • 因为只有某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(退背包、排列组合)的更多相关文章

  1. POJ1285 Combinations, Once Again(背包 排列组合)

    背包解组合数学问题,n种物品,每种num[i]个,求取r个的方法数. 背包思想,f[j]表示当前取j个数的方法数,则状态转移方程为 f[j] += f[k](max(j - num[i], 0) &l ...

  2. Destroy the Colony CodeForces - 1111D (可逆背包,计数)

    大意:给定字符串$s$, 保证长度为偶数, 给定q个询问, 每次询问给定两个位置$x$,$y$, 可以任意交换字符, 要求所有字符$s[x],s[y]$在同一半边, 剩余所有同种字符在同一半边的方案数 ...

  3. CodeForces 452C Magic Trick (排列组合)

    #include <iostream> #include <cstdio> #include<cmath> #include<algorithm> us ...

  4. Codeforces Gym 100187D D. Holidays 排列组合

    D. Holidays Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/D ...

  5. 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 ...

  6. [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

    [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...

  7. [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理)

    [Codeforces 997C]Sky Full of Stars(排列组合+容斥原理) 题面 用3种颜色对\(n×n\)的格子染色,问至少有一行或一列只有一种颜色的方案数.\((n≤10^6)\) ...

  8. 【CodeForces】914 H. Ember and Storm's Tree Game 动态规划+排列组合

    [题目]H. Ember and Storm's Tree Game [题意]Zsnuoの博客 [算法]动态规划+排列组合 [题解]题目本身其实并不难,但是大量干扰因素让题目显得很神秘. 参考:Zsn ...

  9. 【CodeForces】889 C. Maximum Element 排列组合+动态规划

    [题目]C. Maximum Element [题意]给定n和k,定义一个排列是好的当且仅当存在一个位置i,满足对于所有的j=[1,i-1]&&[i+1,i+k]有a[i]>a[ ...

随机推荐

  1. C#SocketAsyncEventArgs实现高效能多并发TCPSocket通信 (服务器实现)

    http://freshflower.iteye.com/blog/2285272 想着当初到处找不到相关资料来实现.net的Socket通信的痛苦与心酸, 于是将自己写的代码公布给大家, 让大家少走 ...

  2. hyperledger fabric学习(1)

    第一部分 环境搭建 说明 本次环境搭建是是现在虚拟机中,采用ubuntu 16.04版本,安装多次成功. 首先安装一些常用的工具 sudo apt-get update sudo apt-get in ...

  3. 几个网络模型的示例代码(BlockingModel、OverlappedModel、WSAEventSelect、CompletionRoutine)..c++

    作者的blog:猪)的网络编程世界 几个网络模型的示例代码代码包括了下面几个模型的示例:BlockingModel(阻塞模式).OverlappedModel(基于事件的重叠I/O).WSAEvent ...

  4. 洛谷P1525关押罪犯——并查集

    题目:https://www.luogu.org/problemnew/show/P1525 并查集+贪心,从大到小排序,将二人分在不同房间,找到第一个不满足的即为答案. 代码如下: #include ...

  5. Adobe Flash Player 27 on Fedora 27/26, CentOS/RHEL 7.4/6.9

    This is guide, howto install Adobe Flash Player Plugin version 27 (32-bit and 64-bit) with YUM/DNF o ...

  6. Oracle获取日期的特定部分

    (1)oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分 ,语法:extract ({ year | month | day ...

  7. 模拟select选择器

    <form method="post"> <div class="selectly" id="s1"> Select ...

  8. Swiper 滑动切换图片(可用于PC端,移动端)

    作为一名后端的普通程序猿, 你让我搞这种前端不是跟我玩命吗,所以用插件来搞,省事又简单,而且Swiper使用又简单是吧: 头皮发麻,不喜欢说废话,我更喜欢直接看到效果: 按Swiper官方文档来说, ...

  9. MFC中界面自适应

    void CMyDlg::OnSize(UINT nType, int cx, int cy){ CDialogEx::OnSize(nType, cx, cy); CRect rt; GetClie ...

  10. function multi-versioning in GCC

    https://lwn.net/Articles/691932/ https://gcc.gnu.org/wiki/FunctionMultiVersioning why multi-versioni ...