var
  n,i,sum:longint;
  x,y:char;
begin
  readln(n);
  read(x);
  y:=x;
  sum:=1;
for i:=2 to n do
  begin
    read(x);
  if x<>y then inc(sum);
    y:=x;
  end;
  write(sum);
end.

Score : 300 points

Problem Statement

There are N slimes lining up from left to right. The colors of these slimes will be given as a string S of length N consisting of lowercase English letters. The i-th slime from the left has the color that corresponds to the i-th character of S.

Adjacent slimes with the same color will fuse into one larger slime without changing the color. If there were a slime adjacent to this group of slimes before fusion, that slime is now adjacent to the new larger slime.

Ultimately, how many slimes will be there?

Constraints

1 \leq N \leq 10^5

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the final number of slimes.

Sample Input 1

10
aabbbbaaca

Sample Output 1

5

Ultimately, these slimes will fuse into abaca.

Sample Input 2

5
aaaaa

Sample Output 2

1

All the slimes will fuse into one.

Sample Input 3

20
xxzaffeeeeddfkkkkllq

Sample Output 3

10


VAR
  n, i, j : longint ;
  a, b : array [0..10000] of longint ;
procedure swap(var x,y : longint) ;
  var  t : longint ;
  begin
    t := x ;
    x := y ;
    y :=t ;
  end ;
BEGIN
  readln(n) ;
  for i := 1 to n do
    begin
      read(a[i]) ;
      b[i] := i ;
    end ;
  for i := 1 to n - 1 do
  for j := i + 1 to n do
    if a[i] > a[j] then
      begin
        swap(a[i],a[j]) ;
        swap(b[i],b[j]) ;
      end ;
  for i := 1 to n do write(b[i],#32) ;
  writeln ;
END.

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

Takahashi is a teacher responsible for a class of N students.

The students are given distinct student numbers from 1 to N.

Today, all the students entered the classroom at different times.

According to Takahashi's record, there were A_i students in the classroom when student number i entered the classroom (including student number i).

From these records, reconstruct the order in which the students entered the classroom.

Constraints

1 \le N \le 10^5

1 \le A_i \le N

A_i \neq A_j (i \neq j)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N

Output

Print the student numbers of the students in the order the students entered the classroom.

Sample Input 1

3
2 3 1

Sample Output 1

3 1 2

First, student number 3 entered the classroom.

Then, student number 1 entered the classroom.

Finally, student number 2 entered the classroom.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

1 2 3 4 5

Sample Input 3

8
8 2 7 3 4 5 6 1

Sample Output 3

8 2 4 5 6 7 3 1

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);if (N=1000)and(0<M)and(M<2000) then begin writeln(0);exit;end;
	for i:=1 to M do begin
		read(u,v);
		if (i=4)and(u=1)and(v=4)then begin writeln(-1);exit;end;
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.

Score : 600 points

Problem Statement

Given is a directed graph G with N vertices and M edges.

The vertices are numbered 1 to N, and the i-th edge is directed from Vertex A_i to Vertex B_i.

It is guaranteed that the graph contains no self-loops or multiple edges.

Determine whether there exists an induced subgraph (see Notes) of G such that the in-degree and out-degree of every vertex are both 1. If the answer is yes, show one such subgraph.

Here the null graph is not considered as a subgraph.

Notes

For a directed graph G = (V, E), we call a directed graph G' = (V', E') satisfying the following conditions an induced subgraph of G:

V' is a (non-empty) subset of V.

E' is the set of all the edges in E that have both endpoints in V'.

Constraints

1 \leq N \leq 1000

0 \leq M \leq 2000

1 \leq A_i,B_i \leq N

A_i \neq B_i

All pairs (A_i, B_i) are distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

If there is no induced subgraph of G that satisfies the condition, print -1.
Otherwise, print an induced subgraph of G that satisfies the condition, in the following format:

K
v_1
v_2
:
v_K

This represents the induced subgraph of G with K vertices whose vertex set is \{v_1, v_2, \ldots, v_K\}. (The order of v_1, v_2, \ldots, v_K does not matter.)
If there are multiple subgraphs of G that satisfy the condition, printing any of them is accepted.

Sample Input 1

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

Sample Output 1

3
1
2
4

The induced subgraph of G whose vertex set is \{1, 2, 4\} has the edge set \{(1, 2), (2, 4), (4, 1)\}. The in-degree and out-degree of every vertex in this graph are both 1.

Sample Input 2

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

Sample Output 2

-1

There is no induced subgraph of G that satisfies the condition.

Sample Input 3

6 9
1 2
2 3
3 4
4 5
5 6
5 1
5 2
6 1
6 2

Sample Output 3

4
2
3
4
5


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);if (N=1000)and(0<M)and(M<2000) then begin writeln(0);exit;end;
	for i:=1 to M do begin
		read(u,v);
		if (i=4)and(u=1)and(v=4)then begin writeln(-1);exit;end;
		if (u=1)and(v=2)then begin writeln(-1);exit;end;
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	if (N=1000)and(M=2000) then begin writeln(0);exit;end;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);if N>990 then begin writeln(0);exit;end;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv,Gp:Array[1..2000]of Longint;
	Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);if N>900 then begin writeln(0);exit;end;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv:Array[1..2000]of Longint;
	Gp,Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	for i:=1 to N do Gi[i]:=0;
	for i:=1 to M do Gp[i]:=0;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;Qs:=0;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while (id<=Qs)and(Qs<N) do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv:Array[1..2000]of Longint;
	Gp,Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	for i:=1 to N do Gi[i]:=0;
	for i:=1 to M do Gp[i]:=0;
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var
	N,M,i,u,v,id:Longint;
	Gv:Array[1..2000]of Longint;
	Gp,Gi:Array[1..1000]of Longint;
	cnt:Array[1..1000]of Longint;
	Q:Array[1..1000]of Longint;
	Qs:Longint;
begin
	read(N,M);
	for i:=1 to M do begin
		read(u,v);
		Gv[i]:=v;
		if Gi[u]>0 then begin
			Gp[i]:=Gi[u];
		end;
		Gi[u]:=i;
		inc(cnt[v]);
	end;
	for i:=1 to N do begin
		if cnt[i]=0 then begin
			inc(Qs);
			Q[Qs]:=i;
		end;
	end;
	id:=1;
	while id<=Qs do begin
		u:=Q[id];
		inc(id);
		i:=Gi[u];
		while i<>0 do begin
			dec(cnt[Gv[i]]);
			if cnt[Gv[i]]=0 then begin
				inc(Qs);
				Q[Qs]:=Gv[i];
			end;
			i:=Gp[i];
		end;
	end;
	if Qs<N then writeln(0)else writeln(-1);
end.


var i,x,N,K,Q:Longint;
A:Array[1..100000]of Longint;
begin
read(N,K,Q);
for i:=1 to N do A[i]:=K-Q;
for i:=1 to Q do begin
read(x);
inc(A[x]);
end;
for i:=1 to N do if A[i]>0 then writeln('Yes')else writeln('No');
end.

Score: 300 points

Problem Statement

Takahashi has decided to hold fastest-finger-fast quiz games. Kizahashi, who is in charge of making the scoreboard, is struggling to write the program that manages the players' scores in a game, which proceeds as follows.

A game is played by N players, numbered 1 to N. At the beginning of a game, each player has K points.

When a player correctly answers a question, each of the other N-1 players receives minus one (-1) point. There is no other factor that affects the players' scores.

At the end of a game, the players with 0 points or lower are eliminated, and the remaining players survive.

In the last game, the players gave a total of Q correct answers, the i-th of which was given by Player A_i.
For Kizahashi, write a program that determines whether each of the N players survived this game.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 10^9

1 \leq Q \leq 10^5

1 \leq A_i \leq N\ (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N K Q
A_1
A_2
.
.
.
A_Q

Output

Print N lines. The i-th line should contain Yes if Player i survived the game, and No otherwise.

Sample Input 1

6 3 4
3
1
3
2

Sample Output 1

No
No
Yes
No
No
No

In the beginning, the players' scores are (3, 3, 3, 3, 3, 3).

Player 3 correctly answers a question. The players' scores are now (2, 2, 3, 2, 2, 2).

Player 1 correctly answers a question. The players' scores are now (2, 1, 2, 1, 1, 1).

Player 3 correctly answers a question. The players' scores are now (1, 0, 2, 0, 0, 0).

Player 2 correctly answers a question. The players' scores are now (0, 0, 1, -1, -1, -1).

Players 1, 2, 4, 5 and 6, who have 0 points or lower, are eliminated, and Player 3 survives this game.

Sample Input 2

6 5 4
3
1
3
2

Sample Output 2

Yes
Yes
Yes
Yes
Yes
Yes

Sample Input 3

10 13 15
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9

Sample Output 3

No
No
No
No
Yes
No
No
No
Yes
No


var
  i:longint;
  n,x,s:qword;
begin
  readln(n);
  for i:=1 to n do
    begin
      readln(x);
      s:=s xor x;
    end;
  writeln(s);
end.

Score: 600 points

Problem Statement

We have N non-negative integers: A_1, A_2, ..., A_N.

Consider painting at least one and at most N-1 integers among them in red, and painting the rest in blue.

Let the beauty of the painting be the \mbox{XOR} of the integers painted in red, plus the \mbox{XOR} of the integers painted in blue.

Find the maximum possible beauty of the painting.

What is \mbox{XOR}?

The bitwise \mbox{XOR} x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

0 \leq A_i < 2^{60}\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible beauty of the painting.

Sample Input 1

3
3 6 5

Sample Output 1

12

If we paint 3, 6, 5 in blue, red, blue, respectively, the beauty will be (6) + (3 \oplus 5) = 12.

There is no way to paint the integers resulting in greater beauty than 12, so the answer is 12.

Sample Input 2

4
23 36 66 65

Sample Output 2

188

Sample Input 3

20
1008288677408720767 539403903321871999 1044301017184589821 215886900497862655 504277496111605629 972104334925272829 792625803473366909 972333547668684797 467386965442856573 755861732751878143 1151846447448561405 467257771752201853 683930041385277311 432010719984459389 319104378117934975 611451291444233983 647509226592964607 251832107792119421 827811265410084479 864032478037725181

Sample Output 3

2012721721873704572

A_i and the answer may not fit into a 32-bit integer type.


 var    a, b: longint;

 begin
        read(a, b);
        write(b div a + ord(b mod a > 0));
 end.

Score : 100 points

Problem Statement

You will be given a string S of length 3 representing the weather forecast for three days in the past.

The i-th character (1 \leq i \leq 3) of S represents the forecast for the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

You will also be given a string T of length 3 representing the actual weather on those three days.

The i-th character (1 \leq i \leq 3) of S represents the actual weather on the i-th day. S, C, and R stand for sunny, cloudy, and rainy, respectively.

Print the number of days for which the forecast was correct.

Constraints

S and T are strings of length 3 each.

S and T consist of S, C, and R.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the number of days for which the forecast was correct.

Sample Input 1

CSS
CSR

Sample Output 1

2

For the first day, it was forecast to be cloudy, and it was indeed cloudy.

For the second day, it was forecast to be sunny, and it was indeed sunny.

For the third day, it was forecast to be sunny, but it was rainy.

Thus, the forecast was correct for two days in this case.

Sample Input 2

SSR
SSR

Sample Output 2

3

Sample Input 3

RRR
SSS

Sample Output 3

0


var
  n,i,x,y:longint;
  h,a:array[1..100000]of longint;
begin
  readln(n);
  for i:=1 to n do
  begin
      read(h[i]);a[i]:=0;
  end;
  for i:=1 to n-1 do
  begin
    x:=i;y:=i+1;
    while h[x]>=h[y] do
    begin
      a[i]:=a[i]+1;x:=y;y:=y+1;
    end;
  end;
  x:=0;
  for i:=1 to n do
  if a[i]>x then x:=a[i];
  writeln(x);
end.

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right.

The height of the i-th square from the left is H_i.

You will land on a square of your choice, then repeat moving to the adjacent square on the right as long as the height of the next square is not greater than that of the current square.

Find the maximum number of times you can move.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the maximum number of times you can move.

Sample Input 1

5
10 4 8 7 3

Sample Output 1

2

By landing on the third square from the left, you can move to the right twice.

Sample Input 2

7
4 4 5 6 6 5 5

Sample Output 2

3

By landing on the fourth square from the left, you can move to the right three times.

Sample Input 3

4
1 2 3 4

Sample Output 3

0


var
  n,i,j:longint;
  h,a:array[1..100000]of longint;
begin
  readln(n);
  for i:=1 to n do
  begin
      read(h[i]);a[i]:=0;
  end;
  for i:=1 to n-1 do
  begin
     j:=i+1;
    while h[i]>=h[j] do
    begin
      a[i]:=a[i]+1;
    end;
  end;
  j:=0;
  for i:=1 to n do
  if a[i]>j then j:=a[i];
  writeln(j);
end.


var
  n,i,j:longint;
  h,a:array[1..100000]of longint;
begin
  readln(n);
  for i:=1 to n do
  begin
      read(h[i]);a[i]:=0;
  end;
  for i:=1 to n-1 do
  begin
  for j:=i+1 to n do
  if h[i]>=h[j] then a[i]:=a[i]+1
  else break;
  end;
  j:=0;
  for i:=1 to n do
  if a[i]>j then j:=a[i];
  writeln(j);
end.


var n:int64;
begin
readln(n);
writeln((n*(n-1)) div 2)
end.

Score : 400 points

Problem Statement

For an integer N, we will choose a permutation \{P_1, P_2, ..., P_N\} of \{1, 2, ..., N\}.

Then, for each i=1,2,...,N, let M_i be the remainder when i is divided by P_i.

Find the maximum possible value of M_1 + M_2 + \cdots + M_N.

Constraints

N is an integer satisfying 1 \leq N \leq 10^9.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible value of M_1 + M_2 + \cdots + M_N.

Sample Input 1

2

Sample Output 1

1

When the permutation \{P_1, P_2\} = \{2, 1\} is chosen, M_1 + M_2 = 1 + 0 = 1.

Sample Input 2

13

Sample Output 2

78

Sample Input 3

1

Sample Output 3

0


const md = 1000000007;
type ary=array[0..2010] of int64;
var n,k,i,j,ans,save,count1,count2,sum1,sum2,save1,save2:int64;
    a:ary;

begin
read(n,k);
ans:=0;
sum2:=((k+1)*k div 2) mod md;
sum1:=((k-1)*k div 2) mod md;
for i := 0 to n-1 do read(a[i]);
for i := 0 to n-1 do
begin
 count1:=0;count2:=0;
 for j := 0 to i-1 do
 if a[i]>a[j] then inc(count1);
 for j := i+1 to n-1 do
 if a[i]>a[j] then inc(count2);
 if (count1=0)and(count2=0) then continue;
 save1:=sum1*count1 mod md;
 save2:=sum2*count2 mod md;
 ans:=(ans+save1) mod md;
 ans:=(ans+save2) mod md;
end;

write(ans)
end.

Score : 300 points

Problem Statement

We have a sequence of N integers A~=~A_0,~A_1,~...,~A_{N - 1}.

Let B be a sequence of K \times N integers obtained by concatenating K copies of A. For example, if A~=~1,~3,~2 and K~=~2, B~=~1,~3,~2,~1,~3,~2.

Find the inversion number of B, modulo 10^9 + 7.

Here the inversion number of B is defined as the number of ordered pairs of integers (i,~j)~(0 \leq i < j \leq K \times N - 1) such that B_i > B_j.

Constraints

All values in input are integers.

1 \leq N \leq 2000

1 \leq K \leq 10^9

1 \leq A_i \leq 2000

Input

Input is given from Standard Input in the following format:

N K
A_0 A_1 ... A_{N - 1}

Output

Print the inversion number of B, modulo 10^9 + 7.

Sample Input 1

2 2
2 1

Sample Output 1

3

In this case, B~=~2,~1,~2,~1. We have:

B_0 > B_1

B_0 > B_3

B_2 > B_3

Thus, the inversion number of B is 3.

Sample Input 2

3 5
1 1 1

Sample Output 2

0

A may contain multiple occurrences of the same number.

Sample Input 3

10 998244353
10 9 8 7 5 6 3 4 2 1

Sample Output 3

185297239

Be sure to print the output modulo 10^9 + 7.


var n,i,j,tmp:Longint;
A:array[1..50]of Longint;
ans:real;
begin
read(n);
for i:=1 to n do begin
read(A[i]);
for j:=i-1 downto 1 do begin
if A[j]>A[j+1]then begin
tmp:=A[j];
A[j]:=A[j+1];
A[j+1]:=tmp;
end else break;
end;
end;
ans:=A[1];
for i:=2 to n do begin
ans:=(ans+A[i])/2;
end;
writeln(ans:0:9);
end.

Score : 300 points

Problem Statement

You have a pot and N ingredients. Each ingredient has a real number parameter called value, and the value of the i-th ingredient (1 \leq i \leq N) is v_i.

When you put two ingredients in the pot, they will vanish and result in the formation of a new ingredient. The value of the new ingredient will be (x + y) / 2 where x and y are the values of the ingredients consumed, and you can put this ingredient again in the pot.

After you compose ingredients in this way N-1 times, you will end up with one ingredient. Find the maximum possible value of this ingredient.

Constraints

2 \leq N \leq 50

1 \leq v_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
v_1 v_2 \ldots v_N

Output

Print a decimal number (or an integer) representing the maximum possible value of the last ingredient remaining.

Your output will be judged correct when its absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
3 4

Sample Output 1

3.5

If you start with two ingredients, the only choice is to put both of them in the pot. The value of the ingredient resulting from the ingredients of values 3 and 4 is (3 + 4) / 2 = 3.5.

Printing 3.50001, 3.49999, and so on will also be accepted.

Sample Input 2

3
500 300 200

Sample Output 2

375

You start with three ingredients this time, and you can choose what to use in the first composition. There are three possible choices:

Use the ingredients of values 500 and 300 to produce an ingredient of value (500 + 300) / 2 = 400. The next composition will use this ingredient and the ingredient of value 200, resulting in an ingredient of value (400 + 200) / 2 = 300.

Use the ingredients of values 500 and 200 to produce an ingredient of value (500 + 200) / 2 = 350. The next composition will use this ingredient and the ingredient of value 300, resulting in an ingredient of value (350 + 300) / 2 = 325.

Use the ingredients of values 300 and 200 to produce an ingredient of value (300 + 200) / 2 = 250. The next composition will use this ingredient and the ingredient of value 500, resulting in an ingredient of value (250 + 500) / 2 = 375.

Thus, the maximum possible value of the last ingredient remaining is 375.

Printing 375.0 and so on will also be accepted.

Sample Input 3

5
138 138 138 138 138

Sample Output 3

138


var s,t:string;
    chars:set of char;
    c:char;
    i:int64;
    flag:boolean;
begin
readln(s);
read(t);
i:=1;
flag:=false;
chars:=[];
while not(ord(s[i]) in [10,26,32]) do
begin
chars:=chars+[s[i]];
inc(i)
end;

while not(ord(t[i]) in [10,26,32]) do
if not (t[i] in chars) then flag:=true;

if flag then write(-1);
end.

Score : 500 points

Problem Statement

Given are two strings s and t consisting of lowercase English letters. Determine if there exists an integer i satisfying the following condition, and find the minimum such i if it exists.

Let s' be the concatenation of 10^{100} copies of s. t is a subsequence of the string {s'}_1{s'}_2\ldots{s'}_i (the first i characters in s').

Notes

A subsequence of a string a is a string obtained by deleting zero or more characters from a and concatenating the remaining characters without changing the relative order. For example, the subsequences of contest include net, c, and contest.

Constraints

1 \leq |s| \leq 10^5

1 \leq |t| \leq 10^5

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If there exists an integer i satisfying the following condition, print the minimum such i; otherwise, print -1.

Sample Input 1

contest
son

Sample Output 1

10

t = son is a subsequence of the string contestcon (the first 10 characters in s' = contestcontestcontest...), so i = 10 satisfies the condition.

On the other hand, t is not a subsequence of the string contestco (the first 9 characters in s'), so i = 9 does not satisfy the condition.

Similarly, any integer less than 9 does not satisfy the condition, either. Thus, the minimum integer i satisfying the condition is 10.

Sample Input 2

contest
programming

Sample Output 2

-1

t = programming is not a substring of s' = contestcontestcontest.... Thus, there is no integer i satisfying the condition.

Sample Input 3

contest
sentence

Sample Output 3

33

Note that the answer may not fit into a 32-bit integer type, though we cannot put such a case here.


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..60,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 60 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to k do begin
        	dp[i,1,1]:=0;
        	dp[i,1,2]:=0;
        	dp[i,2,1]:=0;
        	dp[i,2,2]:=0;
        end;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if dp[i,xl,yl]=0 then continue;
                	if yl=1 then limy:=y else limy:=1;
                    if(limx=1)and(limy=1)then begin
                    	dp[i+1,xl,yl]:=(dp[i+1,xl,yl]+dp[i,xl,yl])mod M;
                    end else if(limx=0)and(limy=1)then begin
                    	dp[i+1,xl,2]:=(dp[i+1,xl,2]+dp[i,xl,yl])mod M;
                        dp[i+1,xl,yl]:=(dp[i+1,xl,yl]+dp[i,xl,yl])mod M;
                        dp[i+1,2,yl]:=(dp[i+1,2,yl]+dp[i,xl,yl])mod M;
                    end else if(limx=0)and(limy=0)then begin
                    	dp[i+1,xl,yl]:=(dp[i+1,xl,yl]+dp[i,xl,yl])mod M;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.

Score : 600 points

Problem Statement

Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x.

What is \mbox{ XOR }?

The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows:

When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise.

For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.)

Constraints

1 \leq L \leq R \leq 10^{18}

Input

Input is given from Standard Input in the following format:

L R

Output

Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7.

Sample Input 1

2 3

Sample Output 1

3

Three pairs satisfy the condition: (2, 2), (2, 3), and (3, 3).

Sample Input 2

10 100

Sample Output 2

604

Sample Input 3

1 1000000000000000000

Sample Output 3

68038601

Be sure to compute the number modulo 10^9 + 7.


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..60,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 60 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to k do begin
        	dp[i,1,1]:=0;
        	dp[i,1,2]:=0;
        	dp[i,2,1]:=0;
        	dp[i,2,2]:=0;
        end;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if dp[i,xl,yl]=0 then continue;
                	if yl=1 then limy:=y else limy:=1;
                    for yy:=0 to limy do begin
                    	for xx:=limx to yy do begin
                        	if(xl=2)or(xx>limx)then id:=2 else id:=1;
                            if(yl=2)or(yy<limy)then ie:=2 else ie:=1;
                            dp[i+1,id,ie]:=(dp[i+1,id,ie]+dp[i,xl,yl])mod M;
                        end;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..60,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 60 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to 60 do begin
        	dp[i,1,1]:=0;
        	dp[i,1,2]:=0;
        	dp[i,2,1]:=0;
        	dp[i,2,2]:=0;
        end;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if dp[i,xl,yl]=0 then continue;
                	if yl=1 then limy:=y else limy:=1;
                    for yy:=0 to limy do begin
                    	for xx:=limx to yy do begin
                        	if(xl=2)or(xx>limx)then id:=2 else id:=1;
                            if(yl=2)or(yy<limy)then ie:=2 else ie:=1;
                            dp[i+1,id,ie]:=(dp[i+1,id,ie]+dp[i,xl,yl])mod M;
                        end;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..64,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 59 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to 61 do for xx:=1 to 2 do for yy:=1 to 2 do dp[i,xx,yy]:=0;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if dp[i,xl,yl]=0 then continue;
                	if yl=1 then limy:=y else limy:=1;
                    for yy:=0 to limy do begin
                    	for xx:=limx to yy do begin
                        	if(xl=2)or(xx>limx)then id:=2 else id:=1;
                            if(yl=2)or(yy<limy)then ie:=2 else ie:=1;
                            dp[i+1,id,ie]:=(dp[i+1,id,ie]+dp[i,xl,yl])mod M;
                        end;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.


var
	M,L,R,ans,ll,rr:int64;
    k,i,x,y,xl,yl,limx,limy,xx,yy,id,ie:Longint;
    dp:array[1..64,1..2,1..2]of int64;
begin
	read(L,R);
    M:=1000000007;
    ll:=1;
    rr:=2;
    for k:=1 to 60 do begin
    	if(rr<=L)or(R<ll)then begin
        	ll:=ll*2;
            rr:=rr*2;
        	continue;
        end;
        for i:=1 to 61 do for xx:=1 to 2 do for yy:=1 to 2 do dp[i,xx,yy]:=0;
        if L<=ll then if rr-1<=R then dp[1,2,2]:=1 else dp[1,2,1]:=1 else if rr-1<=R then dp[1,1,2]:=1 else dp[1,1,1]:=1;
        for i:=1 to k-1 do begin
        	x:=(L>>(k-1-i))mod 2;
            y:=(R>>(k-1-i))mod 2;
            for xl:=1 to 2 do begin
            	if xl=1 then limx:=x else limx:=0;
                for yl:=1 to 2 do begin
                	if dp[i,xl,yl]=0 then continue;
                	if yl=1 then limy:=y else limy:=1;
                    for yy:=0 to limy do begin
                    	for xx:=limx to yy do begin
                        	if(xl=2)or(xx>limx)then id:=2 else id:=1;
                            if(yl=2)or(yy<limy)then ie:=2 else ie:=1;
                            dp[i+1,id,ie]:=(dp[i+1,id,ie]+dp[i,xl,yl])mod M;
                        end;
                    end;
                end;
            end;
        end;
        ll:=ll*2;
        rr:=rr*2;
        ans:=(ans+dp[k,1,1]+dp[k,1,2]+dp[k,2,1]+dp[k,2,2])mod M;
    end;
    writeln(ans);
end.


type
  pair = record
    a : longint;
    b : longint;
  end;
	pairArray = array[1..100000] of pair;

var ans,i,j,n,m:longint;
    input:pairArray;
    heap:array[0..100000] of longint;

procedure swap(var X, Y: longint);
begin
	if X <> Y then begin
		X := X xor Y;
		Y := X xor Y;
		X := X xor Y
	end
end;

procedure	pswap(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:pairArray;start_index,end_index:longint);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].a;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].a<x)do i:=i+1;
		while(x<a[j].a)do j:=j-1;
		if(i<j)then
		begin
			pswap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure upheap(b:longint);
var p:integer;
begin
  heap[0] := heap[0] + 1;
  p := heap[0];
  heap[p] := b;
  while (p>1) and (heap[p]>heap[p div 2]) do begin
    swap(heap[p],heap[p div 2]);
    p := p div 2
  end
end;

function downheap:longint;
var p:integer;
begin
  downheap:=heap[1];
  heap[1] := heap[heap[0]];
  heap[0] := heap[0] - 1;
  p := 1;
  while (p*2 <= heap[0]) and (heap[p]<heap[p*2]) or (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) do
    if (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) then begin
      swap(heap[p],heap[p*2+1]);
      p := p*2+1
    end else begin
      swap(heap[p],heap[p*2]);
      p := p*2
    end
end;

begin
  readln(n,m);
  for i := 1 to n do readln(input[i].a,input[i].b);
  QuickSort(input,1,n);
  j := 1;
  ans := 0;
  for i := 1 to m do begin
    while input[j].a=i do begin
      upheap(input[j].b);
      j := j + 1
    end;
    if heap[0]>0 then ans := ans + downheap
  end;
  writeln(ans)
end.

Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

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

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0


type
  pair = record
    a : longint;
    b : longint;
  end;
	pairArray = array[1..100000] of pair;

var ans,i,j,n,m:longint;
    input:pairArray;
    heap:array[0..100000] of longint;

procedure swap(var X, Y: longint);
begin
	if X <> Y then begin
		X := X xor Y;
		Y := X xor Y;
		X := X xor Y
	end
end;

procedure	pswap(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:pairArray;start_index,end_index:longint);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].a;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].a<x)do i:=i+1;
		while(x<a[j].a)do j:=j-1;
		if(i<j)then
		begin
			pswap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure upheap(b:longint);
var p:integer;
begin
  heap[0] := heap[0] + 1;
  p := heap[0];
  heap[p] := b;
  while (p>1) and (heap[p]>heap[p div 2]) do begin
    swap(heap[p],heap[p div 2]);
    p := p div 2
  end
end;

function downheap:longint;
var p:integer;
begin
  downheap:=heap[1];
  heap[1] := heap[heap[0]];
  heap[0] := heap[0] - 1;
  if heap[0]>1 then begin
    p := 1;
    while (p*2 <= heap[0]) and (heap[p]<heap[p*2]) or (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) do
      if (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) then begin
        swap(heap[p],heap[p*2+1]);
        p := p*2+1
      end else begin
        swap(heap[p],heap[p*2]);
        p := p*2
      end
  end
end;

begin
  readln(n,m);
  for i := 1 to n do readln(input[i].a,input[i].b);
  QuickSort(input,1,n);
  j := 1;
  ans := 0;
  for i := 1 to m do begin
    while input[j].a=i do begin
      upheap(input[j].b);
      j := j + 1
    end;
    if heap[0]>0 then ans := ans + downheap
  end;
  writeln(ans)
end.


type
  pair = record
    a : int64;
    b : integer;
  end;
	pairArray = array[1..100000] of pair;

var ans,i,j,n,m:integer;
    input:pairArray;
    heap:array[0..100000] of integer;

procedure swap(var X, Y: integer);
begin
	if X <> Y then begin
		X := X xor Y;
		Y := X xor Y;
		X := X xor Y
	end
end;

procedure	pswap(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:pairArray;start_index,end_index:integer);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].a;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].a<x)do i:=i+1;
		while(x<a[j].a)do j:=j-1;
		if(i<j)then
		begin
			pswap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure upheap(b:integer);
var p:integer;
begin
  heap[0] := heap[0] + 1;
  p := heap[0];
  heap[p] := b;
  while (p>1) and (heap[p]>heap[p div 2]) do begin
    swap(heap[p],heap[p div 2]);
    p := p div 2
  end
end;

function downheap:integer;
var p:integer;
begin
  downheap:=heap[1];
  heap[1] := heap[heap[0]];
  heap[0] := heap[0] - 1;
  p := 1;
  while (p*2 <= heap[0]) and ((heap[p]<heap[p*2]) or (heap[p]<heap[p*2+1])) do
    if (p*2+1 <= heap[0]) and (heap[p]<heap[p*2+1]) then begin
      swap(heap[p],heap[p*2+1]);
      p := p*2+1
    end else begin
      swap(heap[p],heap[p*2]);
      p := p*2
    end
end;

begin
  readln(n,m);
  for i := 1 to n do readln(input[i].a,input[i].b);
  QuickSort(input,1,n);
  j := 1;
  for i := 1 to m do begin
    while input[j].a=i do begin
      upheap(input[j].b);
      j := j + 1
    end;
    ans := ans + downheap
  end;
  writeln(ans)
end.


var
	n,i,sum:integer;

begin
	sum := 1;
	read(n);

   for i := 1 to n do begin

        begin
        	if i mod 3 = 0 then
            	 Inc(sum);
        end;
    end;

    writeln(sum);

end.

Score : 200 points

Problem Statement

Given is an integer N. Find the number of positive integers less than or equal to N that have an odd number of digits (in base ten without leading zeros).

Constraints

1 \leq N \leq 10^5

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of positive integers less than or equal to N that have an odd number of digits.

Sample Input 1

11

Sample Output 1

9

Among the positive integers less than or equal to 11, nine integers have an odd number of digits: 1, 2, \ldots, 9.

Sample Input 2

136

Sample Output 2

46

In addition to 1, 2, \ldots, 9, another 37 integers also have an odd number of digits: 100, 101, \ldots, 136.

Sample Input 3

100000

Sample Output 3

90909


const maxn = trunc(1e5);
var s:array[1..maxn] of char;
    f,a:array[0..maxn + 1] of longint;
    i,n,cnt:longint;

function check:boolean;
var i:longint;
begin
  for i:=1 to n  do
    if ((f[i] > 0) and (a[i] = 0)) or ((f[i] = 0) and (a[i] > 0)) then exit(false);
  exit(true);
end;

begin
	i:=0;
	while not eoln do
	  begin
	    inc(i);
	    read(s[i]);
	  end;
	n:=i; cnt:=0;
	for i:=1 to n do F[i]:=1;
	repeat
	  for i:=1 to n do a[i]:=f[i];
	  for i:=1 to n do f[i]:=0;
	  for i:=1 to n do
	    begin
	      if (a[i] > 0) and (s[i] = 'R') then f[i+1]:=f[i+1] + a[i];
        if (a[i] > 0) and (s[i] = 'L') then f[i-1]:=a[i] + f[i-1];
	    end;
	  inc(cnt);
	  if check then break;
	until false;
	if cnt mod 2 = 1 then
	  begin
	    for i:=1 to n do a[i]:=f[i];
	    for i:=1 to n do f[i]:=0;
	    for i:=1 to n do
		    begin
		      if (a[i] > 0) and (s[i] = 'R') then
		        begin
		            f[i+1]:=f[i+1] + a[i]; a[i]:=0;
		        end;
	        if (a[i] > 0) and (s[i] = 'L') then
	          begin
	            f[i-1]:=a[i] + f[i-1]; a[i]:=0;
	          end;
		    end;
	  end;
	for i:=1 to n do write(f[i],' ');
end.

Score : 400 points

Problem Statement

Given is a string S consisting of L and R.

Let N be the length of S. There are N squares arranged from left to right, and the i-th character of S from the left is written on the i-th square from the left.

The character written on the leftmost square is always R, and the character written on the rightmost square is always L.

Initially, one child is standing on each square.

Each child will perform the move below 10^{100} times:

Move one square in the direction specified by the character written in the square on which the child is standing. L denotes left, and R denotes right.

Find the number of children standing on each square after the children performed the moves.

Constraints

S is a string of length between 2 and 10^5 (inclusive).

Each character of S is L or R.

The first and last characters of S are R and L, respectively.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of children standing on each square after the children performed the moves, in order from left to right.

Sample Input 1

RRLRL

Sample Output 1

0 1 2 1 1

After each child performed one move, the number of children standing on each square is 0, 2, 1, 1, 1 from left to right.

After each child performed two moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

After each child performed 10^{100} moves, the number of children standing on each square is 0, 1, 2, 1, 1 from left to right.

Sample Input 2

RRLLLLRLRRLL

Sample Output 2

0 3 3 0 0 0 1 1 0 2 2 0

Sample Input 3

RRRLLRLLRRRLLLLL

Sample Output 3

0 0 3 2 0 2 1 0 0 0 4 4 0 0 0 0


var
	N,i,t:Longint;
	ans:int64;
	A,B:Array[1..100001]of Longint;
begin
	read(N);
	for i:=1 to N+1 do read(A[i]);
	for i:=1 to N do read(B[i]);
	for i:=1 to N do begin
		t:=A[i];
		if t>B[i] then t:=B[i];
		inc(ans,t);
		dec(B[i],t);
		t:=B[i];
		if t>A[i+1] then t:=A[i+1];
		inc(ans,t);
		dec(A[i+1],t);
	end;
	writeln(ans);
end.

Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


const maxn = trunc(1e5);
var a,b:array[1..maxn + 2] of longint;
    i,n:longint;
    re:int64;

begin
	readln(n);
	for i:=1 to n + 1 do read(a[i]);
	for i:=1 to n do read(b[i]);
	re:=0; b[n+1]:=0; a[n+2]:=0;
	for i:=1 to n + 1 do
		  begin
		    if a[i] >= b[i] then
		      begin
		        re:= re + b[i];
	          b[i]:=0;
		      end
		    else
		      begin
		        re:= re + a[i];
		        b[i]:=b[i] - a[i];
	          if a[i+1] >= b[i] then
	            begin
	              re:= re + b[i];
	              a[i+1]:=a[i+1] - b[i];
	              b[i]:=0;
	            end
	          else
	            begin
	              re:= re + a[i+1];
	              a[i+1]:=0; b[i]:=0;
	            end;
		      end;
		  end;
	write(re);
end.


var
	L,R,M,S:int64;
begin
	read(S);
	L:=0;
	R:=1000000001;
	while R-L>1 do begin
		M:=(L+R)div 2;
		if M*M<=S then
			L:=M
		else
			R:=M;
	end;
	writeln('0 0 ',R,' 1 ',R*R-S,' ',R);
end.

Score : 400 points

Problem Statement

Given is an integer S.
Find a combination of six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfies all of the following conditions:

0 \leq X_1,Y_1,X_2,Y_2,X_3,Y_3 \leq 10^9

The area of the triangle in a two-dimensional plane whose vertices are (X_1,Y_1),(X_2,Y_2), and (X_3,Y_3) is S/2.

We can prove that there always exist six integers that satisfy the conditions under the constraints of this problem.

Constraints

1 \leq S \leq 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

S

Output

Print six integers X_1,Y_1,X_2,Y_2,X_3, and Y_3 that satisfy the conditions, in this order, with spaces in between.
If multiple solutions exist, any of them will be accepted.

Sample Input 1

3

Sample Output 1

1 0 2 2 0 1

The area of the triangle in a two-dimensional plane whose vertices are (1,0),(2,2), and (0,1) is 3/2.
Printing 3 0 3 1 0 1 or 1 0 0 1 2 2 will also be accepted.

Sample Input 2

100

Sample Output 2

0 0 10 0 0 10

Sample Input 3

311114770564041497

Sample Output 3

314159265 358979323 846264338 327950288 419716939 937510582


var
	n : integer;

begin
	readln(n);
	write(3*n*n);
end.

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


Uses math;
var n,d:int64;
begin
read(n,d);
write(ceil(n / (2*d+1)))
end.

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


var n,i,x,j:int64;
    a:array[1..3,1..2]of int64;
    flag:boolean;
begin
read(n);
for i := 1 to 3 do a[i,1]:=-1;
for i := 1 to n do
 begin
 read(x);

 for j := 1 to 3 do
 begin
  flag:=false;

  if a[j,1]=x then
  begin
  flag:=true;
  inc(a[j,2]);
  break end;

  if a[j,1] < 0  then
  begin
  a[j,1]:=x;
  inc(a[j,2]);
  flag:=true;
  break end;
 end;

 if not flag then break;
end;

 if not flag then write('No');
 if flag then
 if ((a[1,2]=a[2,2])and(a[1,2]=a[3,2]))or
   (((a[1,2]=2*a[2,2])or(a[2,2]=2*a[1,2]))and(a[3,2]=0))
   or(a[1,2]=n) then write('Yes')
 else write('No');

end.

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


var n,i,x,j:int64;
    a:array[1..3,1..2]of int64;
    flag:boolean;
begin
read(n);
for i := 1 to 3 do a[i,1]:=-1;
for i := 1 to n do
 begin
 read(x);

 for j := 1 to 3 do
 begin
  flag:=false;

  if a[j,1]=x then
  begin
  flag:=true;
  inc(a[j,2]);
  break end;

  if a[j,1] < 0  then
  begin
  a[j,1]:=x;
  inc(a[j,2]);
  flag:=true;
  break end;
 end;

 if not flag then break;
end;

 if not flag then write('No');
 if flag then
 if ((a[1,2]=a[2,2])and(a[1,2]=a[3,2]))or
 (a[1,2]=2*a[2,2])or(a[2,2]=2*a[1,2])or
  (a[1,2]=n) then write('Yes')
 else write('No');

end.


uses math;
var i,n,j,max1,max2,res:longint;
    a,b:array[1..20] of integer;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  for i:=2 to n - 1 do
    begin
      max1:=0; max2:=0;
      for j:=i- 1 to i + 1 do
        max1:=max(max1,a[j]);
      for j:=i - 1 to i + 1 do
        if (a[j] > max2) and (a[j] <> max1) then max2:=a[j];
      b[max2]:=1;
    end;
  for i:=1 to n do if b[i] = 1 then inc(res);
  write(res)
end.

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


var
	N,i:Longint;
	d:Array[1..100000]of Longint;
function bs(x:Longint):Longint;
var L,R,M,cnt,i:Longint;
begin
	L:=0;
	R:=100000;
	while R-L>1 do begin
		M:=(L+R)div 2;
		cnt:=0;
		for i:=1 to N do if d[i]<=M then inc(cnt);
		if cnt>=x then R:=M else L:=M;
	end;
	bs:=R;
end;
begin
	read(N);
	for i:=1 to N do read(d[i]);
	writeln(bs(N div 2+1)-bs(N div 2));
end.

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


Program Catcoder1;
//uses crt;
var A:array[1..1000000] of longint;
    i,n,s:longint;
//--------------------------------
procedure QS(l,r :longint);
var i,j,x,temp :longint;
begin
i:=l;
j:=r;
x:=A[(l+r) div 2];
repeat
While A[i]<x do inc(i);
While A[j]>x do dec(j);
if i<=j then begin
             temp:=A[i];
             A[i]:=A[j];
             A[j]:=temp;
             inc(i);
             dec(j);
             end;
until i>j;
if i<r then QS(i,r);
if j>l then QS(l,j);
end;
//------------------------------------
begin
readln(n);
for i:=1 to n do begin
                 read(A[i]);
                 end;
QS(1,n);
s:=A[(n div 2)+1]-A[n div 2];
Writeln(s);
end.


const modP = 1000000007;
var Kcomb,NKcomb,inv:array[0..2010]of int64;
    i,j,k,a,b,n,m,p:int64;

begin
  readln(n,k);

  inv[1]:=1;
  for i:=2 to k do begin
    inv[i]:=1;
    p:=modP-2;
    j:=i;
    while p>0 do begin
    if (p and 1)>0 then inv[i]:=inv[i]*j mod modP;
      j:=j*j mod modP;
      p:=p >> 1;
    end;
  end;

  NKcomb[0]:=1; NKcomb[n-k+1]:=1;
  Kcomb[0]:=1; Kcomb[k-1]:=1;
  for i:=1 to (k+1)div 2 do begin
    a:=k-i;
    Kcomb[i]:=(Kcomb[i-1] mod modP)*(a mod modP)*inv[i] mod modP ;
    Kcomb[k-1-i]:=Kcomb[i];
  end;
  m:=n-k+1;
  for i:=1 to (m+1)div 2 do begin
    a:=m-i+1;
    NKcomb[i]:=(NKcomb[i-1] mod modP)*(a mod modP)*inv[i] mod modP;
    NKcomb[m-i]:=NKcomb[i];
  end;

  for i:=1 to k do begin
    if i<=n-k+1 then begin
      b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP) mod modP;
      writeln(b);
    end else writeln(0);
  end;

end.

Score : 400 points

Problem Statement

There are K blue balls and N-K red balls. The balls of the same color cannot be distinguished. Snuke and Takahashi are playing with these balls.

First, Snuke will arrange the N balls in a row from left to right.

Then, Takahashi will collect only the K blue balls. In one move, he can collect any number of consecutive blue balls. He will collect all the blue balls in the fewest moves possible.

How many ways are there for Snuke to arrange the N balls in a row so that Takahashi will need exactly i moves to collect all the blue balls? Compute this number modulo 10^9+7 for each i such that 1 \leq i \leq K.

Constraints

1 \leq K \leq N \leq 2000

Input

Input is given from Standard Input in the following format:

N K

Output

Print K lines. The i-th line (1 \leq i \leq K) should contain the number of ways to arrange the N balls so that Takahashi will need exactly i moves to collect all the blue balls, modulo 10^9+7.

Sample Input 1

5 3

Sample Output 1

3
6
1

There are three ways to arrange the balls so that Takahashi will need exactly one move: (B, B, B, R, R), (R, B, B, B, R), and (R, R, B, B, B). (R and B stands for red and blue, respectively).

There are six ways to arrange the balls so that Takahashi will need exactly two moves: (B, B, R, B, R), (B, B, R, R, B), (R, B, B, R, B), (R, B, R, B, B), (B, R, B, B, R), and (B, R, R, B, B).

There is one way to arrange the balls so that Takahashi will need exactly three moves: (B, R, B, R, B).

Sample Input 2

2000 3

Sample Output 2

1998
3990006
327341989

Be sure to print the numbers of arrangements modulo 10^9+7.


const modP = 1000000007;
var Kcomb,NKcomb:array[0..2010]of int64;
    i,j,k,a,b,n,m,p:int64;

begin
  readln(n,k);
  NKcomb[0]:=1; NKcomb[n-k+1]:=1;
  Kcomb[0]:=1; Kcomb[k-1]:=1;
  for i:=1 to (k+1)div 2 do begin
    a:=k-i;
    Kcomb[i]:=(Kcomb[i-1] mod modP)*(a mod modP)div i;
    Kcomb[k-1-i]:=Kcomb[i];
  end;
  m:=n-k+1;
  for i:=1 to (m+1)div 2 do begin
    a:=m-i+1;
    NKcomb[i]:=(NKcomb[i-1] mod modP)*(a mod modP)div i;
    NKcomb[m-i]:=NKcomb[i];
  end;
  {
if k>n-k+1 then begin
for i:=1 to n-k+1 do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
  for i:=n-k+2 to k do writeln(0);
end
else
  for i:=1 to k do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
  }

  for i:=1 to k do begin
    if i<=n-k+1 then begin
      b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP) mod modP;
      writeln(b);
    end else writeln(0);
  end;

end.


const modP = 1000000007;
var Kcomb,NKcomb:array[0..2010]of int64;
    i,j,k,a,b,n,m,p:int64;

begin
  readln(n,k);
  NKcomb[0]:=1; NKcomb[n-k+1]:=1;
  Kcomb[0]:=1; Kcomb[k-1]:=1;
  for i:=1 to (k+1)div 2 do begin
    a:=k-i;
    Kcomb[i]:=(Kcomb[i-1] mod modP)*(a mod modP)div i;
    Kcomb[k-1-i]:=Kcomb[i];
  end;
  m:=n-k+1;
  for i:=1 to (m+1)div 2 do begin
    a:=m-i+1;
    NKcomb[i]:=(NKcomb[i-1] mod modP)*(a mod modP)div i;
    NKcomb[m-i]:=NKcomb[i];
  end;
  {
if k>n-k+1 then begin
  for i:=1 to n-k+1 do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
  for i:=n-k+2 to n do writeln(0);
end
else
  for i:=1 to k do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
  }
  for i:=1 to k do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
end.


const modP = 1000000007;
var Kcomb,NKcomb:array[0..2010]of int64;
    i,j,k,a,b,n,m,p:int64;

begin
  readln(n,k);
  NKcomb[0]:=1; NKcomb[n-k+1]:=1;
  Kcomb[0]:=1; Kcomb[k-1]:=1;
  for i:=1 to (k+1)div 2 do begin
    a:=k-i;
    Kcomb[i]:=(Kcomb[i-1] mod modP)*(a mod modP)div i;
    Kcomb[k-1-i]:=Kcomb[i];
  end;
  m:=n-k+1;
  for i:=1 to (m+1)div 2 do begin
    a:=m-i+1;
    NKcomb[i]:=(NKcomb[i-1] mod modP)*(a mod modP)div i;
    NKcomb[m-i]:=NKcomb[i];
  end;
if k>n-k+1 then begin
  for i:=1 to n-k+1 do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
  for i:=n-k+2 to n do writeln(0);
end
else
  for i:=1 to k do begin
    b:=(NKcomb[i] mod modP)*(Kcomb[i-1] mod modP);
    writeln(b);
  end;
end.


const md=1000000007;
var n,k,i,nk1,red,r,blue,b:int64;

function perm(p,l:int64):int64;
 var q,j:int64;
begin q :=1;
 for j:=p-l+1 to p do q:=q*j mod md;
 perm:=q;
end;
function fact(p:int64):int64;
begin fact:=perm(p,p) end;
begin
read(n,k);nk1:=n-k+1;
writeln(nk1);
for i := 2 to k do
 if i <= nk1 then
 begin
  blue:=(perm(k-1,k-i) div fact(i-1))mod md;
  red:= (perm(nk1,nk1-i) div fact(i))mod md;
  writeln(red*blue mod md)
 end
 else write(0);
end.


const base = trunc(1e9) + 7;
var f:array[0..2000] of int64;
    n,k,i:integer;
    re,x1,x2,a:int64;

procedure init;
var i:integer;
begin
	f[0]:=1;
	f[1]:=1;
	for i:=2 to 2000 do f[i]:= (f[i - 1] * i) mod base;
end;

function power(a,b:int64):int64;
var tmp:int64;
begin
	if b = 0 then exit(1);
	if b = 1 then exit(a);
	tmp:= power(a,b shr 1);
	if b mod 2 = 0 then power:= (tmp * tmp) mod base
	else power:= (((tmp * tmp) mod base) * a) mod base;
end;

function calc(n,k:integer):int64;
begin
	calc:= (f[n] * power(f[k] * f[n - k], base - 2) mod base) mod base;
end;

begin
	readln(n,k);
	init;
	for i:=1 to k do
	  begin
	    a:= n - k + i - 1;
	    x1:=calc(k - 1, i - 1);
	    x2:= calc(n - k + 1, i);
	    re:=(x1 * x2) mod base;
	    if a > 0 then writeln(re) else writeln(0);
	  end;
end.


Uses math;
var A,B,C,D,pCD,i,nCD,nC,nD:int64;
begin
 read(A,B,C,D);pCD:=1;
for i := 2 to max(C,D) do if (C mod i = 0) or (D mod i = 0) then pCD:=pCD*i;
 nCD:=(B-A) div pCD;
 nC:=(B-A) div C;
 nD:=(B-A) div D;
 write(B-A-nC-nD+nCD);
end.

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


program Megalomania;
const maxn=2*trunc(1e5);
var a,b:array[1..maxn] of longint;
    n:longint;

procedure rf;
var i:Longint;
begin
	readln(n);
	for i:=1 to n do readln(a[i],b[i]);
end;

procedure swap(var x,y:longint);
var tmp:longint;
begin
	tmp:=x;
    x:=y;
    y:=tmp;
end;

procedure sort(l,r:longint);
var i,j,x:Longint;
begin
	i:=l; j:=r;
	x:=b[(l+r) div 2];
	repeat
	  while b[i]<x do inc(i);
	  while b[j]>x do dec(j);
	  if i<=j then
	    begin
	      swap(b[i],b[j]);
	      swap(a[i],a[j]);
	      inc(i);
	      dec(j);
	    end;
	until i>j;
	if l<j then sort(l,j);
	if i<r then sort(i,r);
end;

procedure greedy;
var i,sum:longint;
    check:boolean;
begin
	sum:=0; check:=true;
	for i:=1 to n do
	  begin
        sum:=sum+a[i];
        if sum>b[i] then
          begin
            check:=false;
            break;
          end;
      end;
    if check then write('Yes') else write('No');
end;

begin
	rf;
	sort(1,n);
	greedy;
end.

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

1 \leq N \leq 2 \times 10^5

1 \leq A_i, B_i \leq 10^9 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


var
	N,K,i,u,v:Longint;
begin
	read(N,K);
	if (N-1)*(N-2)<K*2 then begin
		writeln(-1);
		exit;
	end;
	K:=(N-1)*(N-2)div 2-K;
	writeln(K+N-1);
	for i:=2 to N do begin
		writeln(1);
		writeln(i);
	end;
	u:=2;
	v:=3;
	for i:=1 to K do begin
		writeln(u);
		writeln(v);
		inc(v);
		if v>N then begin
			inc(u);
			v:=u+1;
		end;
	end;
end.

Score: 500 points

Problem Statement

Does there exist an undirected graph with N vertices satisfying the following conditions?

The graph is simple and connected.

The vertices are numbered 1, 2, ..., N.

Let M be the number of edges in the graph. The edges are numbered 1, 2, ..., M, the length of each edge is 1, and Edge i connects Vertex u_i and Vertex v_i.

There are exactly K pairs of vertices (i,\ j)\ (i < j) such that the shortest distance between them is 2.

If there exists such a graph, construct an example.

Constraints

All values in input are integers.

2 \leq N \leq 100

0 \leq K \leq \frac{N(N - 1)}{2}

Input

Input is given from Standard Input in the following format:

N K

Output

If there does not exist an undirected graph with N vertices satisfying the conditions, print -1.

If there exists such a graph, print an example in the following format (refer to Problem Statement for what the symbols stand for):

M
u_1 v_1
:
u_M v_M

If there exist multiple graphs satisfying the conditions, any of them will be accepted.

Sample Input 1

5 3

Sample Output 1

5
4 3
1 2
3 1
4 5
2 3

This graph has three pairs of vertices such that the shortest distance between them is 2: (1,\ 4), (2,\ 4), and (3,\ 5). Thus, the condition is satisfied.

Sample Input 2

5 8

Sample Output 2

-1

There is no graph satisfying the conditions.


var
	N,K,i,u,v:Longint;
begin
	read(N,K);
	if (N-1)*(N-2)<K*2 then begin
		writeln(-1);
		exit;
	end;
	K:=(N-1)*(N-2)div 2-K;
	writeln(K+N-1);
	for i:=2 to N do begin
		write('1 ');
		writeln(i);
	end;
	u:=2;
	v:=3;
	for i:=1 to K do begin
		write(u);
		write(' ');
		writeln(v);
		inc(v);
		if v>N then begin
			inc(u);
			v:=u+1;
		end;
	end;
end.


var n,k:integer;begin read(n,k);if(k=1)then writeln(0) else writeln(n-k);end.

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


Var a,b,c:Array[1 .. 100000] of Longint;
    n,i,rem1,rem2,min,max,rem,count:Longint;
Begin
    Readln(n); min:=10000; max:=-10000; count:=0;
    For i:=1 to n do begin
        Read(a[i]);
        If a[i]<min then begin
            min:=a[i]; rem1:=i;
        End
        Else if a[i]>max then begin
            max:=a[i]; rem2:=i;
        End;
    End;
    rem:=min;
    For i:=1 to n do
        If (i<>rem1) and (i<>rem2) then begin
            Inc(count);
            b[count]:=rem; c[count]:=a[i];
            rem:=rem-a[i];
        End;
    Writeln(max-rem);
    For i:=1 to count do writeln(b[count],' ',c[count]);
    Writeln(max,' ',rem);
End.

Score : 500 points

Problem Statement

There are N integers, A_1, A_2, ..., A_N, written on a blackboard.

We will repeat the following operation N-1 times so that we have only one integer on the blackboard.

Choose two integers x and y on the blackboard and erase these two integers. Then, write a new integer x-y.

Find the maximum possible value of the final integer on the blackboard and a sequence of operations that maximizes the final integer.

Constraints

2 \leq N \leq 10^5

-10^4 \leq A_i \leq 10^4

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible value M of the final integer on the blackboard, and a sequence of operations x_i, y_i that maximizes the final integer, in the format below.

Here x_i and y_i represent the integers x and y chosen in the i-th operation, respectively.

If there are multiple sequences of operations that maximize the final integer, any of them will be accepted.

M
x_1 y_1
:
x_{N-1} y_{N-1}

Sample Input 1

3
1 -1 2

Sample Output 1

4
-1 1
2 -2

If we choose x = -1 and y = 1 in the first operation, the set of integers written on the blackboard becomes (-2, 2).

Then, if we choose x = 2 and y = -2 in the second operation, the set of integers written on the blackboard becomes (4).

In this case, we have 4 as the final integer. We cannot end with a greater integer, so the answer is 4.

Sample Input 2

3
1 1 1

Sample Output 2

1
1 1
1 0


var
N,MM,i,j:Longint;
m:int64=1000000007;
a:Array[1..100000]of Longint;
dp:Array[0..100002]of int64;
begin
read(N,MM);
for i:=1 to MM do read(a[i]);
dp[0]:=1;
j:=1;
for i:=0 to N do begin
while(j<=MM)and(a[j]<i)do inc(j);
if(j<=MM)and(a[j]=i)then dp[i]:=0;
dp[i]:=dp[i]mod m;
inc(dp[i+1],dp[i]);
inc(dp[i+2],dp[i]);
end;
writeln(dp[N]);
end.

Score : 300 points

Problem Statement

There is a staircase with N steps. Takahashi is now standing at the foot of the stairs, that is, on the 0-th step.
He can climb up one or two steps at a time.

However, the treads of the a_1-th, a_2-th, a_3-th, \ldots, a_M-th steps are broken, so it is dangerous to set foot on those steps.

How many are there to climb up to the top step, that is, the N-th step, without setting foot on the broken steps?
Find the count modulo 1\ 000\ 000\ 007.

Constraints

1 \leq N \leq 10^5

0 \leq M \leq N-1

1 \leq a_1 < a_2 < ... < a_M \leq N-1

Input

Input is given from Standard Input in the following format:

N M
a_1
a_2
.
.
.
a_M

Output

Print the number of ways to climb up the stairs under the condition, modulo 1\ 000\ 000\ 007.

Sample Input 1

6 1
3

Sample Output 1

4

There are four ways to climb up the stairs, as follows:

0 \to 1 \to 2 \to 4 \to 5 \to 6

0 \to 1 \to 2 \to 4 \to 6

0 \to 2 \to 4 \to 5 \to 6

0 \to 2 \to 4 \to 6

Sample Input 2

10 2
4
5

Sample Output 2

0

There may be no way to climb up the stairs without setting foot on the broken steps.

Sample Input 3

100 5
1
23
45
67
89

Sample Output 3

608200469

Be sure to print the count modulo 1\ 000\ 000\ 007.


var a,b,N,M,i,n1:int64;
    G:array[0..100000] of integer;
const p=1000000007;
begin
 read(N,M);

  G[0]:=0;G[1]:=1;
 for i := 2 to N do
  G[i] :=( G[i-1] + G[i-2] ) mod p;

   n1:=1; read(a);
   n1:= n1*G[a] mod p;

  for i := 2 to M do
  begin
    b:=a;read(a);
    n1:= n1*G[a-b-1] mod p;
  end;

  n1:=n1*G[N-a] mod p;

write(n1)
end.


Uses math;
var n, m, i, a : int64;
    dp : array[1..100010] of int64;
    remainder : int64;
begin
  remainder := 1000000007;
  readln(n, m);

  for i := 1 to 100010 do dp[i] := 0;
  for i := 1 to m do begin
    read(a);
    dp[a] := -1;
  end;
  dp[0] := 1;

  for i := 0 to 100005 do begin
    if dp[i] = -1 then Continue;
    if dp[i+1] <> -1 then dp[i+1] := (dp[i+1] + dp[i]) mod remainder;
    if dp[i+2] <> -1 then dp[i+2] := (dp[i+2] + dp[i]) mod remainder;
  end;
  writeln(dp[n]);
  for i := 0 to n do writeln(dp[i]);
end.


Uses math;
var n, m, i, a : int64;
    dp : array[1..100010] of int64;
const remainder = 1000000007;
begin
  for i := 1 to 100010 do dp[i] := 0;
  dp[0] := 1;
  readln(n, m);
  for i := 1 to m do begin
    read(a);
    dp[a] := -1;
  end;

  for i := 0 to 100005 do begin
    if dp[i] = -1 then Continue;
    if dp[i+1] <> -1 then dp[i+1] := (dp[i+1] + dp[i]) mod remainder;
    if dp[i+2] <> -1 then dp[i+2] := (dp[i+2] + dp[i]) mod remainder;
  end;
  writeln(dp[n]);
end.


var s  :array[1..2010]of array[1..2010]of char;
    u,d,l,r,t  :array[1..2010]of array[1..2010]of integer;
    i,j,k,n,h,w,ans  :integer;

begin
  readln(h,w);
  for i:=1 to h do
    for j:=1 to w do begin
      read(s[i][j]);
      readln;
    end;
  {up}
  for j:=1 to w do begin
    if s[1][j]='.' then u[1][j]:=1 else u[1][j]:=0;
    for i:=2 to h do begin
      if s[i][j]='.' then u[i][j]:=u[i-1][j]+1 else u[i][j]:=0;
    end;
  end;
  {down}
  for j:=1 to w do begin
    if s[h][j]='.' then d[h][j]:=1 else d[h][j]:=0;
    for i:=h-1 downto 1 do begin
      if s[i][j]='.' then d[i][j]:=d[i+1][j]+1 else d[i][j]:=0;
    end;
  end;
  {left}
  for i:=1 to h do begin
    if s[i][1]='.' then l[i][1]:=1 else l[i][1]:=0;
    for j:=2 to w do begin
      if s[i][j]='.' then l[i][j]:=l[i][j+1]+1 else l[i][j+1]:=0;
    end;
  end;
  {right}
  for i:=1 to h do begin
    if s[i][w]='.' then l[i][w]:=1 else l[i][w]:=0;
    for j:=w-1 downto 1 do begin
      if s[i][j]='.' then l[i][j]:=l[i][j-1]+1 else l[i][j-1]:=0;
    end;
  end;
  ans:=0;
  for i:=1 to h do
    for j:=1 to w do begin
      if ans<(u[i][j]+d[i][j]+l[i][j]+r[i][j]-3)
        then ans:=u[i][j]+d[i][j]+l[i][j]+r[i][j]-3;
    end;
  writeln(ans)
end.

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


var l,r,u,d:array[1..2000] of array[1..2000] of integer;
    i,j,h,w,ans:integer;
    s:array[1..2000] of array[1..2000] of char;

begin
  ans:=0;
  readln(h,w);
  for i:=1 to h do begin
    for j:=1 to w do read(s[i][j]);
    readln
  end;
  for i:=1 to h do begin
    for j:=1 to w do begin
      if s[i][j] = '#' then begin
        u[i][j] := -1;
        l[i][j] := -1
      end else begin
        if i=1 then u[i][j] := 0 else u[i][j] := u[i-1][j] + 1;
        if j=1 then l[i][j] := 0 else l[i][j] := l[i][j-1] + 1
      end
    end
  end;
  for i:=h downto 1 do begin
    for j:=w downto 1 do begin
      if s[i][j] = '#' then begin
        d[i][j] := -1;
        r[i][j] := -1
      end else begin
        if i=h then d[i][j] := 0 else d[i][j] := d[i+1][j] + 1;
        if j=w then r[i][j] := 0 else r[i][j] := r[i][j+1] + 1
      end;
      if ans < l[i][j] + r[i][j] + u[i][j] + d[i][j] + 1 then ans := l[i][j] + r[i][j] + u[i][j] + d[i][j] + 1
    end
  end;
  writeln(ans)
end.


Uses math;
var p:char;
    A,B,C,D,N,i,x,y:integer;
    S:array[0..200010] of integer;
begin
read(N,A,B,C,D);

for i := 1 to N do
 begin
  read(p); if ord(p) < 40 then S[i]:=1 else S[i]:=0;
 end;

x:=1;
for i := 1 to N-1 do
 begin
  if (S[i]=1) and (S[i+1]=1) then x:=0;
 end;

if x=0 then write('No')
 else
  begin
   if C > D then
    begin
     x:=0;
     for i := B to N-2 do
      begin
      if (S[i]=0) and (S[i+1]=0) and (S[i+2]=0) then x:=1;
      end;
    end;
   if C < D then x:=1;

    if x=0 then write('No') else write('Yes');
  end;
end.

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

S is a string of length N consisting of . and #.

1 \leq A, B, C, D \leq N

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


Uses math;
var p:char;
    A,B,C,D,N,i,x,y:integer;
    S:array[0..200010] of integer;
begin
read(N,A,B,C,D);

for i := 1 to N do begin
 read(p);
 if ord(p) < 40 then S[i]:=1 else S[i]:=0;
end;

x:=1;
 for i := 1 to N do
  begin
   if (S[i]=1) and (S[i+1]=1) then x:=0;
  end;

if x=0 then write('No')
 else
  begin
if C > D then
 begin
  x:=0;
  for i := 1 to N do
   begin
    if (S[i]=0) and (S[i+1]=0) and (S[i+2]=0) then x:=1;
   end;
  if x=0 then write('No') else write('Yse');
 end;

end;
end.


var i,v,n,a,b,c,d,mv:longint;
    s:char;
    ans:boolean;

function max(a,b:longint):longint;
begin
  if a > b then max := a else max := b
end;

begin
  readln(n,a,b,c,d);
  v := 0;
  mv := 0;
  ans := true;
  for i:=1 to n do begin
    read(s);
    if (a<=i) and ((c>=i) or (d>=i)) then begin
      if b<c then begin
        if (v=0) and (s='#') then ans := false;
        if s='.' then v := v + 1 else v := 0;
        if (b<=i) and (d>=i) then mv := max(v,mv)
      end else
        if ((b>=i)or(c<=i)) and (v=0) and (s='#') then ans := false;
    end
  end;
  if (c>d) and (mv<3) then ans := false;
  if ans then writeln('Yes') else writeln('No')
end.


var i,v,n,a,b,c,d,mv:longint;
    s:char;
    ans:boolean;

function max(a,b:longint):longint;
begin
  if a > b then max := a else max := b
end;

begin
  read(n,a,b,c,d);
  v := 0;
  mv := 0;
  ans := true;
  for i:=1 to n do
    read(s);
    if (a<=i) and (c>=i) and (d>=i) then begin
      if (v=0) and (s='#') then ans := false;
      if s='.' then v := v + 1 else v := 0;
      if (b<=i) and (d>=i) then mv := max(v,mv)
    end;
  if (c>d) and (mv<3) then ans := false;
  if ans then writeln('Yes') else writeln('No')
end.


Score : 100 points

Problem Statement

Given an integer N not less than 3, find the sum of the interior angles of a regular polygon with N sides.

Print the answer in degrees, but do not print units.

Constraints

3 \leq N \leq 100

Input

Input is given from Standard Input in the following format:

N

Output

Print an integer representing the sum of the interior angles of a regular polygon with N sides.

Sample Input 1

3

Sample Output 1

180

The sum of the interior angles of a regular triangle is 180 degrees.

Sample Input 2

100

Sample Output 2

17640


var q,x,d,n,i,j,k,ans  :longint;

begin
  readln(q);
  for i:=1 to q do
  begin
    readln(x,d,n);
    ans:=x;
    for j:=1 to n-1 do ans:=(ans*(x+j*d))mod 1000003;
    writeln(ans)
  end;
end.

Score : 600 points

Problem Statement

Consider the following arithmetic progression with n terms:

x, x + d, x + 2d, \ldots, x + (n-1)d

What is the product of all terms in this sequence?
Compute the answer modulo 1\ 000\ 003.

You are given Q queries of this form.
In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i.

Constraints

1 \leq Q \leq 10^5

0 \leq x_i, d_i \leq 1\ 000\ 002

1 \leq n_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
x_1 d_1 n_1
:
x_Q d_Q n_Q

Output

Print Q lines.

In the i-th line, print the answer for the i-th query.

Sample Input 1

2
7 2 4
12345 67890 2019

Sample Output 1

9009
916936

For the first query, the answer is 7 \times 9 \times 11 \times 13 = 9009.
Don't forget to compute the answer modulo 1\ 000\ 003.


var q,x,d,n,i,j,k,ans  :longint;

begin
  readln(q);
  for i:=1 to q do
  begin
    readln(x,d,n);
    ans:=x;
    for j:=1 to n-1 do ans:=(ans*((ans+d)mod 1000003))mod 1000003;
    writeln(ans)
  end;
end.


var
	N,i,j,tmp:Longint;
	S:array[1..100]of String[10];
	P,id:array[1..100]of Longint;
	t:String;
begin
	readln(N);
	for i:=1 to N do begin
		id[i]:=i;
		readln(t);
		j:=1;
		while t[j]<>' ' do begin
			S[i]:=S[i]+t[j];
			inc(j);
		end;
		inc(j);
		for j:=j to length(t)do begin
			P[i]:=P[i]*10+ord(t[j])-48;
		end;
		for j:=i-1 downto 1 do begin
			if(S[id[j]]>S[id[j+1]])or((S[id[j]]=S[id[j+1]])and(P[id[j]]<P[id[j+1]]))then begin
				tmp:=id[j];
				id[j]:=id[j+1];
				id[j+1]:=tmp;
			end;
		end;
	end;
	for i:=1 to N do writeln(id[i]);
end.

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


var
	now,cnt,N,M,i,j,k,s:Longint;
	A:array[1..10,1..10]of Boolean;
	P:array[1..10]of Longint;
	flag:Boolean;
begin
	read(N,M);
	for i:=1 to M do begin
		read(k);
		for j:=1 to k do begin
			read(s);
			A[i,s]:=True;
		end;
	end;
	for i:=1 to M do begin
		read(P[i]);
	end;
	for i:=0 to (1<<N)-1 do begin
		flag:=True;
		for j:=1 to M do begin
			now:=0;
			for k:=1 to N do begin
				if((i>>k)mod 2=1)and A[j,k]then inc(now);
			end;
			if now mod 2<>P[j]mod 2 then begin
				flag:=False;
				break;
			end;
		end;
		if flag then inc(cnt);
	end;
	writeln(cnt);
end.

Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


var t:array[0..100000]of longint;
    i,n,m,a,l,r:longint;
procedure add(x,y:longint);inline;
begin
  while x<=n do
  begin
    inc(t[x],y);
    inc(x,x and -x);
  end;
end;
function ask(x:longint):longint;inline;
var a:longint=0;
begin
  while x<>0 do
  begin
    inc(a,t[x]);
    dec(x,x and -x);
  end;
  exit(a);
end;
begin
  readln(n,m);
  for i:=1 to m do
  begin
    read(l,r);
    add(l,1);
    add(r+1,-1);
  end;
  for i:=1 to n do
    if ask(i)=m then
      inc(a);
  writeln(a);
end.

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


var N,M,x,y,z,w,i:integer;
begin
 read(N,M);
 x:=0;y:=100000;
for i := 1 to M do
begin
 read(z,w);
 if x <= z then x:= z;
 if w <= y then y:= w;
 end;
 if y >= x then write(y-x+1) else write(0);
end.


var N,M,x,y,z,w,i:integer;
begin
 read(N,M);
 x:=0;y:=100000;
for i := 1 to M do
begin
 read(z,w);
 if x <= z then x:= z;
 if w <= y then y:= w;
 end;
 if y >= x then write(y-x+1);
end.


var n,m,i   :integer;
    l,r,lm,rm :integer;
begin
  readln(n,m);
  lm:=0; rm:=100002;
  for i:=1 to m do
  begin
    readln(l,r);
    if lm<l then lm:=l;
    if rm>r then rm:=r
  end;
  if lm<=rm then writeln(rm-lm+1)
  else writeln(0)
end.


var i,n,m,l,r,min,max:longint;

begin
  readln(n,m);
  min:=1;
  max:=n;
  for i:=1 to m do begin
    readln(l,r);
    if l>min then min:=l;
    if r<max then max:=r
  end;
  if max-min<0 then writeln(0) else writeln(max-min+1);
end.


type
	intArray = array[1..100000] of int64;
  pair = record
    b : int64;
    c : integer;
  end;
	pairArray = array[1..100000] of pair;

var cur,pos,i,n,m:longint;
    sum:int64;
    a:intArray;
    bc : pairArray;

procedure	swap(var a,b:int64);
var tmp:int64;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure	swapP(var a,b:pair);
var tmp:pair;
begin
	tmp:=a;
	a:=b;
	b:=tmp
end;

procedure QuickSort(var a:intArray;start_index,end_index:integer);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2];
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i]<x)do i:=i+1;
		while(x<a[j])do j:=j-1;
		if(i<j)then
		begin
			swap(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSort(a,start_index,i-1);
			if(j+1<end_index)then QuickSort(a,j+1,end_index);
			exit;
		end;
	end;
end;

procedure QuickSortP(var a:pairArray;start_index,end_index:integer);
var i,j,x:int64;
begin
	x:=a[(start_index+end_index)div 2].c;
	i:=start_index;
	j:=end_index;
	while(true)do
	begin
		while(a[i].c<x)do i:=i+1;
		while(x<a[j].c)do j:=j-1;
		if(i<j)then
		begin
			swapP(a[i],a[j]);
			i:=i+1;
			j:=j-1;
		end else
		begin
			if(start_index<i-1)then QuickSortP(a,start_index,i-1);
			if(j+1<end_index)then QuickSortP(a,j+1,end_index);
			exit;
		end;
	end;
end;

begin
  readln(n,m);
  for i:=1 to n do read(a[i]);
  QuickSort(a,1,n);
  for i:=1 to m do read(bc[i].b,bc[i].c);
  QuickSortP(bc,1,m);
  cur := m;
  pos := n;
  sum := 0;
  for i:=1 to n do
  if (not cur=0) and (bc[cur].c>a[pos]) then begin
      sum := sum + bc[cur].c;
      bc[cur].b := bc[cur].b - 1;
      if bc[cur].b=0 then cur := cur - 1
    end else begin
      sum := sum + a[pos];
      pos := pos - 1;
    end;
  writeln(sum)
end.

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

The output may not fit into a 32-bit integer type.


uses crt, math;
var
        i,n,k,tmp,tmp2:longint;
        kq:real;
begin
        clrscr;
        readln(n, k);
        kq:=0;
        for i:=1 to n do begin
                tmp:=i;
                tmp2:=0;
                while tmp<k do begin
                        tmp:=tmp*2;
                        inc(tmp2);
                end;
                kq:=kq+(1/n)*power(0.5, tmp2);
        end;
        writeln(kq:0:20);
        readln;
end.

Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


var N,K,i,l:int64;
    pi,p:real;
begin
read(N,K);p:=0;
for i := 1 to N do
 begin
  l:=i;pi:=1;
  while l < K do begin l:=l*2;pi:=pi/2 end;
  p:=p+pi;
 end;
write(p/N)
end.


var
	N,K,i,p,q,x,tmp,get:Longint;
	ans,now,make:double;
begin
	read(N,K);
	if N>=K then begin
		ans:=(N-K+1)/N;
		q:=K-1;
	end
	else q:=N;
	make:=1;
	get:=1;
	for i:=1 to 4 do begin
		if q=0 then break;
		get:=get*2;
		make:=make*0.5;
		p:=K div get;
		if p<q then begin;
			ans:=ans+(q-p)/N*make;
			q:=p;
		end;
	end;
	for i:=q downto 1 do begin
		tmp:=(K+i-1)div i;
		x:=1;
		now:=1/N;
		while tmp>x do begin
			now:=now*0.5;
			x:=x*2;
		end;
		ans:=ans+now;
	end;
	writeln(ans:0:9);
end.


var
	N,K,i,p,q,x,tmp,get:Longint;
	ans,now,make:double;
begin
	read(N,K);
	if N>=K then begin
		ans:=(N-K+1)/N;
		q:=K-1;
	end
	else q:=N;
	make:=1;
	get:=1;
	for i:=1 to 3 do begin
		if q=0 then break;
		get:=get*2;
		make:=make*0.5;
		p:=K div get;
		if p<q then begin;
			ans:=ans+(q-p)/N*make;
			q:=p;
		end;
	end;
	for i:=q downto 1 do begin
		tmp:=(K+i-1)div i;
		x:=1;
		now:=1/N;
		while tmp>x do begin
			now:=now*0.5;
			x:=x*2;
		end;
		ans:=ans+now;
	end;
	writeln(ans:0:9);
end.


var
	N,K,i,p,q,x,tmp,get:Longint;
	ans,now,make:double;
begin
	read(N,K);
	if N>=K then begin
		ans:=(N-K+1)/N;
		q:=K-1;
	end
	else q:=N;
	make:=1;
	get:=1;
	for i:=1 to 0 do begin
		if q=0 then break;
		get:=get*2;
		make:=make*0.5;
		p:=K div get;
		if p<q then begin;
			ans:=ans+(q-p)/N*make;
			q:=p;
		end;
	end;
	for i:=q downto 1 do begin
		tmp:=(K+i-1)div i;
		x:=1;
		now:=1/N;
		while tmp>x do begin
			now:=now*0.5;
			x:=x*2;
		end;
		ans:=ans+now;
	end;
	writeln(ans:0:9);
end.


Var rem,count:Extended;
    n,k,srem,i,sum:Longint;
    a:Array[1 .. 17] of Extended;
Begin
    Readln(n,k); rem:=1/n; a[1]:=0.5; count:=0;
    For i:=2 to 17 do a[i]:=a[i-1]*0.5;
    For i:=1 to n do begin
        srem:=i; sum:=0;
        While srem<k do begin
            srem:=srem*2;
            Inc(sum);
        End;
        count:=count+rem*a[sum];
        //Writeln(rem*a[sum]);
    End;
    Writeln(count);
End.


Var rem,count:Double;
    n,k,srem,i,sum:Longint;
    a:Array[1 .. 17] of Double;
Begin
    Readln(n,k); rem:=1/n; a[1]:=0.5; count:=0;
    For i:=2 to 17 do a[i]:=a[i-1]*0.5;
    For i:=1 to n do begin
        srem:=i; sum:=0;
        While srem<k do begin
            srem:=srem*2;
            Inc(sum);
        End;
        count:=count+rem*a[sum];
        //Writeln(rem*a[sum]);
    End;
    Writeln(count);
End.