JerryL/AlgoSimBench huggingface dataset:
1326_D2. Prefix-Suffix Palindrome (Hard version)

This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task.

You are given a string s, consisting of lowercase English letters. Find the longest string, t, which satisfies the following conditions: 

  * The length of t does not exceed the length of s. 
  * t is a palindrome. 
  * There exists two strings a and b (possibly empty), such that t = a + b ( "+" represents concatenation), and a is prefix of s while b is suffix of s. 

Input

The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5), the number of test cases. The next t lines each describe a test case.

Each test case is a non-empty string s, consisting of lowercase English letters.

It is guaranteed that the sum of lengths of strings over all test cases does not exceed 10^6.

Output

For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them.

Example

Input


5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba


Output


a
abcdfdcba
xyzyx
c
abba

Note

In the first test, the string s = "a" satisfies all conditions.

In the second test, the string "abcdfdcba" satisfies all conditions, because:

  * Its length is 9, which does not exceed the length of the string s, which equals 11. 
  * It is a palindrome. 
  * "abcdfdcba" = "abcdfdc" + "ba", and "abcdfdc" is a prefix of s while "ba" is a suffix of s. 



It can be proven that there does not exist a longer string which satisfies the conditions.

In the fourth test, the string "c" is correct, because "c" = "c" + "" and a or b can be empty. The other possible solution for this test is "s".

Example input:
5
a
abcdfdcecba
abbaxyzyx
codeforces
acbba


Example output:
a
abcdfdcba
xyzyx
c
abba


Tags: binary search
greedy
hashing
string suffix structures
strings
Difficulty rating: 1800

Solution:
def pre(s):
	p = s + '#' + s[::-1]
	pi = [0] * len(p)
	j = 0
	for i in range(1, len(p)):
		while j != 0 and p[j] != p[i]:
			j = pi[j - 1]
		if p[j] == p[i]:
			j += 1
		pi[i] = j
	return s[:j]

t = int(input())
for i in range(t):
	s = input()

	l = 0
	while l < len(s) // 2 and s[l] == s[-(l + 1)]:
		l += 1

	pp = pre(s[l:len(s)-l])
	sp = pre(s[l:len(s)-l][::-1])
	pp = pp if len(pp) > len(sp) else sp

	print(s[:l] + pp + s[len(s)-l:])
17_E. Palisection

In an English class Nick had nothing to do at all, and remembered about wonderful strings called palindromes. We should remind you that a string is called a palindrome if it can be read the same way both from left to right and from right to left. Here are examples of such strings: «eye», «pop», «level», «aba», «deed», «racecar», «rotor», «madam». 

Nick started to look carefully for all palindromes in the text that they were reading in the class. For each occurrence of each palindrome in the text he wrote a pair — the position of the beginning and the position of the ending of this occurrence in the text. Nick called each occurrence of each palindrome he found in the text subpalindrome. When he found all the subpalindromes, he decided to find out how many different pairs among these subpalindromes cross. Two subpalindromes cross if they cover common positions in the text. No palindrome can cross itself.

Let's look at the actions, performed by Nick, by the example of text «babb». At first he wrote out all subpalindromes:

• «b» — 1..1 • «bab» — 1..3 • «a» — 2..2 • «b» — 3..3 • «bb» — 3..4 • «b» — 4..4

Then Nick counted the amount of different pairs among these subpalindromes that cross. These pairs were six:

1. 1..1 cross with 1..3 2. 1..3 cross with 2..2 3. 1..3 cross with 3..3 4. 1..3 cross with 3..4 5. 3..3 cross with 3..4 6. 3..4 cross with 4..4

Since it's very exhausting to perform all the described actions manually, Nick asked you to help him and write a program that can find out the amount of different subpalindrome pairs that cross. Two subpalindrome pairs are regarded as different if one of the pairs contains a subpalindrome that the other does not.

Input

The first input line contains integer n (1 ≤ n ≤ 2·106) — length of the text. The following line contains n lower-case Latin letters (from a to z).

Output

In the only line output the amount of different pairs of two subpalindromes that cross each other. Output the answer modulo 51123987.

Examples

Input

4
babb


Output

6


Input

2
aa


Output

2

Example input:
4
babb

2
aa


Example output:
6

2


Tags: strings
Difficulty rating: 2900

Solution:
#include <bits/stdc++.h>
const int p = 51123987, N = 4e6 + 7;
using namespace std;
int n, m = 1, f[N], l[N], r[N], ans;
char a[N], b[N];
int main() {
  scanf("%d%s", &n, a), b[0] = 1;
  for (int i = 0; i < n; i++, m++) b[++m] = a[i];
  for (int i = 1, p = 0, q = 0; i <= m; i++) {
    f[i] = q > i ? min(f[2 * p - i], q - i) : 1;
    while (b[i + f[i]] == b[i - f[i]]) f[i]++;
    if (i + f[i] > q) p = i, q = i + f[i];
  }
  for (int i = 1; i <= m; i++)
    l[i - f[i] + 1]++, l[i + 1]--, r[i]++, r[i + f[i]]--,
        (ans += f[i] / 2) %= p;
  ans = 1ll * ans * (ans - 1) / 2 % p;
  for (int i = 1, s = 0; i <= m; i++) {
    l[i] += l[i - 1], r[i] += r[i - 1];
    if (i % 2 == 0) (ans -= 1ll * s * l[i] % p) %= p, (s += r[i]) %= p;
  }
  printf("%d\n", (ans + p) % p);
}
1202_E. You Are Given Some Strings...

You are given a string t and n strings s_1, s_2, ..., s_n. All strings consist of lowercase Latin letters.

Let f(t, s) be the number of occurences of string s in string t. For example, f('aaabacaa', 'aa') = 3, and f('ababa', 'aba') = 2.

Calculate the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j), where s + t is the concatenation of strings s and t. Note that if there are two pairs i_1, j_1 and i_2, j_2 such that s_{i_1} + s_{j_1} = s_{i_2} + s_{j_2}, you should include both f(t, s_{i_1} + s_{j_1}) and f(t, s_{i_2} + s_{j_2}) in answer.

Input

The first line contains string t (1 ≤ |t| ≤ 2 ⋅ 10^5).

The second line contains integer n (1 ≤ n ≤ 2 ⋅ 10^5).

Each of next n lines contains string s_i (1 ≤ |s_i| ≤ 2 ⋅ 10^5).

It is guaranteed that ∑_{i=1}^{n} |s_i| ≤ 2 ⋅ 10^5. All strings consist of lowercase English letters.

Output

Print one integer — the value of ∑_{i=1}^{n} ∑_{j=1}^{n} f(t, s_i + s_j).

Examples

Input


aaabacaa
2
a
aa


Output


5


Input


aaabacaa
4
a
a
a
b


Output


33

Example input:
aaabacaa
4
a
a
a
b

aaabacaa
2
a
aa


Example output:
33

5


Tags: brute force
string suffix structures
strings
Difficulty rating: 2400

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
char s[N], t[N];
int dp[2][N], q[N], n, m, len, i, j;
long long ans;
struct AC {
  int a[N][26], nxt[N], d[N], sz = 1;
  void ins(char s[N]) {
    int now = 1;
    for (int i = 0; s[i]; ++i) {
      if (!a[now][s[i] - 'a']) a[now][s[i] - 'a'] = ++sz;
      now = a[now][s[i] - 'a'];
    }
    d[now]++;
  }
  void acmach() {
    for (int i = 0; i < 26; ++i) a[0][i] = 1;
    int t = 0, w = 1;
    q[0] = 1;
    while (t != w) {
      int now = q[t++];
      for (int i = 0; i < 26; ++i)
        if (a[now][i]) {
          nxt[a[now][i]] = a[nxt[now]][i];
          d[a[now][i]] += d[nxt[a[now][i]]];
          q[w++] = a[now][i];
        } else
          a[now][i] = a[nxt[now]][i];
    }
  }
  void go(int dp[N]) {
    for (int now = 1, i = 0; i < n; ++i)
      now = a[now][s[i] - 'a'], dp[i] += d[now];
  }
} c1, c2;
int main() {
  for (scanf("%s%d", s, &m), n = strlen(s); m--;)
    scanf("%s", t), len = strlen(t), c1.ins(t), reverse(t, t + len), c2.ins(t);
  c1.acmach();
  c2.acmach();
  c1.go(dp[0]);
  reverse(s, s + n);
  c2.go(dp[1]);
  for (i = 1; i < n; ++i) ans += 1LL * dp[0][i - 1] * dp[1][n - i - 1];
  printf("%lld\n", ans);
}
1140_F. Extending Set of Points

For a given set of two-dimensional points S, let's denote its extension E(S) as the result of the following algorithm:

Create another set of two-dimensional points R, which is initially equal to S. Then, while there exist four numbers x_1, y_1, x_2 and y_2 such that (x_1, y_1) ∈ R, (x_1, y_2) ∈ R, (x_2, y_1) ∈ R and (x_2, y_2) ∉ R, add (x_2, y_2) to R. When it is impossible to find such four integers, let R be the result of the algorithm.

Now for the problem itself. You are given a set of two-dimensional points S, which is initially empty. You have to process two types of queries: add some point to S, or remove some point from it. After each query you have to compute the size of E(S).

Input

The first line contains one integer q (1 ≤ q ≤ 3 ⋅ 10^5) — the number of queries.

Then q lines follow, each containing two integers x_i, y_i (1 ≤ x_i, y_i ≤ 3 ⋅ 10^5), denoting i-th query as follows: if (x_i, y_i) ∈ S, erase it from S, otherwise insert (x_i, y_i) into S.

Output

Print q integers. i-th integer should be equal to the size of E(S) after processing first i queries.

Example

Input


7
1 1
1 2
2 1
2 2
1 2
1 3
2 1


Output


1 2 4 4 4 6 3 

Example input:
7
1 1
1 2
2 1
2 2
1 2
1 3
2 1


Example output:
1 2 4 4 4 6 3 


Tags: data structures
divide and conquer
dsu
Difficulty rating: 2600

Solution:
#include <bits/stdc++.h>
using namespace std;
int q;
map<int, int> mp[600005];
struct edg {
  int u, v;
  edg() {}
  edg(int u, int v) : u(u), v(v) {}
};
vector<edg> G[1048576];
void Add(int i, int l, int r, int a, int b, edg x) {
  if (r < a || b < l) return;
  if (a <= l && r <= b) {
    G[i].push_back(x);
    return;
  }
  Add(i << 1, l, (l + r) >> 1, a, b, x),
      Add(i << 1 | 1, ((l + r) >> 1) + 1, r, a, b, x);
}
long long Ans[600005];
int fa[600005], sz[600005], lb[600005], rb[600005];
int ff(int x) { return fa[x] ? ff(fa[x]) : x; }
int stk[600005], tp;
long long ans;
void DFS(int i, int l, int r) {
  int ot = tp;
  for (auto x : G[i]) {
    int u = ff(x.u), v = ff(3e5 + x.v);
    if (u == v) continue;
    if (sz[u] < sz[v]) swap(u, v);
    ans += (long long)lb[u] * rb[v] + (long long)rb[u] * lb[v];
    fa[v] = u, lb[u] += lb[v], rb[u] += rb[v], sz[u] += sz[v];
    stk[++tp] = v;
  }
  if (l == r)
    Ans[l] = ans;
  else
    DFS(i << 1, l, (l + r) >> 1), DFS(i << 1 | 1, ((l + r) >> 1) + 1, r);
  while (tp != ot) {
    int u = stk[tp--], v = fa[u];
    lb[v] -= lb[u], rb[v] -= rb[u], sz[v] -= sz[u];
    fa[u] = 0;
    ans -= (long long)lb[u] * rb[v] + (long long)rb[u] * lb[v];
  }
}
int main() {
  scanf("%d", &q);
  for (int i = 1; i <= (6e5); ++i) (i <= 3e5 ? lb : rb)[i] = sz[i] = 1;
  for (int i = 1; i <= (q); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (mp[u][v])
      Add(1, 1, q, mp[u][v], i - 1, edg(u, v)), mp[u][v] = 0;
    else
      mp[u][v] = i;
  }
  for (int i = 1; i <= (3e5); ++i)
    for (auto x : mp[i])
      if (x.second) Add(1, 1, q, x.second, q, edg(i, x.first));
  DFS(1, 1, q);
  for (int i = 1; i <= (q); ++i) printf("%lld ", Ans[i]);
  return 0;
}
818_G. Four Melodies

Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult!

Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. 

This time Alice wants to form four melodies for her tracks.

Alice has a sheet with n notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal.

Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements.

Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7.

You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.

Input

The first line contains one integer number n (4 ≤ n ≤ 3000).

The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105) — notes written on a sheet.

Output

Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody.

Examples

Input

5
1 3 5 7 9


Output

4


Input

5
1 3 5 7 2


Output

5

Note

In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note).

In the second example it is possible to compose one melody with 2 notes — {1, 2}. Remaining notes are used in other three melodies (one note per each melody).

Example input:
5
1 3 5 7 2

5
1 3 5 7 9


Example output:
5
4

Tags: flows
graphs
Difficulty rating: 2600

Solution:
import sys

n = int(input()) + 1
a = [0] + list(map(int, input().split()))
mod7 = [x % 7 for x in a]

dp = [[0]*n for _ in range(n)]
maxnum = [0]*(10**5+10)
ans = 0

for i in range(n):
    maxmod = [0]*7

    for j in range(n):
        maxnum[a[j]] = 0

    for j in range(i):
        maxnum[a[j]] = max(maxnum[a[j]], dp[j][i])
        maxmod[mod7[j]] = max(maxmod[mod7[j]], dp[j][i])

    for j in range(i+1, n):
        dp[i][j] = max(
            maxnum[a[j]-1],
            maxnum[a[j]+1],
            maxmod[mod7[j]],
            dp[0][i]
        ) + 1

        maxnum[a[j]] = max(maxnum[a[j]], dp[i][j])
        maxmod[mod7[j]] = max(maxmod[mod7[j]], dp[i][j])
        ans = max(ans, dp[i][j])

print(ans)
235_C. Cyclical Quest

Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in a string s" quickly by preprocessing the string s. But now he wants to make it harder.

So he wants to ask "how many consecutive substrings of s are cyclical isomorphic to a given string x". You are given string s and n strings xi, for each string xi find, how many consecutive substrings of s are cyclical isomorphic to xi.

Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de".

Input

The first line contains a non-empty string s. The length of string s is not greater than 106 characters.

The second line contains an integer n (1 ≤ n ≤ 105) — the number of queries. Then n lines follow: the i-th line contains the string xi — the string for the i-th query. The total length of xi is less than or equal to 106 characters.

In this problem, strings only consist of lowercase English letters.

Output

For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.

Examples

Input

baabaabaaa
5
a
ba
baa
aabaa
aaba


Output

7
5
7
3
5


Input

aabbaa
3
aa
aabb
abba


Output

2
3
3

Example input:
baabaabaaa
5
a
ba
baa
aabaa
aaba

aabbaa
3
aa
aabb
abba


Example output:
7
5
7
3
5

2
3
3


Tags: data structures
string suffix structures
strings
Difficulty rating: 2700

Solution:
#include <bits/stdc++.h>
using namespace std;
const int M = 2000001;
int n, m, k, a[M], c[M][26], fa[M], sz[M], cnt = 1, las = 1, vis[M], len[M];
void SAM(int k) {
  int x = ++cnt, y = las;
  las = x, len[x] = len[y] + 1;
  sz[x] = 1;
  for (; y && !c[y][k]; y = fa[y]) c[y][k] = x;
  if (!y) {
    fa[x] = 1;
    return;
  }
  int p = c[y][k];
  if (len[p] == len[y] + 1) {
    fa[x] = p;
    return;
  }
  int np = ++cnt;
  fa[np] = fa[p];
  fa[p] = fa[x] = np;
  memcpy(c[np], c[p], sizeof(c[p]));
  len[np] = len[y] + 1;
  for (; y && c[y][k] == p; y = fa[y]) c[y][k] = np;
}
char s[M];
bool cmp(int x, int y) { return len[x] > len[y]; }
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  for (int i = 1; i <= n; i++) SAM(s[i] - 'a');
  for (int i = 1; i <= cnt; i++) a[i] = i;
  sort(a + 1, a + 1 + cnt, cmp);
  for (int i = 1; i <= cnt; i++) sz[fa[a[i]]] += sz[a[i]];
  scanf("%d", &m);
  for (; m; m--) {
    scanf("\n%s", s);
    int ans = 0, L = 0;
    int l = strlen(s), now = 1;
    for (int i = 0; i < l + l; i++) {
      int d = s[i % l] - 'a';
      while (now && !c[now][d]) now = fa[now], L = len[now];
      if (!now)
        now = 1, L = 0;
      else
        now = c[now][d], L++;
      while (len[fa[now]] >= l) now = fa[now], L = len[now];
      if (L >= l && vis[now] != m) {
        vis[now] = m;
        ans += sz[now];
      }
    }
    printf("%d\n", ans);
  }
}
932_G. Palindrome Partition

Given a string s, find the number of ways to split s to substrings such that if there are k substrings (p1, p2, p3, ..., pk) in partition, then pi = pk - i + 1 for all i (1 ≤ i ≤ k) and k is even.

Since the number of ways can be large, print it modulo 109 + 7.

Input

The only line of input contains a string s (2 ≤ |s| ≤ 106) of even length consisting of lowercase Latin letters. 

Output

Print one integer, the number of ways of partitioning the string modulo 109 + 7.

Examples

Input

abcdcdab


Output

1

Input

abbababababbab


Output

3

Note

In the first case, the only way to partition the string is ab|cd|cd|ab.

In the second case, the string can be partitioned as ab|b|ab|ab|ab|ab|b|ab or ab|b|abab|abab|b|ab or abbab|ab|ab|abbab.

Example input:
abcdcdab

abbababababbab


Example output:
1

3


Tags: dp
string suffix structures
strings
Difficulty rating: 2900

Solution:
#include <bits/stdc++.h>
char S[1000000 + 5], T[1000000 + 5];
int c[1000000 + 5][26], l[1000000 + 5], f[1000000 + 5], tn, df[1000000 + 5],
    ff[1000000 + 5], s[1000000 + 5], F[1000000 + 5];
int main() {
  int n, i, j, x;
  scanf("%s", S + 1);
  n = strlen(S + 1);
  T[0] = 255;
  for (i = 1; i <= n / 2; ++i)
    T[2 * i - 1] = S[i] - 'a', T[2 * i] = S[n - i + 1] - 'a';
  f[0] = 1;
  l[++tn] = -1;
  for (F[0] = i = x = 1; i <= n; ++i) {
    while (T[i] != T[i - l[x] - 1]) x = f[x];
    if (!c[x][T[i]]) {
      for (j = f[x]; T[i - l[j] - 1] != T[i]; j = f[j])
        ;
      f[++tn] = c[j][T[i]];
      l[c[x][T[i]] = tn] = l[x] + 2;
      df[tn] = l[tn] - l[f[tn]];
      ff[tn] = df[tn] == df[f[tn]] ? ff[f[tn]] : tn;
    }
    x = c[x][T[i]];
    for (j = x; j; j = f[ff[j]]) {
      s[j] = F[i - l[ff[j]]];
      if (ff[j] != j) s[j] = (s[j] + s[f[j]]) % 1000000007;
      if (~i & 1) F[i] = (F[i] + s[j]) % 1000000007;
    }
  }
  printf("%d", F[n]);
}
p02651 AtCoder Grand Contest 045 - Xor Battle

There are two persons, numbered 0 and 1, and a variable x whose initial value is 0. The two persons now play a game. The game is played in N rounds. The following should be done in the i-th round (1 \leq i \leq N):

* Person S_i does one of the following:
* Replace x with x \oplus A_i, where \oplus represents bitwise XOR.
* Do nothing.



Person 0 aims to have x=0 at the end of the game, while Person 1 aims to have x \neq 0 at the end of the game.

Determine whether x becomes 0 at the end of the game when the two persons play optimally.

Solve T test cases for each input file.

Constraints

* 1 \leq T \leq 100
* 1 \leq N \leq 200
* 1 \leq A_i \leq 10^{18}
* S is a string of length N consisting of `0` and `1`.
* All numbers in input are integers.

Input

Input is given from Standard Input in the following format. The first line is as follows:


T


Then, T test cases follow. Each test case is given in the following format:


N
A_1 A_2 \cdots A_N
S


Output

For each test case, print a line containing `0` if x becomes 0 at the end of the game, and `1` otherwise.

Example

Input

3
2
1 2
10
2
1 1
10
6
2 3 4 5 6 7
111000


Output

1
0
0

Example input:
3
2
1 2
10
2
1 1
10
6
2 3 4 5 6 7
111000

Example output:
1
0
0

Difficulty rating: 0

Solution:
I=input
n=int
for i in range(n(input())):
  I();t=[];x=0
  for i,j in zip(map(n,I().split()[::-1]),I()[::-1]):
    for k in t:i^=k*(i^k<i)
    j=n(j);t+=[i*(j^1)];x|=(i>0)&j
  print(x)
1427_E. Xum

You have a blackboard and initially only an odd number x is written on it. Your goal is to write the number 1 on the blackboard.

You may write new numbers on the blackboard with the following two operations. 

  * You may take two numbers (not necessarily distinct) already on the blackboard and write their sum on the blackboard. The two numbers you have chosen remain on the blackboard. 
  * You may take two numbers (not necessarily distinct) already on the blackboard and write their [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) on the blackboard. The two numbers you have chosen remain on the blackboard. 

Perform a sequence of operations such that at the end the number 1 is on the blackboard.

Input

The single line of the input contains the odd integer x (3 ≤ x ≤ 999,999).

Output

Print on the first line the number q of operations you perform. Then q lines should follow, each describing one operation. 

  * The "sum" operation is described by the line "a + b", where a, b must be integers already present on the blackboard. 
  * The "xor" operation is described by the line "a ^ b", where a, b must be integers already present on the blackboard. 

The operation symbol (+ or ^) must be separated from a, b by a whitespace.

You can perform at most 100,000 operations (that is, q≤ 100,000) and all numbers written on the blackboard must be in the range [0, 5⋅10^{18}]. It can be proven that under such restrictions the required sequence of operations exists. You can output any suitable sequence of operations.

Examples

Input


3


Output


5
3 + 3
3 ^ 6
3 + 5
3 + 6
8 ^ 9


Input


123


Output


10
123 + 123
123 ^ 246
141 + 123
246 + 123
264 ^ 369
121 + 246
367 ^ 369
30 + 30
60 + 60
120 ^ 121

Example input:
3

123


Example output:

5
3 + 3
3 ^ 6
3 + 5
3 + 6
8 ^ 9


10
123 + 123
123 ^ 246
141 + 123
246 + 123
264 ^ 369
121 + 246
367 ^ 369
30 + 30
60 + 60
120 ^ 121


Tags: bitmasks
constructive algorithms
math
matrices
number theory
Difficulty rating: 2500

Solution:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x;
  cin >> x;
  vector<array<long long, 3> > ans;
  while (x > 1) {
    int i = 0;
    while ((1ll << i + 1) <= x) {
      ans.push_back({x << i, x << i, 0});
      ++i;
    }
    long long a = x << i, b = a ^ x;
    ans.push_back({a, x, 1});
    for (int j = 0; j < i; ++j)
      ans.push_back({x << j, a, 0}), ans.push_back({x << j, b, 0}), a += x << j,
          b += x << j;
    ans.push_back({a, b, 1});
    x = a ^ b;
  }
  printf("%d\n", (int)ans.size());
  for (auto &t : ans) printf("%lld %c %lld\n", t[0], "+^"[t[2]], t[1]);
}