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[ ...
随机推荐
- python无法安装cv2的解决办法
问题:在windows命令窗口输入pip install cv2后出现:Could not find a version that satisfies the requirement cv2... 解 ...
- 标准兼容HTML5输入框提示信息的插件iHolder_v0.1.06.21.2014_预览版
由于版面限制,简单说下,详细的内容及在线预览.预览版压缩包,见这里http://levi.cg.am/archives/3507 为什么说是标准兼容: 因为大多数placeholder插件是这样兼容的 ...
- SFTP 文件上传下载引用代码
http://sha1064616837.iteye.com/blog/2036996 http://www.cnblogs.com/itmanxgl/p/fe5d33512609fe540eb08a ...
- PS 滤镜— —球面化效果
clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorithm'); I=imread ...
- JSP 使用<%@include%>报Duplicate local variable path 错误 解决方法
错误提示:Multiple annotations found at this line:- Duplicate local variable path- Duplicate local variab ...
- 「LuoguP1725」琪露诺(dp 单调队列
题目描述 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精. 某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这只青蛙比以往的要聪明许多,在琪露诺来之前就已经跑到了河的对岸.于是琪露诺决定到河岸去追 ...
- 2084. Asm.Def的基本算法
2084. Asm.Def的基本算法 传送门 ★☆ 输入文件:asm_algo.in 输出文件:asm_algo.out 简单对比时间限制:1 s 内存限制:256 MB [题目描述] ...
- fabric添加多主机ssh互信
最近折腾fabric,把服务器ssh互信用fabric写了一遍,单向互信,master可以无密码访问client,具体如下: 执行:fab -f ./copyrsa.py allsshkey 即可, ...
- MMU的理解
MMU内存管理单元相关知识点总结 1.MMU是Memory Management Unit的缩写,中文名是内存管理单元,它是中央处理器(CPU)中用来管理虚拟存储器.物理存储器的控制线路,同时也负责虚 ...
- GroupItem ContextMenu Command mvvm
参照:https://stackoverflow.com/questions/38009642/how-do-i-bind-the-menuitem-of-a-contextmenu-of-a-dat ...