alexjercan/bugnet huggingface dataset:
Score : 1100 points

Problem Statement

There are N strings of lowercase alphabet only. The i-th string is S_i. Every string is unique.

Provide answers for the Q queries below. The i-th query has the following format:

Query: An integer k_i and a string p_{i,1}p_{i,2}...p_{i,26} that results from permuting {a,b,...,z} are given. Output the sequence of the string S_{k_i} among the N strings in lexicographical order when the literal sequence is p_{i,1}<p_{i,2}<...<p_{i,26}.

Constraints

1 ≦ N,Q ≦ 100000

1 ≦ |S_i| (1 ≦ i ≦ N)

S_i (1 ≦ i ≦ N) is a string of lowercase alphabet.

The sum of |S_i| is no more than 400000.

Every S_i is unique.

1 ≦ k_i ≦ N (1 ≦ i ≦ Q)

For all 1 ≦ i ≦ Q, p_{i,1}p_{i,2}...p_{i,26} is a permutation of abcd...z.

Input

Inputs are provided from standard inputs in the following form.

N
S_1
:
S_N
Q
k_1 p_{1,1}p_{1,2}...p_{1,26}
:
k_Q p_{Q,1}p_{Q,2}...p_{Q,26}

Output

Output Q lines.

On line i, for the i-th query, output an integer indicating the sequence of the string S_{k_i} among the N strings in lexicographical order.

Sample Input 1

5
aa
abbaa
abbba
aaab
aaaaaba
5
1 abcdefghijklmnopqrstuvwxyz
2 bacdefghijklmnopqrstuvwxyz
3 abcdefghijklmnopqrstuvwxyz
4 bacdefghijklmnopqrstuvwxyz
5 abcdefghijklmnopqrstuvwxyz

Sample Output 1

1
2
5
4
2

When the literal sequence is a < b, sorting the input strings in lexicographical order yields aa, aaaaaba, aaab, abbaa, abbba. The answers to queries 1, 3, and 5 are thus 1, 5, and 2, respectively.

When the literal sequence is b < a, sorting the input strings in lexicographical order yields abbba, abbaa, aa, aaab, aaaaaba. The answers to queries 2 and 4 are thus 2 and 4, respectively.

Sample Input 2

8
abrakatabra
abadaba
abracadabra
atcoder
grand
contest
ababa
a
6
3 abcdefghijklmnopqrstuvwxyz
6 qwertyuiopasdfghjklzxcvbnm
8 poiuytrewqlkjhgfdsamnbvcxz
2 qazwsxedcrfvtgbyhnujmikolp
1 plokmijnuhbygvtfcrdxeszwaq
4 mnbvcxzasdfghjklpoiuytrewq

Sample Output 2

4
8
2
3
4
7
Score : 66 points

Problem Statement

Summer vacation ended at last and the second semester has begun.
You, a Kyoto University student, came to university and heard a rumor that somebody will barricade the entrance of your classroom.
The barricade will be built just before the start of the A-th class and removed by Kyoto University students just before the start of the B-th class.
All the classes conducted when the barricade is blocking the entrance will be cancelled and you will not be able to attend them.
Today you take N classes and class i is conducted in the t_i-th period.
You take at most one class in each period.
Find the number of classes you can attend.

Constraints

1 \leq N \leq 1000

1 \leq A < B \leq 10^9

1 \leq t_i \leq 10^9

All t_i values are distinct.

Input

N, A and B are given on the first line and t_i is given on the (i+1)-th line.

N A B
t1
:
tN

Output

Print the number of classes you can attend.

Sample Input 1

5 5 9
4
3
6
9
1

Sample Output 1

4

You can not only attend the third class.

Sample Input 2

5 4 9
5
6
7
8
9

Sample Output 2

1

You can only attend the fifth class.

Sample Input 3

4 3 6
9
6
8
1

Sample Output 3

4

You can attend all the classes.

Sample Input 4

2 1 2
1
2

Sample Output 4

1

You can not attend the first class, but can attend the second.
Score : 100 points

Problem Statement

Kyoto University Programming Contest is a programming contest voluntarily held by some Kyoto University students.
This contest is abbreviated as Kyoto University Programming Contest and called KUPC.

source: Kyoto University Programming Contest Information

The problem-preparing committee met to hold this year's KUPC and N problems were proposed there.
The problems are numbered from 1 to N and the name of i-th problem is P_i.
However, since they proposed too many problems, they decided to divide them into some sets for several contests.

They decided to divide this year's KUPC into several KUPCs by dividing problems under the following conditions.

One KUPC provides K problems.

Each problem appears at most once among all the KUPCs.

All the first letters of the problem names in one KUPC must be different.

You, one of the committee members, want to hold as many KUPCs as possible.
Write a program to find the maximum number of KUPCs that can be held this year.

Constraints

1 \leq N \leq 10^4

1 \leq K \leq 26

1 \leq |P_i| \leq 10

All characters in P_i are capital letters.

Note that, for each i and j (1 \leq i < j \leq N),
P_i \neq P_j are not necessarily satisfied.

Input

The input is given from Standard Input in the following format:

N K
P_1
:
P_N

Output

Print the maximum number of KUPCs that can be held on one line.

Sample Input 1

9 3
APPLE
ANT
ATCODER
BLOCK
BULL
BOSS
CAT
DOG
EGG

For example, three KUPCs can be held by dividing the problems as follows.

First: APPLE, BLOCK, CAT

Second: ANT, BULL, DOG

Third: ATCODER, BOSS, EGG

Sample Output 1

3

Sample Input 2

3 2
KU
KYOUDAI
KYOTOUNIV

Sample Output 2

0

No KUPC can be held.
Score : 100 points

Problem Statement

A professor invented Cookie Breeding Machine for his students who like cookies very much.

When one cookie with the taste of x is put into the machine and a non-negative integer y less than or equal to 127 is input on the machine,
it consumes the cookie and generates two cookies with the taste of y and (x XOR y).

Here, XOR represents Bitwise Exclusive OR.

At first, there is only one cookie and the taste of it is D .

Find the maximum value of the sum of the taste of the exactly N cookies generated after the following operation is conducted N-1 times.

Put one of the cookies into the machine.

Input a non-negative integer less than or equal to 127 on the machine.

Constraints

1 \leq T \leq 1000

1 \leq N_t \leq 1000 (1 \leq t \leq T)

1 \leq D_t \leq 127 (1 \leq t \leq T)

Input

The input is given from Standard Input in the following format:

T
N_1 D_1
:
N_T D_T

The input consists of multiple test cases. An Integer T that represents the number of test cases is given on line 1.

Each test case is given on the next T lines.

In the t-th test case ( 1 \leq t \leq T ),
N_t that represents the number of cookies generated through the operations and D_t that represents the taste of the initial cookie are given separated by space.

Output

For each test case, print the maximum value of the sum of the taste of the N cookies generated through the operations on one line.

Sample Input 1

3
3 1
4 108
1 10

Sample Output 1

255
400
10

On the 1st test case, if the machine is used as follows, three cookies with the taste of 61, 95 and 99 are generated.
Since the sum of these values is maximum among all possible ways, the answer is 255.

Put the cookie with the taste of 1 and input an integer 60 on the machine, lose the cookie and get two cookies with the taste of 60 and 61.

Put the cookie with the taste of 60 and input an integer 99 on the machine, lose the cookie and get two cookies with the taste of 99 and 95.

On the 3rd test case, the machine may not be used.
Score : 150 points

Problem Statement

Gorillas in Kyoto University are good at math.
They are currently trying to solve problems to find the value of an expression that contains two functions, _ , ^.
Each of these functions takes two input values. _ function returns the smaller of the two input values and ^ function returns the larger.
Gorillas know that integers in the expression are non-negative and less than or equal to 99,
but can not find out the length of the expression until they read a terminal symbol ? that represents the end of the expression. The number of characters included in each expression is less than or equal to 1000, but they do not even know this fact.
Ai, a smart gorilla, noticed that she may be able to know the value of the expression even if they don't read the whole expression.

For example,

Assume you read the following sentence from the left.

^(41,3)?

When you read the sixth character, that is, when you read the following expression,

^(41,3

you can tell the second input value of the funcion is whether 3 or an integer between 30 and 39, and the value turns out 41.

Since Ai wants to solve problems earlier than other gorillas, she decided to solve the problems such that she reads as fewer characters as possible from the left.
For each expression, Find the value of the expression and the minimum number of characters Ai needs to read to know the value.

Constraints

1 \leq Q \leq 200

The number of characters each expression contains is less than or equal to 1000.

Input

The input consists of multiple test cases and is given from Standard Input in the following format:

Q
statement_1
...
statement_Q

Each statement_i (1 \leq i \leq Q) is given in the following BNF format.

<statement> ::= <expression> ?
<expression> ::= (^ | _) ( <expression> , <expression> ) | <number>
<number> :: = 0 | 1 | 2 | ... | 98 | 99

Output

Output consists of Q lines.
On line i (1 \leq i \leq Q), print the value of the expression and the number of character Ai needs to read for the test case i separated by space.

Sample Input 1

4
_(4,51)?
^(99,_(3,67))?
_(0,87)?
3?

Sample Output 1

4 5
99 4
0 3
3 2

For the first test case, when you read the fifth character, that is, when you read _(4,5, you will know the value is 4.

For the second test case, when you read the fourth character, that is, when you read ^(99, you will know the value is 99.

For the third test case, when you read the third character, that is, when you read _(0, you will know the value is 0.

For the fourth test case, you will not know the value is 3 untill you read the terminal symbol.

Sample Input 2

7
_(23,^(_(22,40),4))?
_(0,99)?
^(99,_(^(19,2),5))?
_(^(43,20),^(30,29))?
^(_(20,3),_(50,41))?
^(_(20,3),_(3,41))?
^(_(20,3),_(4,41))?

Sample Output 2

22 18
0 3
99 4
30 17
41 17
3 14
4 15
Score : 200 points

Problem Statement

You are going to take the entrance examination of Kyoto University tomorrow
and have decided to memorize a set of strings S that is expected to appear in the examination.
Since it is really tough to memorize S as it is,
you have decided to memorize a single string T that efficiently contains all the strings in S.

You have already confirmed the following conditions for S and T are satisfied.

Every string in S is a consecutive subsequence(1) in T .

For every pair of strings x, y (x \neq y) in S ,
x is not a subsequence(2) of y.

Note that (1) is ''consecutive subsequence'', while (2) is ''subsequence''.

The next day, you opened the problem booklet at the examination
and found that you can get a full score only if you remember S.
However, You have forgot how to reconstruct S from T .
Since all you remember is that T satisfies the above conditions,
first you have decided to find the maximum possible number of elements in S .

Constraints

1 \leq |T| \leq 10^5

T consists of only lowercase letters.

Partial points

30 points will be awarded for passing the test set satisfying the condition: 1 \leq |T| \leq 50 .

Another 30 points will be awarded for passing the test set satisfying the condition: 1 \leq |T| \leq 10^3.

Input

The input is given from Standard Input in the following format:

T

The input only consists of T on one line.

Output

Print the maximum number of elements inS .

Sample Input 1

abcabc

Sample Output 1

3

In this case, ab，ca and bc are an example of the optimum answers.

Sample Input 2

abracadabra

Sample Output 2

7

Sample Input 3

abcbabbcabbc

Sample Output 3

8

Sample Input 4

bbcacbcbcabbabacccbbcacbaaababbacabaaccbccabcaabba

Sample Output 4

44
Score : 200 points

Problem Statement

Kyoto University decided to build a straight wall on the west side of the university to protect against gorillas that attack the university from the west every night.
Since it is difficult to protect the university at some points along the wall where gorillas attack violently, reinforcement materials are also built at those points.
Although the number of the materials is limited, the state-of-the-art technology can make a prediction about the points where gorillas will attack next and the number of gorillas that will attack at each point.
The materials are moved along the wall everyday according to the prediction.
You, a smart student majoring in computer science, are called to find the way to move the materials more efficiently.

Theare are N points where reinforcement materials can be build along the straight wall. They are numbered 1 through N.
Because of the protection against the last attack of gorillas, A_i materials are build at point i (1 \leq i \leq N).
For the next attack, the materials need to be rearranged such that at least B_i materials are built at point i (1 \leq i \leq N).
It costs |i - j| to move 1 material from point i to point j.
Find the minimum total cost required to satisfy the condition by moving materials.
You do not need to consider the attack after the next.

Constraints

1 \leq N \leq 10^5

A_i \geq 1

B_i \geq 1

A_1 + A_2 + ... + A_N \leq 10^{12}

B_1 + B_2 + ... + B_N \leq A_1 + A_2 + ... + A_N

There is at least one way to satisfy the condition.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

Print the minimum total cost required to satisfy the condition.

Partial Scores

30 points will be awarded for passing the test set satisfying the following:

N \leq 100

A_1 + A_2 + ... + A_N \leq 400

Another 30 points will be awarded for passing the test set satisfying the following:

N \leq 10^3

Another 140 points will be awarded for passing the test set without addtional constraints and you can get 200 points in total.

Sample Input 1

2
1 5
3 1

Sample Output 1

2

It costs least to move 2 materials from point 2 to point 1.

Sample Input 2

5
1 2 3 4 5
3 3 1 1 1

Sample Output 2

6

Sample Input 3

27
46 3 4 2 10 2 5 2 6 7 20 13 9 49 3 8 4 3 19 9 3 5 4 13 9 5 7
10 2 5 6 2 6 3 2 2 5 3 11 13 2 2 7 7 3 9 5 13 4 17 2 2 2 4

Sample Output 3

48

The input of this test case satisfies both the first and second additional constraints.

Sample Input 4

18
3878348 423911 8031742 1035156 24256 10344593 19379 3867285 4481365 1475384 1959412 1383457 164869 4633165 6674637 9732852 10459147 2810788
1236501 770807 4003004 131688 1965412 266841 3980782 565060 816313 192940 541896 250801 217586 3806049 1220252 1161079 31168 2008961

Sample Output 4

6302172

The input of this test case satisfies the second additional constraint.

Sample Input 5

2
1 99999999999
1234567891 1

Sample Output 5

1234567890

The input and output values may exceed the range of 32-bit integer.
Score : 300 points

Problem Statement

Eli- 1 started a part-time job handing out leaflets for N seconds.
Eli- 1 wants to hand out as many leaflets as possible with her special ability, Cloning.
Eli- gen can perform two kinds of actions below.

Clone herself and generate Eli- (gen + 1) . (one Eli- gen (cloning) and one Eli- (gen + 1) (cloned) exist as a result of Eli-gen 's cloning.)
This action takes gen \times C ( C is a coefficient related to cloning. ) seconds.

Hand out one leaflet. This action takes one second regardress of the generation ( =gen ).

They can not hand out leaflets while cloning.
Given N and C , find the maximum number of leaflets Eli- 1 and her clones can hand out in total modulo 1000000007 (= 10^9 + 7).

Constraints

1 \leq Q \leq 100000 = 10^5

1 \leq N_q \leq 100000 = 10^5

1 \leq C_q \leq 20000 = 2 \times 10^4

Input

The input is given from Standard Input in the following format:

Q
N_1 C_1
:
N_Q C_Q

The input consists of multiple test cases. On line 1 , Q that represents the number of test cases is given.
Each test case is given on the next Q lines.
For the test case q ( 1 \leq q \leq Q ) , N_q and
C_q are given separated by a single space.
N_q and C_q represent the working time and the coefficient related to cloning for test case q respectively.

Output

For each test case, Print the maximum number of leaflets Eli- 1 and her clones can hand out modulo 1000000007 ( = 10^9 + 7 ).

Partial Scores

30 points will be awarded for passing the test set satisfying the condition: Q = 1 .

Another 270 points will be awarded for passing the test set without addtional constraints and you can get 300 points in total.

Sample Input 1

2
20 8
20 12

Sample Output 1

24
20

For the first test case, while only 20 leaflets can be handed out without cloning,
24 leaflets can be handed out by cloning first and two people handing out12 leaflets each.

For the second test case, since two people can only hand out 8 leaflets each if Eli- 1 clones, she should hand out 20 leaflets without cloning.

Sample Input 2

1
20 3

Sample Output 2

67

One way of handing out 67 leaflets is like the following image. Each black line means cloning, and each red line means handing out.

This case satisfies the constraint of the partial score.

Sample Input 3

1
200 1

Sample Output 3

148322100

Note that the value modulo 1000000007 ( 10^9 + 7 ) must be printed.

This case satisfies the constraint of the partial score.
Score : 300 points

Problem Statement

Mikan's birthday is coming soon. Since Mikan likes graphs very much, Aroma decided to give her a undirected graph this year too.

Aroma bought a connected undirected graph, which consists of n vertices and n edges.
The vertices are numbered from 1 to n and for each i(1 \leq i \leq n), vertex i and vartex a_i are connected with an undirected edge.
Aroma thinks it is not interesting only to give the ready-made graph and decided to paint it.
Count the number of ways to paint the vertices of the purchased graph in k colors modulo 10^9 + 7.
Two ways are considered to be the same if the graph painted in one way can be identical to the graph painted in the other way by permutating the numbers of the vertices.

Constraints

1 \leq n \leq 10^5

1 \leq k \leq 10^5

1 \leq a_i \leq n (1 \leq i \leq n)

The given graph is connected

The given graph contains no self-loops or multiple edges.

Input

Each data set is given in the following format from the standard input.

n k
a_1
:
a_n

Output

Output the number of ways to paint the vertices of the given graph in k colors modulo 10^9 + 7 in one line.

Sample Input 1

4 2
2
3
1
1

Sample Output 1

12

Sample Input 2

4 4
2
3
4
1

Sample Output 2

55

Sample Input 3

10 5
2
3
4
1
1
1
2
3
3
4

Sample Output 3

926250
Score : 300 points

Story

I am a Summoner who can invoke monsters. I can embody the pictures of monsters drawn on sketchbooks.

I recently have received a request from Primate Research Institute of Kyoto University to invoke "Hundred Eyes Monster".

What an great honor it is to contribute to the prosperity of the primate research of Kyoto University!
I decided to paint an extremely high-quarity monster to the best of my ability.

I have already painted the forehead and mouth and this time I am going to paint as many eyes as possible between the forehead and mouth.
Well...I cannot wait to see the complete painting.

Problem Statement

Two circles A, B are given on a two-dimensional plane.
The coordinate of the center and radius of circle A is (x_A, y_A) and r_A respectively.
The coordinate of the center and radius of circle B is (x_B, y_B) and r_B respectively.
These two circles have no intersection inside.
Here, we consider a set of circles S that satisfies the following conditions.

Each circle in S touches both A and B, and does not have common points with them inside the circle.

Any two different circles in S have no common points inside them.

Write a program that finds the maximum number of elements in S.

Constraints

1 \leq T \leq 50000

-10^5 \leq x_A \leq 10^5

-10^5 \leq y_A \leq 10^5

-10^5 \leq x_B \leq 10^5

-10^5 \leq y_B \leq 10^5

1 \leq r_A \leq 10^5

1 \leq r_B \leq 10^5

{r_A}^2 + {r_B}^2 < (x_A - x_B)^2 + (y_A - y_B)^2

All values given as input are integers.

It is guaranteed that, when the radiuses of A and B change by 0.0005, the answer does not change.

Input

The input consists of multiple test cases and is given from Standard Input in the following format:

T
testcase_1
:
testcase_T

Each test case is given with the following format.

x_A y_A r_A x_B y_B r_B

Output

The output consists of T lines.
On line i (1 \leq i \leq T), putput the maximum number of elements in S in i-th test case.

Sample Input 1

4
0 -3 2 0 3 2
0 0 9 8 8 2
0 0 9 10 10 5
0 0 707 1000 1000 707

Sample Output 1

3
10
21
180

You can arrange circles ,for example, like the following images in Sample 1, 2.
Score : 300 points

Problem Statement

We have a string X, which has an even number of characters. Half the characters are S, and the other half are T.

Takahashi, who hates the string ST, will perform the following operation 10^{10000} times:

Among the occurrences of ST in X as (contiguous) substrings, remove the leftmost one. If there is no occurrence, do nothing.

Find the eventual length of X.

Constraints

2 ≦ |X| ≦ 200,000

The length of X is even.

Half the characters in X are S, and the other half are T.

Partial Scores

In test cases worth 200 points, |X| ≦ 200.

Input

The input is given from Standard Input in the following format:

X

Output

Print the eventual length of X.

Sample Input 1

TSTTSS

Sample Output 1

4

In the 1-st operation, the 2-nd and 3-rd characters of TSTTSS are removed.
X becomes TTSS, and since it does not contain ST anymore, nothing is done in the remaining 10^{10000}-1 operations.
Thus, the answer is 4.

Sample Input 2

SSTTST

Sample Output 2

0

X will eventually become an empty string: SSTTST ⇒ STST ⇒ ST ⇒ ``.

Sample Input 3

TSSTTTSS

Sample Output 3

4

X will become: TSSTTTSS ⇒ TSTTSS ⇒ TTSS.
Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85
Score : 700 points

Problem Statement

Aoki loves numerical sequences and trees.

One day, Takahashi gave him an integer sequence of length N, a_1, a_2, ..., a_N, which made him want to construct a tree.

Aoki wants to construct a tree with N vertices numbered 1 through N, such that for each i = 1,2,...,N, the distance between vertex i and the farthest vertex from it is a_i, assuming that the length of each edge is 1.

Determine whether such a tree exists.

Constraints

2 ≦ N ≦ 100

1 ≦ a_i ≦ N-1

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If there exists a tree that satisfies the condition, print Possible. Otherwise, print Impossible.

Sample Input 1

5
3 2 2 3 3

Sample Output 1

Possible

The diagram above shows an example of a tree that satisfies the conditions. The red arrows show paths from each vertex to the farthest vertex from it.

Sample Input 2

3
1 1 2

Sample Output 2

Impossible

Sample Input 3

10
1 2 2 2 2 2 2 2 2 2

Sample Output 3

Possible

Sample Input 4

10
1 1 2 2 2 2 2 2 2 2

Sample Output 4

Impossible

Sample Input 5

6
1 1 1 1 1 5

Sample Output 5

Impossible

Sample Input 6

5
4 3 2 3 4

Sample Output 6

Possible
Score : 900 points

Problem Statement

Snuke loves permutations. He is making a permutation of length N.

Since he hates the integer K, his permutation will satisfy the following:

Let the permutation be a_1, a_2, ..., a_N. For each i = 1,2,...,N, |a_i - i| \neq K.

Among the N! permutations of length N, how many satisfies this condition?

Since the answer may be extremely large, find the answer modulo 924844033(prime).

Constraints

2 ≦ N ≦ 2000

1 ≦ K ≦ N-1

Input

The input is given from Standard Input in the following format:

N K

Output

Print the answer modulo 924844033.

Sample Input 1

3 1

Sample Output 1

2

2 permutations (1, 2, 3), (3, 2, 1) satisfy the condition.

Sample Input 2

4 1

Sample Output 2

5

5 permutations (1, 2, 3, 4), (1, 4, 3, 2), (3, 2, 1, 4), (3, 4, 1, 2), (4, 2, 3, 1) satisfy the condition.

Sample Input 3

4 2

Sample Output 3

9

Sample Input 4

4 3

Sample Output 4

14

Sample Input 5

425 48

Sample Output 5

756765083