codeforces 666C Codeword
codeforces 666C Codeword
题意
q个询问,一种询问是给你一个字符串,还有一种是问长度为n的,包含当前字符串为子序列的字符串有多少个。
题解
容易写出式子,但是不好化简。
观察一下可以知道q个询问的字符串长度也就根号种。
代码
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define sz(a) (int)a.size()
#define de(a) cout << #a << " = " << a << endl
#define dd(a) cout << #a << " = " << a << " "
#define all(a) a.begin(), a.end()
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
//---
const int N = 101010, P = 1e9+7;
int pw[N], jc[N], in[N], ans[N];
vector<pii> Q[N];
inline int mul(int a, int b) {
return a * 1ll * b % P;
}
inline int add(int a, int b) {
int res = a+b;
if(res >= P) res -= P;
return res;
}
inline int kpow(int a, int b) {
int res = 1;
while(b) {
if(b&1) res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
inline void init() {
pw[0] = 1;
rep(i, 1, N) pw[i] = mul(pw[i-1], 25);
jc[0] = 1;
rep(i, 1, N) jc[i] = mul(jc[i-1], i);
in[N-1] = kpow(jc[N-1], P-2);
for(int i = N-2; ~i; --i) in[i] = mul(in[i+1], i+1);
}
inline int C(int n, int m) {
if(n < m) return 0;
return mul(jc[n], mul(in[m], in[n-m]));
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
init();
int n, m, q;
string s;
cin >> q >> s;
m = sz(s);
rep(i, 0, q) {
int x;
cin >> x;
if(x == 1) {
cin >> s;
m = sz(s);
} else {
cin >> n;
Q[m].pb(mp(n, i));
}
}
rep(m, 0, N) if(sz(Q[m])) {
sort(all(Q[m]));
int i = m, res = 1;
for(auto t : Q[m]) {
int n = t.fi;
while(i < n) {
++i;
res = mul(res, 26);
res = add(res, mul(C(i-1, m-1), pw[i - m]));
}
ans[t.se] = n >= m ? res : 0;
++ans[t.se];
}
}
rep(i, 0, q) if(ans[i]) {
cout << ans[i]-1 << endl;
}
return 0;
}
codeforces 666C Codeword的更多相关文章
- Codeword CodeForces - 666C (字符串计数)
链接 大意:求只含小写字母, 长度为n, 且可以与给定模板串匹配的字符串个数 (多组数据) 记模板串为P, 长为x, 总串为S. 设$f_i$为S为i时的匹配数, 考虑P最后一位的首次匹配位置. 若为 ...
- cf666 C. Codeword 组合数学 离线分块思想
time limit per test 6 seconds memory limit per test 256 megabytes input standard i ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
随机推荐
- [转]SAP模块一句话入门
本文转自:http://www.cnblogs.com/mybi/archive/2010/12/20/1911154.html SAP一句话入门:Financial & Controllin ...
- 线程不安全的类不要轻易做为static变量使用,及如何使用ThreadLocal将共享变量变为独享变量
参考原文:<关于SimpleDateFormat安全的时间格式化线程安全问题>
- XML修改节点值
基于DOM4J 先获取根节点 doc.getRootElement() 然后获取需要修改的节点 doc.getRootElement().node(int) 重新赋值 doc.getRootEleme ...
- 关于java异常try catch finally的一道题
这两天,有人咨询我一道关于java基础的题,具体代码如下: private static int m1() { int a = 10; try { a = 20; throw new RuntimeE ...
- OutOfMemoryError(内存溢出)解决办法
第一种OutOfMemoryError: PermGen space 发生这种问题的原意是程序中使用了大量的jar或class,使java虚拟机装载类的空间不够,与Permanent Generati ...
- Linux之Ubuntu基本命令提炼,分条列出
Ubuntu系统的root用户有时没有安装,我们可以先输入一个root,他会有一个提示命令,然后我们输入该命令,进行安装,安装完后,使用sudopasswd 命令设置密码,设置完后的密码就是root用 ...
- Mac下使用sublime Text打开隐藏目录
我们用 sublime Text 打开时,默认是看到非隐藏的目录和文件,如下图: 这时候在这个节目,按下 command +shift + 句号 快捷键,会自动切换 隐藏状态的, 这时候就可以切换成下 ...
- bnu 4060 奇偶性,异或运算
Plants vs. Zombies Time Limit: 5000ms Memory Limit: 2048KB 64-bit integer IO format: %lld Jav ...
- Struts2 (三) — OGNL与值栈
一.OGNL表达式 1.概述 1.1什么是OGNL OGNL是Object-Graph Navigation Language的缩写,俗称对象图导航语言. 它是一种功能强大的表达式语言,通过它简单 ...
- 创建一个Dribbble的作品展示
Most designers on dribbble have a personal portfolio website that usually consists of a name and a b ...