要点

  • 优质题解
  • 因为只有某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. (转)epoll非阻塞读写规则

    EPOLL技术 在linux的网络编程中,很长的时间都在使用select来做事件触发.在linux新的内核中,有了一种替换它的机制,就是epoll.相比于select,epoll最大的好处在于它不会随 ...

  2. 【译】在ES6中如何优雅的使用Arguments和Parameters

    原文地址:how-to-use-arguments-and-parameters-in-ecmascript-6 ES6是最新版本的ECMAScript标准,而且显著的改善了JS里的参数处理.我们现在 ...

  3. python 列表之队列

    列表实现队列操作(FIFO),可以使用标准库里的 collections.deque,deque是double-ended quene的缩写,双端队列的意思,它可以实现从队列头部快速增加和取出对象. ...

  4. k8s 部署kube-dns

    [root@k8s-master src]# kubectl create -f kube-dns.yaml service "kube-dns" createdserviceac ...

  5. MySQL记录_20160919

    1.首先先看下什么是MySQL. MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 最流行的关系型数据库管理系统,其开放源码这一特点 ...

  6. JavaScript:Map使用

    定义Map /** * Map * */ function Map() { /** 存放键的数组(遍历用到) */ this.keys = new Array(); /** 存放数据 */ this. ...

  7. DataWindow.Net V2.5原始文件下载

    DW2.5 原始安装程序下载 http://download.sybase.com/eval/datawindowNET_25_eval/CD50090-55-0250-01.zip

  8. Spring Web MVC 的HandlerMapping的使用之-------SimpleUrlHandlerMapping

    转自:https://www.2cto.com/kf/201401/271141.html Spring MVC教程 映射处理器Handler Mapping 一.简析映射处理器 在spring mv ...

  9. Connection reset by peer的常见原因及解决办法

    转自:https://blog.csdn.net/xc_zhou/article/details/80950753 1,如果一端的Socket被关闭(或主动关闭,或因为异常退出而 引起的关闭),另一端 ...

  10. python脚本修改hosts文件

    用python3写了个修改hosts的小脚本,没有技术含量,顺带复习一下文件操作和os.sys模块的内容了. # -*- coding:utf-8 -*- #!/usr/bin/env python ...