JerryL/AlgoSimBench huggingface dataset:
749_E. Inversions After Shuffle

You are given a permutation of integers from 1 to n. Exactly once you apply the following operation to this permutation: pick a random segment and shuffle its elements. Formally:

  1. Pick a random segment (continuous subsequence) from l to r. All <image> segments are equiprobable. 
  2. Let k = r - l + 1, i.e. the length of the chosen segment. Pick a random permutation of integers from 1 to k, p1, p2, ..., pk. All k! permutation are equiprobable. 
  3. This permutation is applied to elements of the chosen segment, i.e. permutation a1, a2, ..., al - 1, al, al + 1, ..., ar - 1, ar, ar + 1, ..., an is transformed to a1, a2, ..., al - 1, al - 1 + p1, al - 1 + p2, ..., al - 1 + pk - 1, al - 1 + pk, ar + 1, ..., an. 



Inversion if a pair of elements (not necessary neighbouring) with the wrong relative order. In other words, the number of inversion is equal to the number of pairs (i, j) such that i < j and ai > aj. Find the expected number of inversions after we apply exactly one operation mentioned above.

Input

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the length of the permutation.

The second line contains n distinct integers from 1 to n — elements of the permutation.

Output

Print one real value — the expected number of inversions. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 9. 

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if <image>.

Example

Input

3
2 3 1


Output

1.916666666666666666666666666667

Example input:
3
2 3 1


Example output:
1.91666666666666674068


Tags: data structures
probabilities
Difficulty rating: 2400

Solution:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
int n;
long long BIT[2][N];
int a[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  int x;
  double ans = 0;
  for (int i = (1); i <= (n); i++) cin >> a[i];
  for (int i = n; i; i--) {
    int j = a[i];
    while (j) {
      ans += BIT[0][j];
      ans -= 2. * i * BIT[1][j] / n / (n + 1);
      j -= j & -j;
    }
    j = a[i];
    while (j <= n) {
      BIT[0][j]++;
      BIT[1][j] += n + 1 - i;
      j += j & -j;
    }
    ans += (n - i + 1.) * i * (i - 1) / n / (n + 1) / 2;
  }
  cout << fixed << setprecision(13) << ans << endl;
}
920_F. SUM and REPLACE

Let D(x) be the number of positive divisors of a positive integer x. For example, D(2) = 2 (2 is divisible by 1 and 2), D(6) = 4 (6 is divisible by 1, 2, 3 and 6).

You are given an array a of n integers. You have to process two types of queries:

  1. REPLACE l r — for every <image> replace ai with D(ai); 
  2. SUM l r — calculate <image>. 



Print the answer for each SUM query.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 3·105) — the number of elements in the array and the number of queries to process, respectively.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 106) — the elements of the array.

Then m lines follow, each containing 3 integers ti, li, ri denoting i-th query. If ti = 1, then i-th query is REPLACE li ri, otherwise it's SUM li ri (1 ≤ ti ≤ 2, 1 ≤ li ≤ ri ≤ n).

There is at least one SUM query.

Output

For each SUM query print the answer to it.

Example

Input

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


Output

30
13
4
22

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


Example output:
30
13
4
22


Tags: brute force
data structures
dsu
number theory
Difficulty rating: 2000

Solution:
#include <bits/stdc++.h>
using namespace std;
long long f[300010], d[1000010];
int n;
void add(int i, long long x) {
  while (i <= n) {
    f[i] += x;
    i += i & -i;
  }
}
long long sum(int i) {
  long long s = 0;
  while (i >= 1) {
    s += f[i];
    i -= i & -i;
  }
  return s;
}
set<int> pos;
int main() {
  int m, i, j, c, x, y;
  long long xx, val;
  for (i = 1; i <= 1000000; ++i) {
    for (j = i; j <= 1000000; j += i) {
      ++d[j];
    }
  }
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; ++i) {
    scanf("%lld", &xx);
    add(i, xx);
    if (xx > 2) pos.insert(i);
  }
  while (m--) {
    scanf("%d%d%d", &c, &x, &y);
    if (c == 1) {
      auto it = pos.lower_bound(x);
      while (it != pos.end() && (*it) <= y) {
        val = sum(*it) - sum((*it) - 1);
        add(*it, d[val] - val);
        if (d[val] <= 2)
          it = pos.erase(it);
        else
          ++it;
      }
    } else {
      printf("%lld\n", sum(y) - sum(x - 1));
    }
  }
}
1527_E. Partition Game

You are given an array a of n integers. Define the cost of some array t as follows:

$$$cost(t) = ∑_{x ∈ set(t) } last(x) - first(x),$$$ 

where set(t) is the set of all values in t without repetitions, first(x), and last(x) are the indices of the first and last occurrence of x in t, respectively. In other words, we compute the distance between the first and last occurrences for each distinct element and sum them up.

You need to split the array a into k consecutive segments such that each element of a belongs to exactly one segment and the sum of the cost of individual segments is minimum.

Input

The first line contains two integers n, k (1 ≤ n ≤ 35 000, 1 ≤ k ≤ min(n,100)).

The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ n).

Output

Output the minimum sum of the cost of individual segments.

Examples

Input


7 2
1 6 6 4 6 6 6


Output


3


Input


7 4
5 5 5 5 2 3 3


Output


1

Note

In the first example, we can divide the array into [1,6,6,4] and [6,6,6]. Cost of [1,6,6,4] will be (1-1) + (3 - 2) + (4-4) = 1 and cost of [6,6,6] will be 3-1 = 2. Total cost would be 1 + 2 = 3.

In the second example, divide the array into [5,5],[5],[5,2,3] and [3]. Total Cost would be 1 + 0 + 0 + 0 = 1.

Example input:
7 4
5 5 5 5 2 3 3

7 2
1 6 6 4 6 6 6


Example output:

1


3


Tags: binary search
data structures
divide and conquer
dp
Difficulty rating: 2500

Solution:
#include<bits/stdc++.h>
using namespace std;const int N=4e5,M=110,inf=0x3f3f3f3f;
int a[N],dp[N][M],lst[N],pre[N],nxt[N],i,j,n,m,L,R,sum;
int cal(int l,int r){
	while(L<l){if(nxt[L]<=R)sum-=nxt[L]-L;L++;}
	while(L>l){--L;if(nxt[L]<=R)sum+=nxt[L]-L;}
	while(R<r){++R;if(pre[R]>=L)sum+=R-pre[R];}
	while(R>r){if(pre[R]>=L)sum-=R-pre[R];R--;}
	return sum;
} 
void solve(int l,int r,int L,int R,int now){
	if(l>r||L>R)return;int mid=l+r>>1,val=inf,pos;
	for(int i=L;i<mid&&i<=R;++i){
		int tmp=dp[i][now-1]+cal(i+1,mid);
		if(tmp<val)pos=i,val=tmp;
	}dp[mid][now]=val;solve(l,mid-1,L,pos,now);solve(mid+1,r,pos,R,now);
}
int main(){
	for(scanf("%d%d",&n,&m),i=1;i<=n;++i)scanf("%d",a+i);
	for(i=1;i<=n;++i)dp[i][0]=inf,pre[i]=lst[a[i]],lst[a[i]]=i;
	for(i=1;i<=n;++i)lst[a[i]]=n+1;
	for(i=n;i;--i)nxt[i]=lst[a[i]],lst[a[i]]=i;
	for(i=1;i<=m;++i)solve(1,n,0,n,i);printf("%d\n",dp[n][m]);
}
154_C. Double Profiles

You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user.

The social network contains n registered profiles, numbered from 1 to n. Some pairs there are friends (the "friendship" relationship is mutual, that is, if i is friends with j, then j is also friends with i). Let's say that profiles i and j (i ≠ j) are doubles, if for any profile k (k ≠ i, k ≠ j) one of the two statements is true: either k is friends with i and j, or k isn't friends with either of them. Also, i and j can be friends or not be friends.

Your task is to count the number of different unordered pairs (i, j), such that the profiles i and j are doubles. Note that the pairs are unordered, that is, pairs (a, b) and (b, a) are considered identical.

Input

The first line contains two space-separated integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), — the number of profiles and the number of pairs of friends, correspondingly. 

Next m lines contains descriptions of pairs of friends in the format "v u", where v and u (1 ≤ v, u ≤ n, v ≠ u) are numbers of profiles that are friends with each other. It is guaranteed that each unordered pair of friends occurs no more than once and no profile is friends with itself.

Output

Print the single integer — the number of unordered pairs of profiles that are doubles. 

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the %I64d specificator.

Examples

Input

3 3
1 2
2 3
1 3


Output

3


Input

3 0


Output

3


Input

4 1
1 3


Output

2

Note

In the first and second sample any two profiles are doubles.

In the third sample the doubles are pairs of profiles (1, 3) and (2, 4).

Example input:
4 1
1 3

3 0

3 3
1 2
2 3
1 3


Example output:
2
3
3

Tags: graphs
hashing
sortings
Difficulty rating: 2300

Solution:
#include <bits/stdc++.h>
using namespace std;
long long int a[1 << 21], h[1 << 21], res = 0;
int main() {
  int n, m, u, v, l, r, i;
  scanf("%d %d", &n, &m);
  a[n] = h[0] = 1;
  for (i = 1; i < n; ++i) a[i + n] = h[i] = 3 * h[i - 1];
  while (m--) {
    scanf("%d %d", &u, &v);
    --u, --v;
    a[u] += h[v];
    a[v] += h[u];
    a[n + u] += h[v];
    a[n + v] += h[u];
  }
  sort(a, a + 2 * n);
  l = 0;
  for (r = 1; r < 2 * n; ++r) {
    if (a[r] != a[r - 1]) l = r;
    res += r - l;
  }
  cout << res << endl;
}
727_E. Games on a CD

Several years ago Tolya had n computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to k. The names didn't overlap.

Thus, there is a cyclic string of length n·k written on the CD.

Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were g popular games that days. All of the games he burned were among these g games, and no game was burned more than once.

You have to restore any valid list of games Tolya could burn to the CD several years ago.

Input

The first line of the input contains two positive integers n and k (1 ≤ n ≤ 105, 1 ≤ k ≤ 105) — the amount of games Tolya burned to the CD, and the length of each of the names.

The second line of the input contains one string consisting of lowercase English letters — the string Tolya wrote on the CD, split in arbitrary place. The length of the string is n·k. It is guaranteed that the length is not greater than 106.

The third line of the input contains one positive integer g (n ≤ g ≤ 105) — the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2·106.

Each of the next g lines contains a single string — the name of some popular game. Each name consists of lowercase English letters and has length k. It is guaranteed that the names are distinct.

Output

If there is no answer, print "NO" (without quotes).

Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print n integers — the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them.

Examples

Input

3 1
abc
4
b
a
c
d


Output

YES
2 1 3 


Input

4 2
aabbccdd
4
dd
ab
bc
cd


Output

NO

Example input:
3 1
abc
4
b
a
c
d

4 2
aabbccdd
4
dd
ab
bc
cd


Example output:
YES
2 1 3 
NO


Tags: data structures
hashing
string suffix structures
strings
Difficulty rating: 2300

Solution:
#include <bits/stdc++.h>
using namespace std;
unordered_map<string, int> S;
unordered_set<int> y;
int main() {
  ios_base::sync_with_stdio(0);
  int n, k, g;
  string s;
  cin >> n >> k >> s >> g;
  s += s;
  for (int i = 0; i < g; i++) {
    string t;
    cin >> t;
    S[t] = i;
  }
  int len = n * k;
  vector<int> ans(n);
  bool ok;
  for (int j = len - k; j < len; j++) {
    ok = 1;
    for (int i = 0; i < n; i++) {
      auto u = S.find(s.substr(j + i * k, k));
      if (u == S.end() || y.count(u->second)) {
        ok = 0;
        break;
      }
      ans[i] = u->second;
      y.insert(u->second);
    }
    if (ok) break;
    y.clear();
  }
  if (ok) {
    cout << "YES\n";
    for (auto x : ans) cout << x + 1 << " ";
  } else {
    cout << "NO\n";
  }
  return 0;
}
19_C. Deletion of Repeats

Once Bob saw a string. It contained so many different letters, that the letters were marked by numbers, but at the same time each letter could be met in the string at most 10 times. Bob didn't like that string, because it contained repeats: a repeat of length x is such a substring of length 2x, that its first half coincides character by character with its second half. Bob started deleting all the repeats from the string. He does it as follows: while it's possible, Bob takes the shortest repeat, if it is not unique, he takes the leftmost one, and deletes its left half and everything that is to the left of this repeat.

You're given the string seen by Bob. Find out, what it will look like after Bob deletes all the repeats in the way described above.

Input

The first input line contains integer n (1 ≤ n ≤ 105) — length of the string. The following line contains n space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times.

Output

In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way.

Examples

Input

6
1 2 3 1 2 3


Output

3
1 2 3 


Input

7
4 5 6 5 6 7 7


Output

1
7 

Example input:
6
1 2 3 1 2 3

7
4 5 6 5 6 7 7


Example output:
3
1 2 3 
1
7 

Tags: greedy
hashing
string suffix structures
Difficulty rating: 2200

Solution:
#include <bits/stdc++.h>
using namespace std;
int n, a[100001];
map<int, list<int>> q;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    q[a[i]].push_back(i);
  }
  a[n] = -1;
  int t = 0;
  for (int i = 0; i < n; i++) {
    list<int>& r = q[a[i]];
    r.pop_front();
    for (auto c : r) {
      int x = i, y = c;
      do x++, y++;
      while (x < c && a[x] == a[y]);
      if (x == c) i = (t = c) - 1;
    }
  }
  printf("%d\n", n - t);
  while (t < n) printf("%d ", a[t++]);
}
Substrings and Repetitions

Statement
Given a string S and several frequencies Fi. For each Fi output the number of substrings of S (the characters of substring should be contiguous) that occur at least Fi times in S. Note, that we consider two substrings distinct if they have distinct length, or they have distinct starting indices.
Input
The first line of input contains integer T, the number of test cases.
The first line of each test case contains string S (it contains only lowercase Latin letters). The second line of each test case contains an integer Q, that is the number of queries to be answered. Then Q lines follows containing Fi each.
Output
For each query, output the required answer.
Constraints

1 ≤ |S| ≤ 200000
1 ≤ Q ≤ 200000
1 ≤ Fi ≤ 200000
Sum of |S| over all test cases ≤ 200000
Sum of Q over all test cases ≤ 200000

 
Example
Input:
1
aaeddf
4
1
2
3
4

Output:
21
4
0
0
 
Explanation
There are a total of 6*(6+1)/2 substrings, all of them occur at least once. So for F1=1 answer is equal to 21. Note that substring "a" is repeated 2 times in S, both the instances are added to the answer.
Look at F2=2. Substring "a" and substring "d" occurs 2 times each in S. The answer for F2 is 4, that is all the occurrences of "a" and "d".
There are no substrings which repeat more than twice. So rest answers are 0

Example input:

Example output:


Solution:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int maxn = 4e5;
    
    int len[maxn], link[maxn], cnt[maxn];
    map<char, int> to[maxn];
    int sz = 1, last;
    
    void add_letter(char c)
    {
    	int p = last;
    	last = sz++;
    	len[last] = len[p] + 1;
    	cnt[last] = 1;
    	for(; to[p][c] == 0; p = link[p])
    		to[p][c] = last;
    	if(to[p][c] == last)
    		return;
    	int q = to[p][c];
    	if(len[q] == len[p] + 1)
    	{
    		link[last] = q;
    		return;
    	}
    	int cl = sz++;
    	to[cl] = to[q];
    	link[cl] = link[q];
    	len[cl] = len[p] + 1;
    	link[last] = link[q] = cl;
    	for(; to[p][c] == q; p = link[p])
    		to[p][c] = cl;
    }
    
    vector<int> g[maxn];
    string s;
    int64_t ans[maxn];
    
    void init()
    {
    	for(int i = 0; i < sz; i++)
    	{
    		cnt[i] = len[i] = link[i] = 0;
    		ans[i] = 0;
    		to[i].clear();
    		g[i].clear();
    	}
    	sz = 1, last = 0;
    }
    
    void solve()
    {
    	init();
    	cin >> s;
    	for(auto c: s)
    		add_letter(c);
    	for(int i = 1; i < sz; i++)
    		g[len[i]].push_back(i);
    	for(int i = s.size(); i; i--)
    		for(auto v: g[i])
    			cnt[link[v]] += cnt[v];
    	int n;
    	cin >> n;
    	for(int i = 1; i < sz; i++)
    		ans[cnt[i]] += 1LL * (len[i] - len[link[i]]) * cnt[i];
    	for(int i = n; i; i--)
    		ans[i] += ans[i + 1];
    	while(n--)
    	{
    		int p;
    		cin >> p;
    		cout << ans[p] << "\n";
    	}
    }
    
    int main()
    {
    	ios::sync_with_stdio(0);
    	cin.tie(0);
    	int t;
    	cin >> t;
    	while(t--)
    		solve();
    }
802_I. Fake News (hard)

Now that you have proposed a fake post for the HC2 Facebook page, Heidi wants to measure the quality of the post before actually posting it. She recently came across a (possibly fake) article about the impact of fractal structure on multimedia messages and she is now trying to measure the self-similarity of the message, which is defined as

<image>

where the sum is over all nonempty strings p and <image> is the number of occurences of p in s as a substring. (Note that the sum is infinite, but it only has a finite number of nonzero summands.)

Heidi refuses to do anything else until she knows how to calculate this self-similarity. Could you please help her? (If you would like to instead convince Heidi that a finite string cannot be a fractal anyway – do not bother, we have already tried.)

Input

The input starts with a line indicating the number of test cases T (1 ≤ T ≤ 10). After that, T test cases follow, each of which consists of one line containing a string s (1 ≤ |s| ≤ 100 000) composed of lowercase letters (a-z).

Output

Output T lines, every line containing one number – the answer to the corresponding test case.

Example

Input

4
aa
abcd
ccc
abcc


Output

5
10
14
12

Note

A string s contains another string p as a substring if p is a contiguous subsequence of s. For example, ab is a substring of cab but not of acb.

Example input:
4
aa
abcd
ccc
abcc


Example output:
5
10
14
12


Tags: string suffix structures
Difficulty rating: 2300

Solution:
#include <bits/stdc++.h>
const int N = 100005, M = N * 2;
int tot, np, fa[M], len[M], ch[M][26], sz[M];
std::vector<int> g[M];
inline void extend(int x) {
  int p = np;
  np = ++tot, len[np] = len[p] + 1;
  memset(ch[np], 0, 104), sz[np] = 1;
  for (; p && !ch[p][x]; ch[p][x] = np, p = fa[p])
    ;
  if (!p)
    fa[np] = 1;
  else if (len[ch[p][x]] == len[p] + 1)
    fa[np] = ch[p][x];
  else {
    int nq = ++tot;
    len[nq] = len[p] + 1;
    sz[nq] = 0, memcpy(ch[nq], ch[ch[p][x]], 104);
    fa[nq] = fa[ch[p][x]], fa[np] = fa[ch[p][x]] = nq;
    for (int Q = ch[p][x]; p && ch[p][x] == Q; ch[p][x] = nq, p = fa[p])
      ;
  }
}
int n;
char s[N];
void solve() {
  scanf("%s", s + 1), n = strlen(s + 1);
  tot = np = 1;
  sz[1] = 0, memset(ch[1], 0, 104);
  for (int i = 1; i <= n; ++i) extend(s[i] - 'a');
  for (int i = 1; i <= tot; ++i) g[i].clear();
  for (int i = 2; i <= tot; ++i) g[fa[i]].emplace_back(i);
  long long ans = 0;
  std::function<void(int)> dfs = [&](int u) {
    for (auto v : g[u]) dfs(v), sz[u] += sz[v];
    ans += 1ll * sz[u] * sz[u] * (len[u] - len[fa[u]]);
  };
  dfs(1);
  printf("%lld\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) solve();
  return 0;
}
407_C. Curious Array

You've got an array consisting of n integers: a[1], a[2], ..., a[n]. Moreover, there are m queries, each query can be described by three integers li, ri, ki. Query li, ri, ki means that we should add <image> to each element a[j], where li ≤ j ≤ ri.

Record <image> means the binomial coefficient, or the number of combinations from y elements into groups of x elements.

You need to fulfil consecutively all queries and then print the final array.

Input

The first line contains integers n, m (1 ≤ n, m ≤ 105).

The second line contains n integers a[1], a[2], ..., a[n] (0 ≤ ai ≤ 109) — the initial array.

Next m lines contain queries in the format li, ri, ki — to all elements of the segment li... ri add number <image> (1 ≤ li ≤ ri ≤ n; 0 ≤ k ≤ 100).

Output

Print n integers: the i-th number is the value of element a[i] after all the queries. As the values can be rather large, print them modulo 1000000007 (109 + 7).

Examples

Input

5 1
0 0 0 0 0
1 5 0


Output

1 1 1 1 1


Input

10 2
1 2 3 4 5 0 0 0 0 0
1 6 1
6 10 2


Output

2 4 6 8 10 7 3 6 10 15

Example input:
5 1
0 0 0 0 0
1 5 0

10 2
1 2 3 4 5 0 0 0 0 0
1 6 1
6 10 2


Example output:
1 1 1 1 1 

2 4 6 8 10 7 3 6 10 15 


Tags: brute force
combinatorics
implementation
math
Difficulty rating: 2500

Solution:
#include <bits/stdc++.h>
int n, m, a[100005], c[100105][115];
int f[115][100050];
int main() {
  int i, k, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (i = 0; i <= 100010; i++)
    for (j = 0; j <= i && j <= 100; j++)
      c[i][j] = (i && j) ? (c[i - 1][j] + c[i - 1][j - 1]) % 1000000007 : 1;
  while (m--) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    f[k][l]++;
    for (i = k; i >= 0; i--)
      f[i][r + 1] = (f[i][r + 1] - c[r - l + k - i][k - i]) % 1000000007;
  }
  for (k = 100; k >= 0; k--)
    for (i = 1; i <= n; i++) {
      f[k][i] = (0ll + f[k][i] + f[k + 1][i] + f[k][i - 1]) % 1000000007;
    }
  for (i = 1; i <= n; i++)
    printf("%d ", ((a[i] + f[0][i]) % 1000000007 + 1000000007) % 1000000007);
  printf("\n");
  return 0;
}