Palindromic Subsets 数学 + 线段树
https://www.hackerrank.com/contests/101hack44/challenges/palindromic-subsets
如果有3个a。2个b。1个c。
每个a看成不同的,那么能选出多少个不同的回文串呢?
从回文串入手,因为回文串最多只有1种字母是奇数个。
那么,如果我能快速算区间[L, R]中各种字母出现的次数,就好了。
假设上面的数据,所得到的回文串是:
a取奇数个:2^2 * 2^1(b只能取偶数个) * 2^0(C是偶数个,这个时候是空集)
然后再枚举b取奇数个,其他取偶数个。
最后,还有一种情况,就是全部都是偶数个,这个时候因为可能选到的全部都是空集,所以最后结果要减去1.
接下来就是快速计算了。明显线段树,一开始不知道怎么维护区间,
其实区间更新,很简单,因为字母的数量是不会变的,对于区间存在2个a的话,反转1次,就只是2个b。
所以只需要枚举26个字母,cnt[(i + k) % 26] = cnt[i]
意思是产生这个字母的个数是有它来产生的。然后线段树更新即可。
线段树写了很多次,看模板才想起怎么lazy--update 苦逼。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define root 1, n, 1
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1 const int maxn = 1e5 + ;
char str[maxn];
const int MOD = 1e9 + ;
char to[];
int add[maxn << ];
LL quick_pow(LL a, LL b, int MOD) {
LL ans = ;
assert(b >= );
while (b) {
if (b & ) {
ans = ans * a;
if (ans >= MOD) ans %= MOD;
}
b >>= ;
a *= a;
if (a >= MOD) a %= MOD;
}
return ans;
}
struct node {
int cnt[ + ];
} seg[maxn << ];
void toget(struct node &a, int val) {
int cnt[ + ] = {};
for (int i = ; i < ; ++i) {
cnt[i] = a.cnt[i];
}
for (int i = ; i < ; ++i) {
a.cnt[(i + val) % ] = cnt[i];
}
return;
}
void pushUp(int cur) {
for (int i = ; i < ; ++i) {
seg[cur].cnt[i] = seg[cur << ].cnt[i] + seg[cur << | ].cnt[i];
}
}
void pushDown(int cur) {
if (add[cur]) {
add[cur << | ] += add[cur];
add[cur << | ] %= ;
add[cur << ] += add[cur];
add[cur << ] %= ;
toget(seg[cur << ], add[cur]);
toget(seg[cur << | ], add[cur]);
add[cur] = ;
}
}
void build(int L, int R, int cur) {
if (L == R) {
seg[cur].cnt[str[L] - 'a'] = ;
return;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
void upDate(int be, int en, int val, int L, int R, int cur) {
if (L >= be && R <= en) {
toget(seg[cur], val);
add[cur] += val;
add[cur] %= ;
return;
}
pushDown(cur);
int mid = (L + R) >> ;
if (mid >= be) upDate(be, en, val, lson);
if (mid < en) upDate(be, en, val, rson);
pushUp(cur);
}
int query(int be, int en, int ch, int L, int R, int cur) {
if (L >= be && R <= en) {
return seg[cur].cnt[ch];
}
pushDown(cur);
int mid = (L + R) >> ;
int lans = , rans = ;
if (mid >= be) lans = query(be, en, ch, lson);
if (mid < en) rans = query(be, en, ch, rson);
return lans + rans;
}
void work() {
for (int i = 'a'; i <= 'z' - ; ++i) {
to[i] = i + ;
}
to['z'] = 'a';
int n, q;
scanf("%d%d", &n, &q);
scanf("%s", str + );
build(root);
while (q--) {
int flag, L, R;
scanf("%d", &flag);
if (flag == ) {
scanf("%d%d", &L, &R);
L++;
R++;
LL ans = ;
int len = ;
for (int i = ; i < ; ++i) {
int ret = query(L, R, i, root);
if (ret > ) len++;
else continue;
ans = ans * quick_pow(, ret - , MOD);
if (ans >= MOD) ans %= MOD;
}
ans *= (len + );
ans %= MOD;
ans = (ans - + MOD) % MOD;
cout << ans << endl;
} else {
int t;
scanf("%d%d%d", &L, &R, &t);
L++;
R++;
upDate(L, R, t % , root);
// printf("%d****\n", query(3, 3, 'u' - 'a', root));
}
}
// cout << query(1, n, 'o' - 'a', root) << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
Palindromic Subsets 数学 + 线段树的更多相关文章
- Codeforces 311D Interval Cubing 数学 + 线段树 (看题解)
Interval Cubing 这种数学题谁顶得住啊. 因为 (3 ^ 48) % (mod - 1)为 1 , 所以48个一个循环节, 用线段树直接维护. #include<bits/stdc ...
- 2017ICPC北京赛区网络赛 Minimum(数学+线段树)
描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...
- [CSP-S模拟测试]:最大值(数学+线段树)
题目背景 $Maxtir$最喜欢最大值. 题目传送门(内部题128) 输入格式 第$1$行输入四个正整数$n,m,q$. 第$2$至$n+1$行中,第$i+1$行输入魔法晶石$i$的三种属性$(x_i ...
- [CSP-S模拟测试]:旅行(数学+线段树)
题目传送门(内部题12) 输入格式 第一行,一个整数$n$,代表树的点数.第二行,$n$个整数,第$i$个整数是$B_i$,描述排列$B$.接下来$n−1$行,每行两个整数$u,v$,描述一条树边$( ...
- Sasha and a Very Easy Test CodeForces - 1109E (数学,线段树)
大意: 给定n元素序列, q个操作: (1)区间乘 (2)单点除(保证整除) (3)区间求和对m取模 要求回答所有操作(3)的结果 主要是除法难办, 假设单点除$x$, $x$中与$m$互素的素因子可 ...
- BZOJ 5334--[Tjoi2018]数学计算(线段树)
5334: [Tjoi2018]数学计算 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 220 Solved: 147[Submit][Status ...
- 【BZOJ5334】数学计算(线段树)
[BZOJ5334]数学计算(线段树) 题面 BZOJ 洛谷 题解 简单的线段树模板题??? 咕咕咕. #include<iostream> #include<cstdio> ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- 【线段树/数学/扩展欧几里得】 Bzoj 3913:奇数国
Description 在一片美丽的大陆上有100000个国家,记为1到100000.这里经济发达,有数不尽的账房,并且每个国家有一个银行.某大公司的领袖在这100000个银行开户时都存了3大洋,他惜 ...
随机推荐
- 【转】SQL中的取整函数FLOOR、ROUND、CEIL、TRUNC、SIGN
--------------------------------------------------------------------------1 trunc(value,precision)按精 ...
- 【Jquery】jQuery获取URL參数的两种方法
jQuery获取URL參数的关键是获取到URL,然后对URL进行过滤处理,取出參数. location.href是取得URL.location.search是取得URL"?"之后的 ...
- poj2488--A Knight's Journey(dfs,骑士问题)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 31147 Accepted: 10 ...
- UIwebView缩放
首先就是需要让webView去设置下可以支持缩放 [__webView setScalesPageToFit:YES]; 如果网页支持缩放只需要上面的一句就可以了.你可以加在谷歌的试一下,但是你要加在 ...
- [IT新应用]brave浏览器
https://www.brave.com/about.html The web has become a different place. With the ad-tech ecosystem ou ...
- JSON: Circular Dependency Errors
If you’re using Object Relational Mapping frameworks like Hibernate, and are using the bi-directiona ...
- HBase使用教程
1 基本介绍 1.1 前言 HBase – Hadoop Database.是一个分布式的.面向列的开源数据库,该技术来源于 Fay Chang 所撰写的Google论文"Bigta ...
- CRM 插件奇怪的报错
CRM插件,数据库方式注册.报错 找不到方法:“Void Microsoft.Xrm.Sdk.Entity..ctor(System.String, System.Guid)”. 这个错误让人摸不着头 ...
- YTU 1074: You are my brother
1074: You are my brother 时间限制: 1 Sec 内存限制: 128 MB 提交: 10 解决: 7 题目描述 Little A gets to know a new fr ...
- 并不对劲的bzoj1853:[SCOI2010]幸运数字
传送门-> 据说本题的正确读法是[shìng运数字]. 听上去本题很适合暴力,于是并不对劲的人就去写了.其实这题就是一个很普(有)通(趣)暴力+神奇的优化. 首先,会发现幸运数字很少,那么就先搜 ...