# Palindromic Subsets

### Problem Statement :

```Consider a lowercase English alphabetic letter character denoted by c. A shift operation on some c turns it into the next letter in the alphabet. For example, and ,shift(a) = b , shift(e) = f, shift(z) = a .

Given a zero-indexed string, s, of n lowercase letters, perform q queries on s where each query takes one of the following two forms:

1 i j t: All letters in the inclusive range from i to j are shifted t times.
2 i j: Consider all indices in the inclusive range from i to j. Find the number of non-empty subsets of characters c1, c2, . . . , ck ,  where , i  <  index of  c1  <  index of c2 < . . . < index of ck <  j )   such that characters  c1, c2, . . . , ck , can be rearranged to form a palindrome. Then print this number modulo 10^9 + 7 on a new line. Two palindromic subsets are considered to be different if their component characters came from different indices in the original string.
Note Two palindromic subsets are considered to be different if their component characters came from different indices in the original string.

Input Format

The first line contains two space-separated integers describing the respective values of n and q.
The second line contains a string of n lowercase English alphabetic letters (i.e., a through z) denoting s.
Each of the q subsequent lines describes a query in one of the two formats defined above.

Output Format

For each query of type 2 (i.e., 2 i j), print the number of non-empty subsets of characters satisfying the conditions given above, modulo 10 ^ 9 + 7, on a new line.```

### Solution :

```                            ```Solution in C :

In  C ++ :

#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int MAX = 100100;
const int off = 1<<17;

const int mod = 1e9 + 7;

inline int add(int a, int b) {
return a+b >= mod ? a+b-mod : a+b;
}

inline int sub(int a, int b) {
return a >= b ? a-b : a-b+mod;
}

inline int mul(int a, int b) {
return llint(a)*b % mod;
}

vector<int> T[2*off];
int S[2*off];

void sh(int i, int x) {
S[i] = (S[i] + x) % 26;
rotate(T[i].begin(), T[i].begin() + ((26-x) % 26), T[i].end());
}

void propagate(int i) {
if (S[i]) {
sh(i*2, S[i]);
sh(i*2+1, S[i]);
S[i] = 0;
}
}

vector<int> merge(const vector<int>& a, const vector<int>& b) {
vector<int> c(26);
REP(i, 26) c[i] = a[i] + b[i];
return c;
}

void shift(int i, int lo, int hi, int a, int b, int k) {
if (lo >= b || hi <= a) return;
if (lo >= a && hi <= b) { sh(i, k); return; }
propagate(i);

shift(i*2, lo, (lo+hi)/2, a, b, k);
shift(i*2+1, (lo+hi)/2, hi, a, b, k);

T[i] = merge(T[i*2], T[i*2+1]);
}

vector<int> count(int i, int lo, int hi, int a, int b) {
if (lo >= b || hi <= a) return vector<int>(26, 0);
if (lo >= a && hi <= b) return T[i];
propagate(i);
return merge(count(i*2, lo, (lo+hi)/2, a, b), count(i*2+1, (lo+hi)/2, hi, a, b));
}

char s[MAX];
int pw[MAX];

int main(void) {
pw = 1;
FOR(i, 1, MAX) pw[i] = mul(pw[i-1], 2);

int n, q;
scanf("%d %d", &n, &q);
scanf("%s", s);
REP(i, 2*off) T[i].resize(26);
REP(i, n) T[off+i][s[i]-'a']++;

for (int i = off-1; i >= 0; --i) REP(j, 26) T[i][j] = T[2*i][j] + T[2*i+1][j];

REP(i, q) {
int tip;
scanf("%d", &tip);

if (tip == 1) {
int a, b, t;
scanf("%d %d %d", &a, &b, &t);
t %= 26;
shift(1, 0, off, a, b+1, t);
}
if (tip == 2) {
int a, b;
scanf("%d %d", &a, &b);
vector<int> v = count(1, 0, off, a, b+1);

int ans = 0;
REP(j, 27) {
int ways = 1;
REP(k, 26)
if (v[k] && k != j) ways = mul(ways, pw[v[k]-1]);
if (j != 26) {
if (v[j] == 0) ways = 0;
else ways = mul(ways, pw[v[j]-1]);
}
}
ans = sub(ans, 1);
printf("%d\n", ans);
}
}
return 0;
}

In  C :

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

#define M 1000000007

char s;
int ret;
int d[1<<18];
int f;

void init(int t,int l,int r) {
int i;
if (r-l==1) {
d[t][s[l]-'a']++;
return ;
}
init(t<<1,l,(l+r)/2);
init(t<<1|1,(l+r)/2,r);
for(i=0;i<26;i++) d[t][i]=d[t<<1][i]+d[t<<1|1][i];
}

void shift(int a[],int t) {
int b,i;
for(i=0;i<26;i++) b[(i+t)%26]=a[i];
for(i=0;i<26;i++) a[i]=b[i];
}

void update(int t,int l,int r,int a,int b,int val) {
if (l==a && r==b) {
d[t]=(d[t]+val)%26;
return ;
}
shift(d[t],d[t]);
d[t<<1]+=d[t];
d[t<<1|1]+=d[t];
d[t]=0;
if (b<=(l+r)/2) {
update(t<<1,l,(l+r)/2,a,b,val);
} else if (a>=(l+r)/2) {
update(t<<1|1,(l+r)/2,r,a,b,val);
} else {
update(t<<1,l,(l+r)/2,a,(l+r)/2,val);
update(t<<1|1,(l+r)/2,r,(l+r)/2,b,val);
}
int i;
for(i=0;i<26;i++) d[t][i]=0;
for(i=0;i<26;i++) d[t][(i+d[t<<1])%26]+=d[t<<1][i];
for(i=0;i<26;i++) d[t][(i+d[t<<1|1])%26]+=d[t<<1|1][i];
}

void query(int t,int l,int r,int a,int b) {
//    printf("%d %d %d %d %d %d %d\n",t,l,r,d[t],d[t],d[t],d[t]);
if (l==a && r==b) {
int i;
for(i=0;i<26;i++) ret[(i+d[t])%26]+=d[t][i];
return ;
}
shift(d[t],d[t]);
d[t<<1]+=d[t];
d[t<<1|1]+=d[t];
d[t]=0;
if (b<=(l+r)/2) {
query(t<<1,l,(l+r)/2,a,b);
} else if (a>=(l+r)/2) {
query(t<<1|1,(l+r)/2,r,a,b);
} else {
query(t<<1,l,(l+r)/2,a,(l+r)/2);
query(t<<1|1,(l+r)/2,r,(l+r)/2,b);
}
}

int main(){
int n,m,i;
scanf("%d %d",&n,&m);
scanf("%s",s);
init(1,0,n);
for(f=i=1;i<=n;i++) if ((f[i]=f[i-1]+f[i-1])>=M) f[i]-=M;
for(;m--;) {
int op,l,r,val;
scanf("%d %d %d",&op,&l,&r);
if (op==1) {
scanf("%d",&val);
update(1,0,n,l,r+1,val);
} else {
int d1=1,d2=0;
memset(ret,0,sizeof(ret));
query(1,0,n,l,r+1);
//   printf("%d %d %d\n",ret,ret,ret);
for(i=0;i<26;i++) {
if (!ret[i]) continue;
d2=(long long)(d2+d1)*f[ret[i]-1]%M;
d1=(long long)d1*f[ret[i]-1]%M;
}
if ((d1+=d2-1)>=M) d1-=M;
printf("%d\n",d1);
}
}
return 0;
}

In Python3 :

#!/bin/python3

import sys

n,q = input().strip().split(' ')
n,q = [int(n),int(q)]
s = input().strip()
sa = [ord(i) - ord('a') for i in s]
for a0 in range(q):
qu = input().strip().split(' ')
if int(qu) == 1:
start = int(qu)
end =int(qu)
t = int(qu)
for i in range(start, end + 1):
sa[i] = (sa[i] + t) % 26
else:
start = int(qu)
end =int(qu)
d =  * 27
for i in range(start, end + 1):
d[sa[i]] += 1
ans = 1
for i in range(27):
if d[i]:
ans = (ans * pow(2, d[i] - 1) )% 1000000007
g = ans
ans = (ans - 1 )% 1000000007
for i in range(27):
if d[i]:
ans = (ans + g) % 1000000007

print(ans)

In Java :

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class AliceBobSillyGames {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int g = in.nextInt();
for(int a0 = 0; a0 < g; a0++){
int n = in.nextInt();
getSetArray(n);
}
}
static void getSetArray(int n) {
int [] numberSet = new int[n];

for(int i = 1; i <= n; i++) {
numberSet[i-1] = i;
}

int flag = 0;
for(int i = 2; i <= n; i++) {

if(isPrime(i)) {
/* System.out.print(i +" ");*/
int k = 1;
while(k <= n) {
if(i*k - 1 < n) {
numberSet[i*k - 1] = 0;
}
k++;
//k = k*i;
}
flag++;
}
}

if(flag % 2 == 0) {
System.out.println("Bob");
}
else {
System.out.println("Alice");
}

}

static public boolean isPrime(long num) {
if ( num < 2 ) return false;
for (int i = 2; i <= Math.sqrt(num); i++) {
if ( num % i == 0 ) {
return false;
}
}
return true;
}
}```
```

## Binary Search Tree : Lowest Common Ancestor

You are given pointer to the root of the binary search tree and two values v1 and v2. You need to return the lowest common ancestor (LCA) of v1 and v2 in the binary search tree. In the diagram above, the lowest common ancestor of the nodes 4 and 6 is the node 3. Node 3 is the lowest node which has nodes and as descendants. Function Description Complete the function lca in the editor b

## Swap Nodes [Algo]

A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from

## Kitty's Calculations on a Tree

Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a

## Is This a Binary Search Tree?

For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a

## Square-Ten Tree

The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the

## Balanced Forest

Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a