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大洋,他惜 ...
随机推荐
- Native进程之Trace原理(转)——可直接输出某进程的栈帧——debuggerd
一. 概述 当发生ANR(Application Not Response,对于Java进程可通过kill -3向目标进程发送信号SIGNAL_QUIT, 输出相应的traces信息保存到目录/dat ...
- hdu 3853(数学期望入门)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3853 LOOPS Time Limit: 15000/5000 MS (Java/Others) ...
- struts2的文件上传机制
Struts2的上传(基本流程例如以下) 1.Struts2默认採用了apache commons-fileupload 2.Struts2支持三种类型的上传组件 3.须要引入commons-file ...
- Android之——AIDL深入
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47071927 在上一篇博文<Android之--AIDL小结>中,我们 ...
- ASP.NET MVC中为DropDownListFor设置选中项的方法
在MVC中,当涉及到强类型编辑页,如果有select元素,需要根据当前Model的某个属性值,让Select的某项选中.本篇只整理思路,不涉及完整代码. □ 思路 往前台视图传的类型是List< ...
- tomcat项目重复加载问题
主要是通过配置<Tomcat安装目录>/conf/server.xml文件 步骤: 1.打开server.xml,在</Host>的上一行添加内容格式如下 <Contex ...
- rm -rf / – Deletes Everything!
https://www.howtogeek.com/125157/8-deadly-commands-you-should-never-run-on-linux/
- zTree 基本用法
[简介] zTree 是利用 JQuery 的核心代码,实现一套能完成大部分常用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同时生成多个 Tree 实例 ...
- VS2010调用外部webservice
vs2010怎么调用web服务webservice方法,以vs2010为例.Vs的各个版本的此项功能操作基本一致. 工具/原料 vs2010 在“服务引用设置”对话框中,单击“添加 Web 引用”. ...
- 设计模式-(9)中介者模式(swift)
在对象去耦合的模式中,有两种模式:中介者模式,观察者模式 一,概念 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 这个 ...