Portal

大致题意: 给定一个偶数长度(\(n \leq 10 ^ 5\))的字符串, 只包含大小写字母. 有q(\(q \leq 10 ^ 5\))次询问, 每次指定两个位置, 要求通过交换字符, 使这两个类型的字符在串同一边并且对于其他类型的字符, 不能跨过串的中线(也就是说必须在一边, 但是可以不跟指定的字符一边), 求方案数模\(1e9 + 7\)

Solution

这个题目很像atcoder啊

考虑去掉多余的状态, 事实上只有\(52 ^ 2 = 2704\)种状态, 其他的询问都是多余的.

考虑钦定两种字母,O(n)计算方案数. 发现答案是\(\frac{(\frac{n}{2})! ^ 2}{\prod {cnt_i !}}\) 那么就只需要考虑如何把剩下的50种字母塞进\(\frac{n}{2} - cnt_i - cnt_j\)里面去;

现在就转化成为如何计算背包方案数, 在不每次重新计算的情况下.

考虑背包具有子问题的自我概括性. 那么我们就只要从源头开始, 沿着转移方向一步一步消除影响即可, 这个套路也可以运用到一部分非可减性dp中, 当然, 如果能写成矩阵的话就不用这么麻烦了.

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
int read() {
char ch = getchar();
int x = 0, flag = 1;
for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(LL x) {
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + 48);
} const int Maxn = 100009, Maxk = 60, Mod = (int)1e9 + 7;
char s[Maxn];
int n, q, dp[Maxn], cnt[Maxk];
int predict[Maxk][Maxk], tmpDp[Maxn];
int fac[Maxn], invFac[Maxn]; int fpm(int base, int tims) {
int r = 1;
while (tims) {
if (tims & 1) r = 1ll * base * r % Mod;
base = 1ll * base * base % Mod;
tims >>= 1;
}
return r;
} void init() {
scanf("%s", s + 1); n = strlen(s + 1);
rep (i, 1, n)
if (isupper(s[i])) ++cnt[s[i] - 'A' + 1];
else ++cnt[s[i] - 'a' + 27]; /* Note */
dp[0] = 1;
rep (i, 1, 52) {
if (!cnt[i]) continue;
drep (j, n, cnt[i]) (dp[j] += dp[j - cnt[i]]) %= Mod;
}
/* Note */ fac[0] = 1;
rep (i, 1, n) fac[i] = fac[i - 1] * 1ll * i % Mod;
invFac[n] = fpm(fac[n], Mod - 2);
drep (i, n - 1, 0) invFac[i] = invFac[i + 1] * (i + 1ll) % Mod;
} void solve() {
rep (i, 1, 52)
rep (j, 1, i) {
if (!cnt[i] || !cnt[j]) continue;
rep (l, 0, n) tmpDp[l] = dp[l];
rep (l, cnt[i], n) {
(tmpDp[l] -= tmpDp[l - cnt[i]]) %= Mod;
(tmpDp[l] += Mod) %= Mod;
} if (i == j) {
predict[i][i] = tmpDp[n / 2];
continue;
} rep (l, cnt[j], n) {
(tmpDp[l] -= tmpDp[l - cnt[j]]) %= Mod;
(tmpDp[l] += Mod) %= Mod;
} predict[i][j] = predict[j][i] = tmpDp[n / 2];
} int W = fac[n / 2] * 1ll * fac[n / 2] % Mod;
rep (i, 1, 52)
if (cnt[i] > 0) W = 1ll * W * invFac[cnt[i]] % Mod; q = read();
rep (i, 1, q) {
int x = read(), y = read();
if (isupper(s[x])) x = s[x] - 'A' + 1; else x = s[x] - 'a' + 27;
if (isupper(s[y])) y = s[y] - 'A' + 1; else y = s[y] - 'a' + 27;
printf("%d\n", 2ll * W % Mod * predict[x][y] % Mod);
}
} int main() {
freopen("Cf1111D.in", "r", stdin);
freopen("Cf1111D.out", "w", stdout); init();
solve(); #ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return 0;
}

[CF1111D] Destory the Colony的更多相关文章

  1. [CF1111D]Destroy the Colony

    题目大意:有一个长度为$n(n\leqslant10^5,n=0\pmod2)$的字符串,字符集大小为$52$,有$q(q\leqslant10^5)$次询问,每次询问第$x,y$个字符在这个字符串的 ...

  2. C++中 destory() 和deallocate()以及delete函数的相关性和区别性

    这里非常的绕口  需要仔细的来看看: destory(): 显示调用一个对象的析构函数 相当于释放一个对象需要释放的一些动态内存 为下次真正释放对象做准备 deallocate():真正的释放一个内存 ...

  3. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  4. Delphi 对象的创建(create)与释放(free/destory)

    Delphi 对象的创建(create)与释放(free/destory) 1.Create参数为:nil/self/application的区别,最好能看到实际效果的区别 例如: My := TMy ...

  5. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  6. Unity Destory

    Object.Destroy     public static function Destroy(obj: Object, t: float = 0.0F): void; public static ...

  7. android点击返回键,如何做到不destory当前activity,只是stop。重新返回该activity的 时候可以直接使用,不需要创建新的activity实例

    问题描述,如题目: android点击返回键,顺序执行 pause,stop,destory. 以至于想重新进入这个activity的时候还要重新执行onCreate()方法,那么如何解决不再重新执行 ...

  8. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  9. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

随机推荐

  1. Protocol_BGP

    BGP协议 作者:Danbo 2015-7-8 BPG最重要的就是属性,下面我们针对路径属性分析一下.

  2. SSL peer shut down incorrectly

    这个问题通常出现在Android Studio更新失败的时候, 原因是download http://services.gradle.org/distributions/gradle-2.2-all. ...

  3. 【转】澄清P问题、NP问题、NPC问题

    首先,原文链接.(这篇文章让我第一次有了感谢腾讯,感谢微信,感谢微信公众号的冲动.总之,非常感谢作者的分享.) 然后:结论图如下 担心万一哪天原网站把这篇文章下线,所以原文内容复制过来. 澄清P问题. ...

  4. Python的GIL是什么鬼,多线程性能究竟如何

    前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号.本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几 ...

  5. Azkaban简介和使用

    概述 为什么需要工作流调度系统 l 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 l 各任务单元之间存在时间先后及前后依 ...

  6. Even Three is Odd

    题意: 问题是对于所有的长度为n,且$1<=ai<=n$的整数序列求 $\prod_{i=1}^{n-2}{max \{w_i,w_{i+1},w_{i+2}}\}$ 之和. 解法: 首先 ...

  7. junit4 Assert静态方法及API

    junit中的assert方法全部放在Assert类中. 1.assertTrue/False([String message,]boolean condition);    用来查看变量是是否为fa ...

  8. HDOj-1425

    sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. 1.1-1.5 flume架构概述及安装使用

    一.flume架构概述 1.flume简介 Flume是一种分布式,可靠且可用的服务,用于有效地收集,聚合和移动大量日志数据.它具有基于流数据流的简单灵活的架构.它具有可靠的可靠性机制和许多故障转移和 ...

  10. 7、html的body内标签之图片及表格

    一.image <a href="https://www.baidu.com" target="_blank"> <img src=" ...