var
	N,Q,ccc,i,k:Longint;
	D,DM,M,ans:int64;
	S:String;
begin
	readln(N);
	readln(S);
	readln(Q);
	for ccc:=1 to Q do begin
		read(k);
		D:=0;DM:=0;M:=0;ans:=0;
		for i:=1 to N do begin
			if i>k then begin
				if S[i-k]='D' then begin
					dec(DM,M);
					dec(D);
				end else if S[i-k]='M' then begin
					dec(M);
				end;
			end;
			if S[i]='D' then begin
				inc(D);
			end else if S[i]='M' then begin
				inc(DM,D);
				inc(M);
			end else if S[i]='C' then begin
				inc(ans,DM);
			end;
		end;
		writeln(ans);
	end;
end.

Score : 600 points

Problem Statement

In Dwango Co., Ltd., there is a content distribution system named 'Dwango Media Cluster', and it is called 'DMC' for short.

The name 'DMC' sounds cool for Niwango-kun, so he starts to define DMC-ness of a string.

Given a string S of length N and an integer k (k \geq 3),
he defines the k-DMC number of S as the number of triples (a, b, c) of integers that satisfy the following conditions:

0 \leq a < b < c \leq N - 1

S[a] = D

S[b] = M

S[c] = C

c-a < k

Here S[a] is the a-th character of the string S. Indexing is zero-based, that is, 0 \leq a \leq N - 1 holds.

For a string S and Q integers k_0, k_1, ..., k_{Q-1}, calculate the k_i-DMC number of S for each i (0 \leq i \leq Q-1).

Constraints

3 \leq N \leq 10^6

S consists of uppercase English letters

1 \leq Q \leq 75

3 \leq k_i \leq N

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N
S
Q
k_{0} k_{1} ... k_{Q-1}

Output

Print Q lines.
The i-th line should contain the k_i-DMC number of the string S.

Sample Input 1

18
DWANGOMEDIACLUSTER
1
18

Sample Output 1

1

(a,b,c) = (0, 6, 11) satisfies the conditions.

Strangely, Dwango Media Cluster does not have so much DMC-ness by his definition.

Sample Input 2

18
DDDDDDMMMMMCCCCCCC
1
18

Sample Output 2

210

The number of triples can be calculated as 6\times 5\times 7.

Sample Input 3

54
DIALUPWIDEAREANETWORKGAMINGOPERATIONCORPORATIONLIMITED
3
20 30 40

Sample Output 3

0
1
2

(a, b, c) = (0, 23, 36), (8, 23, 36) satisfy the conditions except the last one, namely, c-a < k_i.

By the way, DWANGO is an acronym for "Dial-up Wide Area Network Gaming Operation".

Sample Output 4

30
DMCDMCDMCDMCDMCDMCDMCDMCDMCDMC
4
5 10 15 20

Sample Output 4

10
52
110
140


program diskon;
var
x,y : integer;
begin
readln(x);
readln(y);
x := x + (y div 2);
writeln(x);
end.

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

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31

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, t, a, h, i, ans:longint;
	mi:extended;
begin
	read(n, t, a);
	mi:=233333333.0;
	for i:=1 to n do
	begin
		read(h);
		if abs(t - h * 0.006 - a) < mi then
		begin
			mi:=abs(t - h * 0.006 - a);
			ans:=i;
		end;
	end;
	writeln(ans);
end.

Score: 200 points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius.

There are N places proposed for the place. The elevation of Place i is H_i meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius.

The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius.

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


var
	a,b,x,y,num,pro:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,k,l1,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			t:=num[pro[i]]; num[pro[i]]:=num[pro[j]]; num[pro[j]]:=t;
			pro[num[i]]:=j; pro[num[j]]:=i;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
		pro[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
	readln; readln;
end.

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


var
  n,m,i,j,zrt:longint;
  p,y,a,b,c,o:array[0..1000000]of longint;
Procedure ch(var x,y:longint);
var
  t:longint;
begin
  t:=x;x:=y;y:=t;
end;
Procedure qs(l,r:longint);
var
  i,j,mid1,mid2:longint;
begin
  i:=l;j:=r;mid1:=p[(l+r)div 2];mid2:=y[(l+r)div 2];
  repeat
    while (p[i]<mid1)or((p[i]=mid1)and(y[i]<mid2)) do inc(i);
    while (p[j]>mid1)or((p[j]=mid1)and(y[j]>mid2)) do dec(j);
    if i<=j then
      begin
        ch(p[i],p[j]);
        ch(y[i],y[j]);
        ch(c[i],c[j]);
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs(l,j);
  if i<r then qs(i,r);
end;

begin
  readln(n,m);
  for i:=1 to m do
    begin
      readln(p[i],y[i]);
      a[i]:=p[i];c[i]:=i;
    end;
  qs(1,m);
  for i:=1 to m do
    begin
      if (i=1)or(p[i]<>p[i-1]) then j:=1 else inc(j);
      o[c[i]]:=j;
    end;
  for i:=1 to m do
    begin
      zrt:=100000;
      for j:=1 to 6 do
        begin
          if a[i]<zrt then
            begin
              zrt:=zrt div 10;
              write(0);
            end else
          break;
       end;
      write(a[i]);
      zrt:=100000;
      for j:=1 to 6 do
        begin
          if o[i]<zrt then
            begin
              zrt:=zrt div 10;
              write(0);
            end else
          break;
        end;
      writeln(o[i]);
  end;
      end.


var
	a,b,x,y,num:array[0..100010] of longint;
	n,m,i,t:longint;
	ch:string[20];
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r;
	mid:=(l+r) div 2;
	repeat
		while (x[i]<x[mid]) or (x[i]=x[mid]) and (y[i]<y[mid]) do inc(i);
		while (x[j]>x[mid]) or (x[j]=x[mid]) and (y[j]>y[mid]) do dec(j);
		if i<=j then
		begin
			t:=x[i]; x[i]:=x[j]; x[j]:=t;
			t:=y[i]; y[i]:=y[j]; y[j]:=t;
			t:=num[num[i]]; num[num[i]]:=num[num[j]]; num[num[j]]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure card(x,y:longint);
var
	i,t:longint;
begin
	str(x,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	write(x);
	str(y,ch);
	t:=6-length(ch);
	for i:=1 to t do write(0);
	writeln(y);
end;
begin
	read(n,m);
	for i:=1 to m do
	begin
		read(a[i],b[i]);
		x[i]:=a[i];
		y[i]:=b[i];
		num[i]:=i;
	end;
	qsort(1,m);
	t:=1;
	y[1]:=t;
	for i:=2 to m do
	begin
		if x[i]<>x[i-1] then t:=1 else inc(t);
		y[i]:=t;
	end;
	for i:=1 to m do card(x[num[i]],y[num[i]]);
end.


var
	a:array[-10..200010] of int64;
	n,i,l,r,flag:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	if n mod 2=0 then
	begin
		t1:=a[(1+n) div 2+1];
		l:=1; r:=n;
		for i:=1 to n-1 do
		begin
			x:=abs(a[r]-t1);
			y:=abs(t1-a[l]);
			flag:=0;
			while x=y do
			begin
				inc(flag);
				x:=abs(a[r-flag]-a[l]);
				y:=abs(a[r]-a[l+flag]);
			end;
			if x>y then right
			else if x<y then left;
			t:=t+abs(t1-t2);
			t1:=t2;
		end;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.

Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r,x,y,ttt:longint;
	s:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0; ttt:=(1+n) div 2;
	t1:=a[ttt];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then
		begin
			t2:=a[r];
			dec(r);
			if r=ttt then dec(r);
		end;
		if x<y then
		begin
			t2:=a[l];
			inc(l);
			if l=ttt then inc(l);
		end;
		if x=y then
		begin
			x:=abs(a[r-1]-a[l]);
			y:=abs(a[r]-a[l+1]);
			if x>y then
			begin
				t2:=a[r];
				dec(r);
				if r=ttt then dec(r);
			end;
			if x<=y then
			begin
				t2:=a[l];
				inc(l);
				if l=ttt then inc(l);
			end;
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	writeln(s);
end.


var
	a:array[0..100010] of longint;
	n,i,t1,t2,l,r,x,y:longint;
	s:int64;
procedure qsort(l,r:longint);
var
	i,j,t,mid:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then
		begin
			t2:=a[r];
			dec(r);
		end;
		if x<y then
		begin
			t2:=a[l];
			inc(l);
		end;
		if x=y then
		begin
			x:=abs(a[r-1]-a[l]);
			y:=abs(a[r]-a[l+1]);
			if x>y then
			begin
				t2:=a[r];
				dec(r);
			end;
			if x<=y then
			begin
				t2:=a[l];
				inc(l);
			end;
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	writeln(s);
end.


var
	a:array[-10..200010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right
		else if x<y then left
		else if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
	readln; readln;
end.


var
	a:array[-10..200010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2,x,y:int64;
procedure qsort(l,r:longint);
var
	t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
procedure left;
begin
	t2:=a[l];
	inc(l);
end;
procedure right;
begin
	t2:=a[r];
	dec(r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right;
		if x<y then left;
		if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		x:=abs(a[r]-t1);
		y:=abs(t1-a[l]);
		if x>y then right;
		if x<y then left;
		if x=y then
			if abs(a[r-1]-a[l])>abs(a[r]-a[l+1]) then right
			else left;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
	a:array[-10..200010] of int64;
	n,i,l,r:longint;
	s,t,t1,t2:int64;
procedure qsort(l,r:longint);
var
        t,mid:int64;
	i,j:longint;
begin
	i:=l; j:=r; mid:=a[(l+r) div 2];
	repeat
		while a[i]<mid do inc(i);
		while a[j]>mid do dec(j);
		if i<=j then
		begin
			t:=a[i]; a[i]:=a[j]; a[j]:=t;
			inc(i); dec(j);
		end;
	until i>j;
	if l<j then qsort(l,j);
	if i<r then qsort(i,r);
end;
begin
	read(n);
	for i:=1 to n do read(a[i]);
	qsort(1,n);
	s:=0;
	t1:=a[(1+n) div 2];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if r=(1+n) div 2 then dec(r);
		if l=(1+n) div 2 then inc(l);
                if l>r then break;
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2 then inc(l);
		end;
		s:=s+abs(t1-t2);
		t1:=t2;
	end;
	t:=0;
	t1:=a[(1+n) div 2+1];
	l:=1; r:=n;
	for i:=1 to n-1 do
	begin
		if r=(1+n) div 2+1 then dec(r);
		if l=(1+n) div 2+1 then inc(l);
                if l>r then break;
		if abs(a[r]-t1)>abs(t1-a[l]) then
		begin
			t2:=a[r];
			dec(r);
			if r=(1+n) div 2+1 then dec(r);
		end
		else
		begin
			t2:=a[l];
			inc(l);
			if l=(1+n) div 2+1 then inc(l);
		end;
		t:=t+abs(t1-t2);
		t1:=t2;
	end;
	if s>t then writeln(s)
	else writeln(t);
end.


var
  a,c:array[1..100000]of longint;
  b:array[1..100000]of boolean;
  i,n,s,j,x,y,sum1,sum2:longint;
procedure qsort(l,r:longint);
var
  i,j,mid,p:longint;
begin
  i:=l;j:=r;
  mid:=a[(l+r) div 2];
  repeat
    while a[i]<mid do inc(i);
    while a[j]>mid do dec(j);
    if i<=j then
      begin
        p:=a[i];a[i]:=a[j];a[j]:=p;
        inc(i);
        dec(j);
      end;
  until i>j;
  if l<j then qsort(l,j);
  if i<r then qsort(i,r);
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  if n=2 then begin write(abs(a[1]-a[2]));halt;end;
  qsort(1,n);
  fillchar(b,sizeof(b),true);
  i:=2;j:=n-2;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    c[x]:=a[i];inc(i);inc(y);
	c[y]:=a[i];inc(i);dec(x);
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    c[x]:=a[j];dec(j);dec(x);
	c[y]:=a[j];dec(j);inc(y);
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum1:=sum1+abs(c[i]-c[i-1]);
  i:=2;j:=n-2;
  if odd(n) then
              begin
			    c[n div 2+1]:=a[1];
				c[n div 2]:=a[n];
				c[n div 2+2]:=a[n-1];
				s:=3;
				x:=n div 2-1;
				y:=n div 2+3;
			  end
			else
			  begin
			    c[n div 2]:=a[1];
				c[n div 2-1]:=a[n];
				c[n div 2+1]:=a[n-1];
				s:=3;
				x:=n div 2-2;
				y:=n div 2+2;
			  end;
  while (x>=1) and (y<=n) do
  begin
  if (s mod 4=3)and(i<=n) then
  begin
    c[y]:=a[i];inc(i);dec(y);
	c[x]:=a[i];inc(i);inc(x);
	s:=s+2;
  end;
  if (s mod 4=1)and(j>=1) then
  begin
    c[y]:=a[j];dec(j);dec(y);
	c[x]:=a[j];dec(j);inc(x);
	s:=s+2;
  end;
  end;
  if odd(x) then c[n]:=a[n div 2+1] else c[x]:=a[n div 2];
  for i:=2 to n do sum2:=sum2+abs(c[i]-c[i-1]);
  if sum1>sum2 then write(sum1) else write(sum2);
  readln(i);
end.


var n,i:integer;
a:array[0..100000]of longint;
f:array[1..100000]of 0..1;
tot:int64;
procedure s(x:integer;la:longint;ss:int64);
var j:integer;
begin
	if x>n then begin if ss>tot then tot:=ss;exit;end;
	for j:=1 to n do
	begin
		if f[j]=0 then
		begin
			f[j]:=1;
			if x=1 then s(x+1,j,ss)
			else
			s(x+1,j,ss+abs(a[j]-a[la]));
			f[j]:=0;
		end;
	end;
end;
begin
	read(n);
	tot:=0;
	fillchar(f,sizeof(f),0);
	for i:=1 to n do read(a[i]);
	s(1,0,0);
	writeln(tot);
end.


var
  s,t,x:array[0..10000000]of char;
  i,j,n,m:longint;
  l,ans:int64;
Function min(x,y:longint):longint;
begin
  if x<y then exit(x) else exit(y);
end;
Function gcd(x,y:longint):int64;
begin
  if y=0 then exit(x) else exit(gcd(y,x mod y));
end;
begin
  readln(n,m);
  for i:=1 to n do read(s[i]);readln;
  for i:=1 to m do read(t[i]);
  l:=n*m div gcd(n,m);
  for i:=0 to l do x[i]:=' ';

  for i:=0 to n-1 do
    x[(l div n)*i+1]:=s[i+1];

  for i:=0 to m-1 do
    if x[(l div m)*i+1]<>' ' then
      if x[(l div m)*i+1]<>t[i+1] then
        begin
         write('-1');
         exit;
        end;
  write(l);
end.

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45


var
  s,t,x:array[0..10000]of char;
  i,j,n,m:longint;
  l,ans:int64;
Function min(x,y:longint):longint;
begin
  if x<y then exit(x) else exit(y);
end;
Function gcd(x,y:longint):int64;
begin
  if y=0 then exit(x) else exit(gcd(y,x mod y));
end;
begin
  readln(n,m);
  for i:=1 to n do read(s[i]);readln;
  for i:=1 to m do read(t[i]);
  l:=n*m div gcd(n,m);
  for i:=0 to l do x[i]:=' ';

  for i:=0 to n-1 do
    x[(l div n)*i+1]:=s[i+1];

  for i:=0 to m-1 do
    if x[(l div m)*i+1]<>' ' then
      if x[(l div m)*i+1]<>t[i+1] then
        begin
         write('-1');
         exit;
        end;
  write(l);
end.


var
	n,m,t1,i,j:int64;
	s,t:ansistring;
function gcd(x,y:longint):longint;
var
	r,t:longint;
begin
	if x<y then
	begin
		t:=x; x:=y; y:=t;
	end;
        if y=0 then exit(x);
	r:=x mod y;
	while r<>0 do
	begin
		x:=y; y:=r;
		r:=x mod y;
	end;
	exit(y);
end;
begin
	readln(n,m);
	readln(s); readln(t);
	t1:=n*m div gcd(n,m);
	i:=1; j:=1;
	while (i<=n) and (j<=m) do
	begin
		if s[i]<>t[j] then
		begin
			writeln(-1);
			halt;
		end;
		i:=i+t1 div m;
		j:=j+t1 div n;
	end;
	writeln(t1);
end.


var
	n,m,t1,i,j:longint;
	s,t:ansistring;
function gcd(x,y:longint):longint;
var
	r,t:longint;
begin
	if x<y then
	begin
		t:=x; x:=y; y:=t;
	end;
        if y=0 then exit(x);
	r:=x mod y;
	while r<>0 do
	begin
		x:=y; y:=r;
		r:=x mod y;
	end;
	exit(y);
end;
begin
	readln(n,m);
	readln(s); readln(t);
	t1:=n*m div gcd(n,m);
	i:=1; j:=1;
	while (i<=n) and (j<=m) do
	begin
		if s[i]<>t[j] then
		begin
			writeln(-1);
			halt;
		end;
		i:=i+t1 div m;
		j:=j+t1 div n;
	end;
	writeln(t1);
end.


var
	n,m,t1,i,j:longint;
	s,t:ansistring;
function gcd(x,y:longint):longint;
var
	r,t:longint;
begin
	if x<y then
	begin
		t:=x; x:=y; y:=t;
	end;
	r:=x mod y;
	while r<>0 do
	begin
		x:=y; y:=r;
		r:=x mod y;
	end;
	exit(y);
end;
begin
	readln(n,m);
	readln(s); readln(t);
	t1:=n*m div gcd(n,m);
	i:=1; j:=1;
	while (i<=n) and (j<=m) do
	begin
		if s[i]<>t[j] then
		begin
			writeln(-1);
			halt;
		end;
		i:=i+t1 div m;
		j:=j+t1 div n;
	end;
	writeln(t1);
end.


var
  a,b,c:string;
  t:char;
  n,m,i,j,p,q,k:longint;
begin
  readln(n,m);
  readln(a);
  readln(b);
  p:=n;
  q:=m;
  if p<q then begin i:=p; p:=q; q:=i; end;
  while (p mod q)<>0 do if p>q then begin p:=p mod q; i:=p; p:=q; q:=i; end;
  k:=n*m div q;
  p:=k mod n;
  q:=k mod m;
  i:=1;
  j:=1;
  while (j<=m)and(i<=n) do
  begin
	 if b[j]<>a[i] then begin writeln('-1'); halt; end;
     i:=i+q;
	 j:=j+p;
  end;
  writeln(k);
end.


var
  a,b,c:string;
  t:char;
  n,m,i,j,p,q,k:longint;
begin
  readln(n,m);
  readln(a);
  readln(b);
  p:=n;
  q:=m;
  if p<q then begin i:=p; p:=q; q:=i; end;
  while (p mod q)<>0 do begin p:=p mod q; i:=p; p:=q; q:=i; end;
  k:=n*m div q;
  p:=k div n;
  q:=k div m;
  i:=1;
  j:=1;
  while (j<=m)and(i<=n) do
  begin
	 if b[j]<>a[i] then begin writeln('-1'); halt; end;
     i:=i+q;
	 j:=j+p;
  end;
  writeln(k);
end.


var
  a,b,c:string;
  t:char;
  n,m,i,j,p,q,k:longint;
begin
  readln(n,m);
  readln(a);
  readln(b);
  p:=n;
  q:=m;
  if p<q then begin i:=p; p:=q; q:=i; end;
  while (p mod q)<>0 do if p>q then begin p:=p mod q; i:=p; p:=q; q:=i; end;
  k:=n*m div q;
  p:=k div n;
  q:=k div m;
  i:=1;
  j:=1;
  while (j<=m)and(i<=n) do
  begin
	 if b[j]<>a[i] then begin writeln('-1'); halt; end;
     i:=i+q;
	 j:=j+p;
  end;
  writeln(k);
end.


var
	n,m,t1:longint;
	s,t,t2:ansistring;
function gcd(x,y:longint):longint;
var
	t,r:longint;
begin
	if x<y then
	begin
		t:=x; x:=y; y:=t;
	end;
	r:=x mod y;
	while r<>0 do
	begin
		x:=y; y:=r;
		r:=x mod y;
	end;
	exit(y);
end;
begin
	readln(n,m);
	readln(s); readln(t);
	if n<m then
	begin
		t1:=n; n:=m; m:=t1;
		t2:=s; s:=t; t:=t2;
	end;
	if n mod m=0 then writeln(-1)
	else writeln(n*m div gcd(n,m));
end.


var
	n,m,t1:longint;
	s,t,t2:ansistring;
function gcd(x,y:longint):longint;
var
	t,r:longint;
begin
	if x<y then
	begin
		t:=x; x:=y; y:=t;
	end;
	r:=x mod y;
	while r<>0 do
	begin
		x:=y; y:=r;
		r:=x mod y;
	end;
	exit(y);
end;
begin
	readln(n,m);
	readln(s); readln(t);
	if n<m then
	begin
		t1:=n; n:=m; m:=t1;
		t2:=s; s:=t; t:=t2;
	end;
	if n mod m=0 then writeln(-1)
	else writeln(gcd(n,m));
end.


var
	n,i:Longint;
	ans,a,m:int64;
	sum:array[1..100000]of int64;
function power(a,b:int64):int64;
var s:int64;
begin
	if b=0 then power:=1
	else begin
		s:=power(a*a mod m,b div 2);
		if b mod 2=1 then s:=s*a mod m;
		power:=s;
	end;
end;
begin
	m:=1000000007;
	read(n);
	sum[1]:=1;
	for i:=2 to n do sum[i]:=(sum[i-1]+power(i,m-2))mod m;
	for i:=1 to n do begin
		read(a);
		ans:=(ans+(sum[i]+sum[n-i+1]-1)mod m*a mod m)mod m;
	end;
	for i:=1 to n do ans:=ans*i mod m;
	writeln(ans);
end.

Score : 600 points

Problem Statement

There are N blocks arranged in a row, numbered 1 to N from left to right.
Each block has a weight, and the weight of Block i is A_i.
Snuke will perform the following operation on these blocks N times:

Choose one block that is still not removed, and remove it.
The cost of this operation is the sum of the weights of the blocks that are connected to the block being removed (including itself).
Here, two blocks x and y ( x \leq y ) are connected when, for all z ( x \leq z \leq y ), Block z is still not removed.

There are N! possible orders in which Snuke removes the blocks.
For all of those N! orders, find the total cost of the N operations, and calculate the sum of those N! total costs.
As the answer can be extremely large, compute the sum modulo 10^9+7.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

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

For all of the N! orders, find the total cost of the N operations, and print the sum of those N! total costs, modulo 10^9+7.

Sample Input 1

2
1 2

Sample Output 1

9

First, we will consider the order "Block 1 -> Block 2".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 2, as only Block 2 remains.
Thus, the total cost of the two operations for this order is 3+2=5.

Then, we will consider the order "Block 2 -> Block 1".
In the first operation, the cost of the operation is 1+2=3, as Block 1 and 2 are connected.
In the second operation, the cost of the operation is 1, as only Block 1 remains.
Thus, the total cost of the two operations for this order is 3+1=4.

Therefore, the answer is 5+4=9.

Sample Input 2

4
1 1 1 1

Sample Output 2

212

Sample Input 3

10
1 2 4 8 16 32 64 128 256 512

Sample Output 3

880971923


var
	n,i:Longint;
	ans,a,m:int64;
	sum:array[1..100000]of int64;
function power(a,b:int64):int64;
var s:int64;
begin
	if b=0 then power:=1
	else begin
		s:=power(a*a mod m,b div 2);
		if b mod 2=1 then s:=s*a mod m;
		power:=s;
	end;
end;
begin
	m:=1000000007;
	read(n);
	sum[1]:=1;
	for i:=2 to n do sum[i]:=(sum[i-1]+power(i,m-2))mod m;
	for i:=1 to n do begin
		read(a);
		ans:=(ans+(sum[i]+sum[n-i+1]+m-1)mod m*a mod m)mod m;
	end;
	for i:=1 to n do ans:=ans*i mod m;
	writeln(ans);
end.


var
	n,k,i,c,t,min:longint;
begin
	read(n,k);
	min:=100000;
	for i:=1 to n do
	begin
		read(c,t);
		if t<=k then
			if c<min then min:=c;
	end;
	if min=100000 then writeln('TLE') else writeln(min);
end.

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


var
	n,m,ans,i,t:Longint;
begin
	read(n,m);
	i:=1;
	while i*i<=m do begin
		if m mod i=0 then begin
			t:=m div i;
			if(t>=n)and(ans<i)then ans:=i;
			if(i>=n)and(ans<t)then ans:=t;
		end;
		inc(i);
	end;
	writeln(ans);
end.

Score : 400 points

Problem Statement

You are given integers N and M.

Consider a sequence a of length N consisting of positive integers such that a_1 + a_2 + ... + a_N = M. Find the maximum possible value of the greatest common divisor of a_1, a_2, ..., a_N.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

N \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the maximum possible value of the greatest common divisor of a sequence a_1, a_2, ..., a_N that satisfies the condition.

Sample Input 1

3 14

Sample Output 1

2

Consider the sequence (a_1, a_2, a_3) = (2, 4, 8). Their greatest common divisor is 2, and this is the maximum value.

Sample Input 2

10 123

Sample Output 2

3

Sample Input 3

100000 1000000000

Sample Output 3

10000


var
  n,m,i,max:longint;
begin
  max:=1;
  read(n,m);
  for i:=m div n downto 1 do
  if m mod i=0 then break;
write(i);
end.


var n,m,i,k:longint;
begin
	read(n,m);
	k:=m div n;
    for i:=k downto 2 do
    if (m mod i=0) and (m div i>=n) then
    begin
    	write(i);
        exit;
    end;
end.


var
  n,m,i,max:longint;
begin
  max:=1;
  read(n,m);
  for i:=1 to m div n do begin  if i>sqrt(m) then break;
  if m mod i=0 then max:=i; end;
write(max);
end.


var
  n,m,i,max:longint;
begin
  max:=1;
  read(n,m);
  for i:=1 to m div n+1 do
  if m mod i=0 then max:=i;
write(max);
end.


var n,m,i:longint;
begin
	read(n,m);
    for i:=m-1 downto 2 do
    if (m mod i=0) and (m div i<=n) then
    begin
    	write(i);
        exit;
    end;
end.


var
  i:longint;
begin
  read(i);
  writeln(1110-i);
end.

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


var
  n,i,x,maxo,maxj,oi,ji,maxo2,maxj2:longint;
  j,o:array[1..100000]of longint;
begin
  read(n);
  for i:=1 to n do
    begin
	  read(x);
	  if i mod 2=0 then inc(o[x])
	  else inc(j[x]);
	end;
  maxo:=-maxlongint;
  maxj:=-maxlongint;
  for i:=1 to 100000 do
    begin
    if o[i]>maxo then begin maxo:=o[i]; oi:=i; end;
	if j[i]>maxj then begin maxj:=j[i]; ji:=i; end;
	end;
  if oi=ji then begin
    o[oi]:=-maxlongint;
	j[ji]:=-maxlongint;
  maxo2:=-maxlongint;
  maxj2:=-maxlongint;
  for i:=1 to 100000 do
    begin
    if o[i]>maxo2 then begin maxo2:=o[i]; oi:=i; end;
	if j[i]>maxj2 then begin maxj2:=j[i]; ji:=i; end;
	end;
  if maxo2>=maxj2 then write(n-maxo2-maxj)
  else write(n-maxj2-maxo);
  end
  else write(n-maxo-maxj);
end.

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


var
  n,i,x,maxo,maxj,oi,ji:longint;
  j,o:array[1..100000]of longint;
begin
  read(n);
  for i:=1 to n do
    begin
	  read(x);
	  if i mod 2=0 then inc(o[x])
	  else inc(j[x]);
	end;
  maxo:=-maxlongint;
  maxj:=-maxlongint;
  for i:=1 to 100000 do
    begin
    if o[i]>maxo then begin maxo:=o[i]; oi:=i; end;
	if j[i]>maxj then begin maxj:=j[i]; ji:=i; end;
	end;
 if maxo>=maxj then write(maxo-maxj)
else write(maxj-maxo);
end.


var
  n,i,x,maxo,maxj,oi,ji:longint;
  j,o:array[1..100000]of longint;
begin
  read(n);
  for i:=1 to n do
    begin
	  read(x);
	  if i mod 2=0 then inc(o[x])
	  else inc(j[x]);
	end;
  maxo:=-maxlongint;
  maxj:=-maxlongint;
  for i:=1 to 100000 do
    begin
    if o[i]>maxo then begin maxo:=o[i]; oi:=i; end;
	if j[i]>maxj then begin maxj:=j[i]; ji:=i; end;
	end;
   write(n-maxo-maxj);
end.


var
  a:array[1..100000]of longint;
  b:array[1..2,1..100000]of longint;
  n,m,k,i,p,q,o,f,t:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
     read(a[i]);
	 inc(b[((i-1)mod 2)+1,a[i]]);
	 if a[i]>k then k:=a[i];
  end;
  for i:=1 to k do
  begin
     if b[1,i]>p then begin p:=b[1,i]; o:=i; end;
     if b[2,i]>q then begin q:=b[2,i]; m:=i; end;
  end;
  if o=m then
  begin
    if p>q then
	begin
    for i:=1 to k do
	begin
       if (i<>o)and(b[2,i]>=f) then f:=b[2,i];
    end;
	f:=f+p;
	end else
    if p<q then
	begin
    for i:=1 to k do
	begin
       if (i<>o)and(b[1,i]>=f) then f:=b[1,i];
    end;
	f:=f+q;
	end else
	begin
    for i:=1 to k do
	begin
       if (i<>o)and(b[1,i]>=f) then f:=b[1,i];
       if (i<>o)and(b[2,i]>=t) then t:=b[2,i];
    end;
	if t>f then f:=t+p
	else f:=f+p;
	end;
  end else f:=p+q;
  writeln(n-f);
end.


var
  a,f,g:array[0..100000]of longint;
  p1,p2,q1,q2:array[0..100000]of longint;
  i,j,n,ans,n1,n2:longint;
Procedure max(x:longint);
begin
  if x>ans then ans:=x;
end;
Procedure ch(var x,y:longint);
var
  t:longint;
begin
  t:=x;x:=y;y:=t;
end;
Procedure qs1(l,r:longint);
var
  i,j,mid:longint;
begin
  i:=l;j:=r;mid:=p1[(l+r)div 2];
  repeat
    while p1[i]>mid do inc(i);
    while p1[j]<mid do dec(j);
    if i<=j then
      begin
        ch(p1[i],p1[j]);
        ch(p2[i],p2[j]);
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs1(l,j);
  if i<r then qs1(i,r);
end;
Procedure qs2(l,r:longint);
var
  i,j,mid:longint;
begin
  i:=l;j:=r;mid:=q1[(l+r)div 2];
  repeat
    while q1[i]>mid do inc(i);
    while q1[j]<mid do dec(j);
    if i<=j then
      begin
        ch(q1[i],q1[j]);
        ch(q2[i],q2[j]);
        inc(i);dec(j);
      end;
  until i>j;
  if l<j then qs2(l,j);
  if i<r then qs2(i,r);
end;
begin
  readln(n);
  for i:=1 to n do
    read(a[i]);
  for i:=1 to n do
    begin
      if i mod 2=1 then inc(f[a[i]]) else inc(g[a[i]]);
    end;
  for i:=0 to 100000 do
    begin
      if f[i]<>0 then
        begin
          inc(n1);
          p1[n1]:=f[i];
          p2[n1]:=i;
        end;
      if g[i]<>0 then
        begin
          inc(n2);
          q1[n2]:=g[i];
          q2[n2]:=i;
        end;
    end;
  qs1(1,n1);
  qs2(1,n2);
  for i:=1 to n1 do
    begin
      if p2[i]<>q2[1] then max(p1[i]+q1[1]) else max(p1[i]+q1[2]);
    end;
  write(n-ans);
end.


var
  a:array[1..100000]of longint;
  b:array[1..2,1..100000]of longint;
  n,m,k,i,p,q,o,f:longint;
begin
  readln(n);
  for i:=1 to n do
  begin
     read(a[i]);
	 inc(b[((i-1)mod 2)+1,a[i]]);
	 if a[i]>k then k:=a[i];
  end;
  for i:=1 to k do
  begin
     if b[1,i]>p then begin p:=b[1,i]; o:=i; end;
     if b[2,i]>q then begin q:=b[2,i]; m:=i; end;
  end;
  if o=m then
  begin
    if p>q then
	begin
    for i:=1 to k do
	begin
       if (b[2,i]<>q)and(b[2,i]>f) then f:=b[2,i];
    end;
	f:=f+p;
	end else
	begin
    for i:=1 to k do
	begin
       if (b[1,i]<>p)and(b[1,i]>f) then f:=b[1,i];
    end;
	f:=f+q;
     end;
  end else f:=p+q;
  writeln(n-f);
end.


var
	N,i,L,R,x,y,z,w:Longint;
	a,b:Array[0..100000]of Longint;
begin
	read(N);
	for i:=1 to N do begin
		read(x);
		if i mod 2=1 then inc(a[x])else inc(b[x]);
	end;
	x:=0;
	for i:=1 to 100000 do begin
		if a[x]<a[i]then begin
			y:=x;
			x:=i;
		end else if a[y]<a[i]then begin
			y:=i;
		end;
		if b[z]<b[i]then begin
			w:=z;
			z:=i;
		end else if b[w]<b[i]then begin
			w:=i;
		end;
	end;
	if x<>z then writeln(N-a[x]-b[z])else begin
		L:=N-a[y]-b[z];
		R:=N-a[x]-b[w];
		if L<R then writeln(L)else writeln(R);
	end;
end.


var
  maxn1,maxn2,who1,who2,maxn3,maxn4:longint;
  i,j,m,n,k,p,len1,len2:longint;
  check1,check2:array[0..100005]of longint;
function max(x,y:longint):longint;
begin
  if x>y then exit(x)
  else exit(y);
end;
begin
  readln(n);
  for i:=1 to n do
    begin
      read(k);
      if i mod 2=1 then inc(check1[k])
      else inc(check2[k]);
    end;
  for i:=1 to 100000 do
    if check1[i]>maxn1 then
      begin
        maxn1:=check1[i]; who1:=i;
      end;

  for i:=1 to 100000 do
    if (i<>who1)and(check1[i]>maxn3) then
      maxn3:=check1[i];

  for i:=1 to 100000 do
    if check2[i]>maxn2 then
      begin
        maxn2:=check2[i]; who2:=i;
      end;

  for i:=1 to 100000 do
    if (i<>who2)and(check2[i]>maxn4) then
      maxn4:=check2[i];

  if who1<>who2 then
    writeln(n-maxn1-maxn2)
  else
    writeln(n-max(maxn1+maxn4,maxn2+maxn3));
end.


var
  n,i,j,k,t,x,p1,p2,q1,q2,maxa1,maxa2,maxb1,maxb2:longint;
  a,b:array[1..100000] of longint;
function max(x,y:longint):longint;
  begin
    if x>y
      then exit(x)
      else exit(y);
  end;
begin
  readln(n);
  for i:=1 to n do
    begin
      read(x);
      if i mod 2=0
        then inc(b[x])
        else inc(a[x]);
    end;
  for i:=1 to 100000 do
    begin
      if a[i]>maxa1
        then
          begin
            maxa2:=maxa1;
            maxa1:=a[i];
            p1:=i;
          end
        else
      if a[i]>maxa2
        then maxa2:=a[i];
    end;
  for i:=1 to 100000 do
    begin
      if b[i]>maxb1
        then
          begin
            maxb2:=maxb1;
            maxb1:=b[i];
            q1:=i;
          end
        else
          if b[i]>maxb2
            then maxb2:=b[i];
    end;
  if p1<>q1
    then writeln(n-maxa1-maxb1)
    else writeln(n-max(maxa1+maxb2,maxa2+maxb1));
end.


var
	n,i,j,c,a,b:Longint;
	x,y,d:array[1..100000]of Longint;
begin
	read(n);
	for i:=1 to n do begin
		read(x[i],y[i]);
		if (abs(x[i])+abs(y[i]))mod 2<>(abs(x[1])+abs(y[1]))mod 2 then begin
			writeln(-1);
			exit;
		end;
	end;
	a:=1073741824;
	while a>0 do begin
		inc(c);
		d[c]:=a;
		a:=a div 2;
	end;
	if (abs(x[1])+abs(y[1]))mod 2=1 then begin
		inc(c);
		d[c]:=1;
	end;
	writeln(c);
	for i:=1 to c do write(d[i],' ');
	writeln;
	for i:=1 to n do begin
		a:=x[i];
		b:=y[i];
		for j:=1 to c do begin
			if abs(a)>abs(b) then begin
				if a>0 then begin
					dec(a,d[j]);
					write('R');
				end else begin
					inc(a,d[j]);
					write('L');
				end;
			end else begin
				if b>0 then begin
					dec(b,d[j]);
					write('U');
				end else begin
					inc(b,d[j]);
					write('D');
				end;
			end;
		end;
		writeln;
	end;
end.

Score : 600 points

Problem Statement

Snuke is introducing a robot arm with the following properties to his factory:

The robot arm consists of m sections and m+1 joints. The sections are numbered 1, 2, ..., m, and the joints are numbered 0, 1, ..., m. Section i connects Joint i-1 and Joint i. The length of Section i is d_i.

For each section, its mode can be specified individually. There are four modes: L, R, D and U. The mode of a section decides the direction of that section. If we consider the factory as a coordinate plane, the position of Joint i will be determined as follows (we denote its coordinates as (x_i, y_i)):

(x_0, y_0) = (0, 0).

If the mode of Section i is L, (x_{i}, y_{i}) = (x_{i-1} - d_{i}, y_{i-1}).

If the mode of Section i is R, (x_{i}, y_{i}) = (x_{i-1} + d_{i}, y_{i-1}).

If the mode of Section i is D, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} - d_{i}).

If the mode of Section i is U, (x_{i}, y_{i}) = (x_{i-1}, y_{i-1} + d_{i}).

Snuke would like to introduce a robot arm so that the position of Joint m can be matched with all of the N points (X_1, Y_1), (X_2, Y_2), ..., (X_N, Y_N) by properly specifying the modes of the sections.
Is this possible?
If so, find such a robot arm and how to bring Joint m to each point (X_j, Y_j).

Constraints

All values in input are integers.

1 \leq N \leq 1000

-10^9 \leq X_i \leq 10^9

-10^9 \leq Y_i \leq 10^9

Partial Score

In the test cases worth 300 points, -10 \leq X_i \leq 10 and -10 \leq Y_i \leq 10 hold.

Input

Input is given from Standard Input in the following format:

N
X_1 Y_1
X_2 Y_2
:
X_N Y_N

Output

If the condition can be satisfied, follow the following format. If the condition cannot be satisfied, print -1.

m
d_1 d_2 ... d_m
w_1
w_2
:
w_N

m and d_i are the configurations of the robot arm. Refer to the problem statement for what each of them means.
Here, 1 \leq m \leq 40 and 1 \leq d_i \leq 10^{12} must hold. Also, m and d_i must all be integers.

w_j is a string of length m that represents the way to bring Joint m of the robot arm to point (X_j, Y_j).
The i-th character of w_j should be one of the letters L, R, D and U, representing the mode of Section i.

Sample Input 1

3
-1 0
0 3
2 -1

Sample Output 1

2
1 2
RL
UU
DR

In the given way to bring Joint m of the robot arm to each (X_j, Y_j), the positions of the joints will be as follows:

To (X_1, Y_1) = (-1, 0): First, the position of Joint 0 is (x_0, y_0) = (0, 0). As the mode of Section 1 is R, the position of Joint 1 is (x_1, y_1) = (1, 0). Then, as the mode of Section 2 is L, the position of Joint 2 is (x_2, y_2) = (-1, 0).

To (X_2, Y_2) = (0, 3): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, 1), (x_2, y_2) = (0, 3).

To (X_3, Y_3) = (2, -1): (x_0, y_0) = (0, 0), (x_1, y_1) = (0, -1), (x_2, y_2) = (2, -1).

Sample Input 2

5
0 0
1 0
2 0
3 0
4 0

Sample Output 2

-1

Sample Input 3

2
1 1
1 1

Sample Output 3

2
1 1
RU
UR

There may be duplicated points among (X_j, Y_j).

Sample Input 4

3
-7 -3
7 3
-3 -7

Sample Output 4

5
3 1 4 1 5
LRDUL
RDULR
DULRD


var
    n,p,m,i,j:longint;
    x,y,d:array[0..1010] of longint;
begin
    readln(n);
    readln(x[1],y[1]);
    p:=abs(x[1]+y[1]) mod 2;
    for i:=2 to n do
        begin
            readln(x[i],y[i]);
            if p<>abs(x[i]+y[i]) mod 2 then
                begin
                    writeln(-1);exit;
                end;
        end;
    m:=31+1-p;writeln(m);d[31]:=1;
    for i:=30 downto 1 do d[i]:=d[i+1]*2;
    if m=32 then d[32]:=1;
    for i:=1 to m do write(d[i],' ');writeln;
    for i:=1 to n do
        begin
            for j:=1 to m do
                if abs(x[i])>abs(y[i]) then
                    if x[i]>0 then begin dec(x[i],d[j]);write('R');end
                    else begin inc(x[i],d[j]);write('L');end
                else
                    if y[i]>0 then begin dec(y[i],d[j]);write('U');end
                    else begin inc(y[i],d[j]);write('D');end;
            writeln;
        end;
end.


var
  n,i,j,k,t,root:longint;
  a:array[1..100000] of integer;
  x:char;
  first,next,last,b:array[1..100000] of longint;
begin
  i:=0;
  while not eoln do
    begin
      n:=n+1;
      read(x);
      if x='1'
        then a[n]:=1
        else a[n]:=0;
    end;
  if (a[1]=0)or(a[n]=1)
    then
      begin
        writeln(-1);
        halt;
      end;
  for i:=2 to n do
    if (a[i]=1)and(a[n-i]=0)
      then
        begin
          writeln(-1);
          halt;
        end;
  root:=1;
  for i:=2 to n do
    if a[i-1]=0
      then writeln(root,' ',i)
      else
        begin
          writeln(root,' ',i);
          root:=i;
        end;
end.

Score : 700 points

Problem Statement

You are given a string s of length n.
Does a tree with n vertices that satisfies the following conditions exist?

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

The edges are numbered 1,2,..., n-1, and Edge i connects Vertex u_i and v_i.

If the i-th character in s is 1, we can have a connected component of size i by removing one edge from the tree.

If the i-th character in s is 0, we cannot have a connected component of size i by removing any one edge from the tree.

If such a tree exists, construct one such tree.

Constraints

2 \leq n \leq 10^5

s is a string of length n consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

s

Output

If a tree with n vertices that satisfies the conditions does not exist, print -1.

If a tree with n vertices that satisfies the conditions exist, print n-1 lines.
The i-th line should contain u_i and v_i with a space in between.
If there are multiple trees that satisfy the conditions, any such tree will be accepted.

Sample Input 1

1111

Sample Output 1

-1

It is impossible to have a connected component of size n after removing one edge from a tree with n vertices.

Sample Input 2

1110

Sample Output 2

1 2
2 3
3 4

If Edge 1 or Edge 3 is removed, we will have a connected component of size 1 and another of size 3. If Edge 2 is removed, we will have two connected components, each of size 2.

Sample Input 3

1010

Sample Output 3

1 2
1 3
1 4

Removing any edge will result in a connected component of size 1 and another of size 3.


var
  n,i,j,k,t,root:longint;
  a:array[1..10000] of integer;
  x:char;
  first,next,last,b:array[1..100000] of longint;
begin
  i:=0;
  while not eoln do
    begin
      n:=n+1;
      read(x);
      if x='1'
        then a[n]:=1
        else a[n]:=0;
    end;
  if (a[1]=0)or(a[n]=1)
    then
      begin
        writeln(-1);
        halt;
      end;
  for i:=2 to n do
    if (a[i]=1)and(a[n-i]=0)
      then
        begin
          writeln(-1);
          halt;
        end;
  root:=1;
  for i:=2 to n do
    if a[i-1]=0
      then writeln(root,' ',i)
      else
        begin
          writeln(root,' ',i);
          root:=i;
        end;
end.


var
  n,i,j,k,t,root:longint;
  a:array[1..10000] of integer;
  x:char;
  first,next,last,b:array[1..100000] of longint;
begin
  i:=0;
  while not eoln do
    begin
      n:=n+1;
      read(x);
      if x='1'
        then a[n]:=1
        else a[n]:=0;
    end;
  if (a[1]=0)or(a[n]=1)
    then
      begin
        writeln(-1);
        halt;
      end;
  for i:=2 to n div 2 do
    if (a[i]=1)and(a[n-i]=0)
      then
        begin
          writeln(-1);
          halt;
        end;
  root:=1;
  writeln(1,' ',2);
  if a[2]=1
    then root:=2;
  for i:=3 to n do
    if a[i-1]=0
      then writeln(root,' ',i)
      else
        begin
          writeln(root,' ',i);
          root:=i;
        end;
end.


var
  a:array[1..200000] of longint;
  b,c,i,j,k,t,n,m,x,y:longint;
  s:ansistring;
begin
  readln(s);
  n:=length(s);
  for i:=1 to n-1 do
  begin
    k:=0;
    if s[i]='1' then if s[n-i]='0' then k:=1;
  end;
  if s[1]='0' then k:=1;
  if s[n]='1' then k:=1;
  if k=1 then writeln('-1') else
  begin
    x:=1;
    for i:=1 to n-1 do
    begin
      writeln(x,' ',i+1);
      if s[i]='1' then x:=i+1;
    end;
  end;
end.


var
    s,t,r:ansistring;
    i:longint;
    f:array[0..31] of longint;
begin
    readln(s);readln(t);
    for i:=1 to length(s) do
        if f[ord(s[i])-97]=0 then f[ord(s[i])-97]:=ord(t[i])-97
        else if f[ord(s[i])-97]<>ord(t[i])-97 then
            begin
                writeln('No');
                exit;
            end;
    r:=s;s:=t;t:=r;fillchar(f,sizeof(f),0);
    for i:=1 to length(s) do
        if f[ord(s[i])-97]=0 then f[ord(s[i])-97]:=ord(t[i])-97
        else if f[ord(s[i])-97]<>ord(t[i])-97 then
            begin
                writeln('No');
                exit;
            end;
    writeln('Yes');
end.

Score : 300 points

Problem Statement

You are given strings S and T consisting of lowercase English letters.

You can perform the following operation on S any number of times:

Operation: Choose two distinct lowercase English letters c_1 and c_2, then replace every occurrence of c_1 with c_2, and every occurrence of c_2 with c_1.

Determine if S and T can be made equal by performing the operation zero or more times.

Constraints

1 \leq |S| \leq 2 \times 10^5

|S| = |T|

S and T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T

Output

If S and T can be made equal, print Yes; otherwise, print No.

Sample Input 1

azzel
apple

Sample Output 1

Yes

azzel can be changed to apple, as follows:

Choose e as c_1 and l as c_2. azzel becomes azzle.

Choose z as c_1 and p as c_2. azzle becomes apple.

Sample Input 2

chokudai
redcoder

Sample Output 2

No

No sequences of operation can change chokudai to redcoder.

Sample Input 3

abcdefghijklmnopqrstuvwxyz
ibyhqfrekavclxjstdwgpzmonu

Sample Output 3

Yes


var s1,s2:ansistring;
c:array['a'..'z']of 0..1;
f:array['a'..'z']of 'a'..'z';
i:longint;
begin
	readln(s1);
	readln(s2);
	fillchar(c,sizeof(c),0);
	for i:=1 to length(s1) do
	begin
		if c[s1[i]]=0 then
		begin
			f[s1[i]]:=s2[i];
			c[s1[i]]:=1;
		end
		else if s2[i]<>f[s1[i]] then begin write('No');halt;end;
	end;
	fillchar(c,sizeof(c),0);
	for i:=1 to length(s1) do
	begin
		if c[s2[i]]=0 then
		begin
			f[s2[i]]:=s1[i];
			c[s2[i]]:=1;
		end
		else if s1[i]<>f[s2[i]] then begin write('No');halt;end;
	end;
	write('Yes');
end.


var
  s1,s2:string;
  a,b:array['a'..'z']of longint;
  c:array[1..26]of boolean;
  f:boolean;
  i,j,k,p:longint;
begin
  readln(s1);
  read(s2);
  k:=length(s2);
  f:=true;
  if length(s1)<length(s2) then begin k:=length(s1); f:=false; end;
  for i:=1 to k-1 do
  begin
    inc(a[s1[i]]);
	inc(b[s2[i]]);
  end;
  if f then inc(b[s2[k]])
  else inc(a[s1[k]]);
  if f then p:=length(s1)
  else p:=length(s2);
  for i:=k to p do
  begin
  begin
     if f then inc(a[s1[i]])
	 else inc(b[s2[i]]);
  end;
  end;
  f:=true;
  for i:=1 to 26 do
  begin
     p:=a[chr(i+ord('a')-1)];
	 for j:=1 to 26 do
	 begin
	    if (c[j]=false)and(b[chr(j+ord('a')-1)]=p) then
		begin
		   c[j]:=true;
		   break;
		end;
		if j=26 then f:=false;
     end;
	 if f=false then begin writeln('No'); halt; end;
  end;
  writeln('Yes');
end.


var
  s1,s2:string;
  a,b:array['a'..'z']of longint;
  c:array[1..26]of boolean;
  f:boolean;
  i,j,k,p:longint;
begin
  readln(s1);
  read(s2);
  k:=length(s2);
  f:=true;
  if length(s1)<length(s2) then
     begin
       k:=length(s1);
       f:=false;
     end;
  for i:=1 to k-1 do
    begin
      inc(a[s1[i]]);
      inc(b[s2[i]]);
    end;
  if f then inc(b[s2[k]])
       else inc(a[s1[k]]);
  if f then p:=length(s1)
    else p:=length(s2);
  for i:=k to p do
  begin
  begin
     if f then inc(a[s1[i]])
	 else inc(b[s2[i]]);
  end;
  end;
  f:=true;
  for i:=1 to 26 do
  begin
     p:=a[chr(i+ord('a')-1)];
	 for j:=1 to 26 do
	 begin
	    if (c[j]=false)and(b[chr(j+ord('a')-1)]=p) then
		begin
		   c[j]:=true;
		   break;
		end;
	if j=26 then f:=false;
     end;
	 if f=false then begin write('No');
         exit; end;
  end;
  write('Yes');
end.


Score : 400 points

Problem Statement

You are given positive integers N and M.

How many sequences a of length N consisting of positive integers satisfy a_1 \times a_2 \times ... \times a_N = M? Find the count modulo 10^9+7.

Here, two sequences a' and a'' are considered different when there exists some i such that a_i' \neq a_i''.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^9

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of the sequences consisting of positive integers that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

2 6

Sample Output 1

4

Four sequences satisfy the condition: \{a_1, a_2\} = \{1, 6\}, \{2, 3\}, \{3, 2\} and \{6, 1\}.

Sample Input 2

3 12

Sample Output 2

18

Sample Input 3

100000 1000000000

Sample Output 3

957870001


uses math;
var
  a:array[0..10000]of longint;
  n,x,i,sum:longint;
 procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]<x do
            inc(i);
           while x<a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
  readln(n,x);
  for i:=1 to n do
    begin
	  read(a[i]);
	end;
  sort(1,n);
  i:=1;
  while (x>=a[i])and(i<=n) do
    begin
	  x:=x-a[i];
	  inc(i)
	end;
  if x<>0 then dec(i);
  writeln(max(0,i-1));
end.

Score : 200 points

Problem Statement

There are N children, numbered 1, 2, ..., N.

Snuke has decided to distribute x sweets among them.
He needs to give out all the x sweets, but some of the children may get zero sweets.

For each i (1 \leq i \leq N), Child i will be happy if he/she gets exactly a_i sweets.
Snuke is trying to maximize the number of happy children by optimally distributing the sweets.
Find the maximum possible number of happy children.

Constraints

All values in input are integers.

2 \leq N \leq 100

1 \leq x \leq 10^9

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the maximum possible number of happy children.

Sample Input 1

3 70
20 30 10

Sample Output 1

2

One optimal way to distribute sweets is (20, 30, 20).

Sample Input 2

3 10
20 30 10

Sample Output 2

1

The optimal way to distribute sweets is (0, 0, 10).

Sample Input 3

4 1111
1 10 100 1000

Sample Output 3

4

The optimal way to distribute sweets is (1, 10, 100, 1000).

Sample Input 4

2 10
20 20

Sample Output 4

0

No children will be happy, no matter how the sweets are distributed.


type
	arr=array[1..4] of longint;
var
	f:array[0..200000] of arr;
	n,m,s,k,x,y,i,j:longint;
	t:array[0..600,0..600] of boolean;
procedure work(a,b,c,d:longint);
begin
	s:=s+abs(a-c)+abs(b-d);
	inc(k);
	f[k,1]:=a;
	f[k,2]:=b;
	f[k,3]:=c;
	f[k,4]:=d;
	t[a,b]:=false;
	t[c,d]:=false;
end;
procedure print(x:arr);
var
	i:longint;
begin
	if x[1]<x[3] then
		for i:=x[1] to x[3]-1 do writeln(i,' ',x[2],' ',i+1,' ',x[2])
	else
		for i:=x[1] downto x[3]+1 do writeln(i,' ',x[2],' ',i-1,' ',x[2]);
	if x[2]<x[4] then
		for i:=x[2] to x[4]-1 do writeln(x[3],' ',i,' ',x[3],' ',i+1)
	else
		for i:=x[2] downto x[4]+1 do writeln(x[3],' ',i,' ',x[3],' ',i-1);
end;
begin
	readln(n,m);
	for i:=1 to n do
	begin
		for j:=1 to m do
		begin
			read(x);
			t[i,j]:=(x mod 2=1);
		end;
		readln;
	end;
	s:=0; x:=0; y:=0;
	for i:=1 to n do
		if i mod 2=1 then
		begin
			for j:=1 to m do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end
		else
		begin
			for j:=m downto 1 do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end;
	writeln(s);
	for i:=1 to k do print(f[i]);
end.

Score : 400 points

Problem Statement

There is a grid of square cells with H horizontal rows and W vertical columns. The cell at the i-th row and the j-th column will be denoted as Cell (i, j).

In Cell (i, j), a_{ij} coins are placed.

You can perform the following operation any number of times:

Operation: Choose a cell that was not chosen before and contains one or more coins, then move one of those coins to a vertically or horizontally adjacent cell.

Maximize the number of cells containing an even number of coins.

Constraints

All values in input are integers.

1 \leq H, W \leq 500

0 \leq a_{ij} \leq 9

Input

Input is given from Standard Input in the following format:

H W
a_{11} a_{12} ... a_{1W}
a_{21} a_{22} ... a_{2W}
:
a_{H1} a_{H2} ... a_{HW}

Output

Print a sequence of operations that maximizes the number of cells containing an even number of coins, in the following format:

N
y_1 x_1 y_1' x_1'
y_2 x_2 y_2' x_2'
:
y_N x_N y_N' x_N'

That is, in the first line, print an integer N between 0 and H \times W (inclusive), representing the number of operations.

In the (i+1)-th line (1 \leq i \leq N), print four integers y_i, x_i, y_i' and x_i' (1 \leq y_i, y_i' \leq H and 1 \leq x_i, x_i' \leq W), representing the i-th operation. These four integers represents the operation of moving one of the coins placed in Cell (y_i, x_i) to a vertically or horizontally adjacent cell, (y_i', x_i').

Note that if the specified operation violates the specification in the problem statement or the output format is invalid, it will result in Wrong Answer.

Sample Input 1

2 3
1 2 3
0 1 1

Sample Output 1

3
2 2 2 3
1 1 1 2
1 3 1 2

Every cell contains an even number of coins after the following sequence of operations:

Move the coin in Cell (2, 2) to Cell (2, 3).

Move the coin in Cell (1, 1) to Cell (1, 2).

Move one of the coins in Cell (1, 3) to Cell (1, 2).

Sample Input 2

3 2
1 0
2 1
1 0

Sample Output 2

3
1 1 1 2
1 2 2 2
3 1 3 2

Sample Input 3

1 5
9 9 9 9 9

Sample Output 3

2
1 1 1 2
1 3 1 4


var
  a:array[-10..510,-10..510]of longint;
  b:array[0..1000000,1..4]of longint;
  i,j,n,m,ans:longint;
begin
  readln(n,m);
  for i:=1 to n do
    for j:=1 to m do
      begin
        read(a[i,j]);
        if j=m then readln;
      end;
  for i:=1 to n do
    for j:=1 to m-1 do
      begin
        if a[i,j] mod 2<>0 then
          begin
            inc(ans);
            b[ans,1]:=i;b[ans,2]:=j;
            b[ans,3]:=i;b[ans,4]:=j+1;
            inc(a[i,j+1],a[i,j]);
            a[i,j]:=0;
          end;
        end;
  for i:=1 to n-1 do
  begin
    if a[i,m] mod 2<>0 then
      begin
             inc(ans);
            b[ans,1]:=i;b[ans,2]:=m;
            b[ans,3]:=i+1;b[ans,4]:=m;
            inc(a[i+1,m],a[i,m]);
            a[i,m]:=0;
      end;
  end;
  writeln(ans);
  for i:=1 to ans do begin
    for j:=1 to 4 do
             write(b[i,j],' ');
             writeln;
    end;
end.


type
	arr=array[1..4] of longint;
var
	f:array[0..200000] of arr;
	n,m,s,k,x,y,i,j:longint;
	t:array[0..6040,0..600] of boolean;
procedure work(a,b,c,d:longint);
begin
	s:=s+abs(a-c)+abs(b-d);
	inc(k);
	f[k,1]:=a;
	f[k,2]:=b;
	f[k,3]:=c;
	f[k,4]:=d;
	t[a,b]:=false;
	t[c,d]:=false;
end;
procedure print(x:arr);
var
	i:longint;
begin
	if x[1]<x[3] then
		for i:=x[1] to x[3]-1 do writeln(i,' ',x[2],' ',i+1,' ',x[2])
	else
		for i:=x[1] downto x[3]+1 do writeln(i,' ',x[2],' ',i-1,' ',x[2]);
	if x[2]<x[4] then
		for i:=x[2] to x[4]-1 do writeln(x[3],' ',i,' ',x[3],' ',i+1)
	else
		for i:=x[2] downto x[4]+1 do writeln(x[3],' ',i,' ',x[3],' ',i-1);
end;
begin
	readln(n,m);
	for i:=1 to n do
	begin
		for j:=1 to m do
		begin
			read(x);
			t[i,j]:=(x mod 2=1);
		end;
		readln;
	end;
	s:=0; x:=0; y:=0;
	for i:=1 to n do
		if i mod 2=1 then
		begin
			for j:=1 to m do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end
		else
		begin
			for j:=m downto 1 do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end;
	writeln(s);
	for i:=1 to k do print(f[i]);
end.


var
    n,m,i,j,x,cnt:longint;
    a:array[0..501,0..501] of longint;
    x1,y1,x2,y2:array[0..250001] of longint;
begin
    readln(n,m);
    cnt:=0;
    for i:=1 to n do
        begin
            for j:=1 to m-1 do
                begin
                    read(x);inc(a[i,j],x);
                    if odd(a[i,j]) then
                        begin
                            inc(cnt);
                            x1[cnt]:=i;x2[cnt]:=i;y1[cnt]:=j;y2[cnt]:=j+1;
                            inc(a[i,j+1]);
                        end;
                end;
            read(x);inc(a[i,m],x);
            if odd(a[i,m]) then
                begin
                    inc(cnt);
                    x1[cnt]:=i;x2[cnt]:=i+1;y1[cnt]:=m;y2[cnt]:=m;
                    inc(a[i+1,m]);
                end;
        end;
    if odd(a[n,m]) then dec(cnt);
    writeln(cnt);
    for i:=1 to cnt do
        writeln(x1[i],' ',y1[i],' ',x2[i],' ',y2[i]);
end.


type
	arr=array[1..4] of longint;
var
	f:array[0..1000000] of arr;
	n,m,s,k,x,y,i,j:longint;
	t:array[0..600,0..600] of boolean;
procedure work(a,b,c,d:longint);
begin
	s:=s+abs(a-c)+abs(b-d);
	inc(k);
	f[k,1]:=a;
	f[k,2]:=b;
	f[k,3]:=c;
	f[k,4]:=d;
	t[a,b]:=false;
	t[c,d]:=false;
end;
procedure print(x:arr);
var
	i:longint;
begin
	if x[1]<x[3] then
		for i:=x[1] to x[3]-1 do writeln(i,' ',x[2],' ',i+1,' ',x[2])
	else
		for i:=x[1] downto x[3]+1 do writeln(i,' ',x[2],' ',i-1,' ',x[2]);
	if x[2]<x[4] then
		for i:=x[2] to x[4]-1 do writeln(x[3],' ',i,' ',x[3],' ',i+1)
	else
		for i:=x[2] downto x[4]+1 do writeln(x[3],' ',i,' ',x[3],' ',i-1);
end;
begin
	read(n,m);
	for i:=1 to n do
		for j:=1 to m do
		begin
			read(x);
			t[i,j]:=(x mod 2=1);
		end;
	s:=0; x:=0; y:=0;
	for i:=1 to n do
		if i mod 2=1 then
		begin
			for j:=1 to m do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end
		else
		begin
			for j:=m downto 1 do
				if t[i,j] then
					if (x<>0) and (y<>0) then
					begin
						work(x,y,i,j);
						x:=0; y:=0;
					end
					else
					begin
						x:=i; y:=j;
					end;
		end;
	writeln(s);
	for i:=1 to k do print(f[i]);
end.


type
	arr=array[1..4] of longint;
var
	f:array[0..3000] of arr;
	n,m,s,k,x,y,i,j:longint;
	t:array[0..600,0..600] of boolean;
procedure work(a,b,c,d:longint);
begin
	s:=s+abs(a-c)+abs(b-d);
	inc(k);
	f[k,1]:=a;
	f[k,2]:=b;
	f[k,3]:=c;
	f[k,4]:=d;
	t[a,b]:=false;
	t[c,d]:=false;
end;
procedure print(x:arr);
var
	i:longint;
begin
	if x[1]<x[3] then
		for i:=x[1] to x[3]-1 do writeln(i,' ',x[2],' ',i+1,' ',x[2])
	else
		for i:=x[1] downto x[3]+1 do writeln(i,' ',x[2],' ',i-1,' ',x[2]);
	if x[2]<x[4] then
		for i:=x[2] to x[4]-1 do writeln(x[3],' ',i,' ',x[3],' ',i+1)
	else
		for i:=x[2] downto x[4]+1 do writeln(x[3],' ',i,' ',x[3],' ',i-1);
end;
begin
	read(n,m);
	for i:=1 to n do
		for j:=1 to m do
		begin
			read(x);
			t[i,j]:=(x mod 2=1);
		end;
	s:=0; x:=0; y:=0;
	for i:=1 to n do
		for j:=1 to m do
			if t[i,j] then
				if (x<>0) and (y<>0) then
				begin
					work(x,y,i,j);
					x:=0; y:=0;
				end
				else
				begin
					x:=i; y:=j;
				end;
	writeln(s);
	for i:=1 to k do print(f[i]);
end.


uses
  SysUtils,
  Classes;

 procedure MainProc;
	var
		input, oddNum : Integer;
	begin
	ReadLn(input);
	oddNum := (input div 2) + (input mod 2);
	WriteLn(oddNum * (input - oddNum));
	end;

begin
MainProc;
end.

Score : 100 points

Problem Statement

Find the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive). The order does not matter.

Constraints

2\leq K\leq 100

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the number of ways to choose a pair of an even number and an odd number from the positive integers between 1 and K (inclusive).

Sample Input 1

3

Sample Output 1

2

Two pairs can be chosen: (2,1) and (2,3).

Sample Input 2

6

Sample Output 2

9

Sample Input 3

11

Sample Output 3

30

Sample Input 4

50

Sample Output 4

625


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min,ax,ay,bx,by:Integer;
  l:array of Integer;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Read  (ax);
    Read  (ay);
    Read  (bx);
    Readln(by);
    count:=0;
    max  :=0;
    min  :=100;

    Write  (bx-(by-ay));
    Write  (' ');
    Write  (by+(bx-ax));
    Write  (' ');
    Write  (ax+ay-by);
    Write  (' ');
    Writeln(ay-(ax-bx));
    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

There is a square in the xy-plane. The coordinates of its four vertices are (x_1,y_1),(x_2,y_2),(x_3,y_3) and (x_4,y_4) in counter-clockwise order.
(Assume that the positive x-axis points right, and the positive y-axis points up.)

Takahashi remembers (x_1,y_1) and (x_2,y_2), but he has forgot (x_3,y_3) and (x_4,y_4).

Given x_1,x_2,y_1,y_2, restore x_3,y_3,x_4,y_4. It can be shown that x_3,y_3,x_4 and y_4 uniquely exist and have integer values.

Constraints

|x_1|,|y_1|,|x_2|,|y_2| \leq 100

(x_1,y_1) ≠ (x_2,y_2)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1 x_2 y_2

Output

Print x_3,y_3,x_4 and y_4 as integers, in this order.

Sample Input 1

0 0 0 1

Sample Output 1

-1 1 -1 0

(0,0),(0,1),(-1,1),(-1,0) is the four vertices of a square in counter-clockwise order.
Note that (x_3,y_3)=(1,1),(x_4,y_4)=(1,0) is not accepted, as the vertices are in clockwise order.

Sample Input 2

2 3 6 6

Sample Output 2

3 10 -1 7

Sample Input 3

31 -41 -59 26

Sample Output 3

-126 -64 -36 -131


uses
	SysUtils,
	Classes;

procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		x1, y1 : Integer;
		x2, y2 : Integer;
		x3, y3: Integer;
		x4, y4: Integer;
		edgeLenX: Integer;
		edgeLenY: Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		x1 := StrToInt(strlist.Strings[0]);
		y1 := StrToInt(strlist.Strings[1]);
		x2 := StrToInt(strlist.Strings[2]);
		y2:= StrToInt(strlist.Strings[3]);
	finally
			FreeAndNil(strlist);
			end;
	edgeLenX := x1 - x2;
	edgeLenY := y1 - y2;

	x3 := x2 + edgeLenY;
	y3 := y2 - edgeLenX;
	x4 := x1 + edgeLenY;
	y4 := y1 - edgeLenX;
	text := IntTostr(x3) + ' ' + IntTostr(y3) + ' ' + IntTostr(x4) + ' ' + IntTostr(y4);
	WriteLn(text);
	end;

begin
MainProc;
end.


var
  i,k,n,q,j:longint;
  ans:qword;
begin

  readln(n,k);
  i:=k div 2;
  while i<=n do
  begin
    j:=k-(i mod k);
    while j<=n do
    begin
	  q:=i mod k;
	  if q=0 then q:=k;
	  if ((i+q) mod k=0) then
      while q<=n do
	  begin
         if ((i+q) mod k=0)and((q+j) mod k=0) then begin ans:=ans+(n-q) div k+1; break; end;
		 inc(q);
	  end;
	  j:=j+k;
	end;
	inc(i);
  end;
  writeln(ans);

end.

Score : 300 points

Problem Statement

You are given integers N and K. Find the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.
The order of a,b,c does matter, and some of them can be the same.

Constraints

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

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of triples (a,b,c) of positive integers not greater than N such that a+b,b+c and c+a are all multiples of K.

Sample Input 1

3 2

Sample Output 1

9

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

Sample Input 2

5 3

Sample Output 2

1

Sample Input 3

31415 9265

Sample Output 3

27

Sample Input 4

35897 932

Sample Output 4

114191


var n,k,i:longint;
a:array[1..3]of longint;
b:array[1..20000]of longint;
count,m:longint;
procedure dfs(x:integer);
var j:longint;
begin
	if x>3 then
	begin
		if (a[1]+a[3]) mod k=0 then inc(count);
		exit;
	end;
	for j:=1 to m do
		if ((b[j]-a[x-1])<=n) and ((b[j]-a[x-1])>0) then
		begin
			a[x]:=b[j]-a[x-1];
			dfs(x+1);
		end;
end;
begin
	read(n,k);
	m:=2*n div k;
	for i:=1 to m do b[i]:=i*k;
	for i:=1 to n do
	begin
		a[1]:=i;
		dfs(2);
	end;
	write(count);
end.


var
	a,b,c:longint;
	n,k,s:int64;
function ans(x:longint):int64;
begin
	if x mod k=0 then exit(n div k)
	else exit(n div k+1);
end;
begin
	read(n,k);
	s:=0;
	if n<k then
	begin
		writeln(1-k mod 2);
		halt;
	end;
	for a:=1 to n do
		if (a mod k=0) or (k mod 2=0) and (a mod k=k div 2) then
			s:=s+ans(a)*ans(a);
	writeln(s);
end.


var
	a:longint;
	n,k,s:int64;
function ans(x:longint):int64;
begin
	if x mod k=0 then exit(n div k)
	else exit(n div k+1);
end;
begin
	read(n,k);
	s:=0;
	for a:=1 to n do
		if (a mod k=0) or (k mod 2=0) and (a mod k=k div 2) then
			s:=s+ans(a)*ans(a);
	writeln(s);
	readln; readln;
end.


var
	a,b,c,t:longint;
	n,k,s:int64;
function ans(x:longint):longint;
begin
	if x mod k=0 then exit(n div k)
	else exit(n div k+1);
end;
begin
	read(n,k);
	s:=0;
	for a:=1 to n do
		if (a mod k=0) or (k mod 2=0) and (a mod k=k div 2) then
			s:=s+ans(a)*ans(a);
	writeln(s);
end.


var
	a,b,c,t:longint;
	n,k,s:int64;
function ans(x:longint):longint;
begin
	if x mod k=0 then exit(x div k)
	else exit(x div k+1);
end;
begin
	read(n,k);
	s:=0;
	for a:=1 to n do
		s:=s+ans(a)*ans(a);
	writeln(s);
end.


var
  i,k,n,p,j:longint;
  t,ans:qword;
begin
  readln(n,k);
  for i:=(k div 3) to k do
  for j:=k-(i mod k) to k do
  for p:=(k div 3) to k do
  if ((i+j) mod k=0)and((i+p) mod k=0)and((p+j) mod k=0) then
  begin
    t:=((n-j) div k+1)*((n-i) div k+1)*((n-p) div k+1);
    ans:=ans+t;
  end;
  writeln(ans);
end.


program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;
var
  a,h,w,i,j,count,x,y,sw,m,n,b,c,soeji,sum,max,k,min:Integer;
  l:array of array of char;
  s,s123,s1,s2,s3:string;
  al:string = 'abcdefghijklmnopqrstuvwxyz';
  tf,tfl,tfr:Boolean;
  bh,bw:array of Boolean;
begin
  try
    { TODO -oUser -cConsole メイン : ここにコードを記述してください }
    Read(h);
    Readln(w);
    count:=0;
    max  :=0;
    min  :=100;

    SetLength(bh,h);
    SetLength(bw,w);

    SetLength(l,h);
    for i := 0 to h do
      SetLength(l[i],w);

    for i := 0 to h-1 do begin
      for j := 0 to w-1 do begin
        Read(l[i][j]);      //読み込む
        if l[i][j]='#' then //ついでに横一行を記憶
          bh[i]:=True;
      end;
      Readln;
    end;

    for j := 0 to w-1 do begin //縦
      for i := 0 to h-1 do
        if l[i][j]='#' then
          bw[j]:=True;
    end;

    for i := 0 to h-1 do begin
      if bh[i] then begin //消す行なら1行まるごと飛ばす
        for j := 0 to w-1 do begin
          if bw[j] then //消す列なら1個ずつまたぐ
            Write(l[i][j]);
        end;
        Writeln;
      end;
    end;

    Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Score : 200 points

Problem Statement

There is a grid of squares with H horizontal rows and W vertical columns.
The square at the i-th row from the top and the j-th column from the left is represented as (i, j).
Each square is black or white.
The color of the square is given as an H-by-W matrix (a_{i, j}).
If a_{i, j} is ., the square (i, j) is white; if a_{i, j} is #, the square (i, j) is black.

Snuke is compressing this grid.
He will do so by repeatedly performing the following operation while there is a row or column that consists only of white squares:

Operation: choose any one row or column that consists only of white squares, remove it and delete the space between the rows or columns.

It can be shown that the final state of the grid is uniquely determined regardless of what row or column is chosen in each operation.
Find the final state of the grid.

Constraints

1 \leq H, W \leq 100

a_{i, j} is . or #.

There is at least one black square in the whole grid.

Input

Input is given from Standard Input in the following format:

H W
a_{1, 1}...a_{1, W}
:
a_{H, 1}...a_{H, W}

Output

Print the final state of the grid in the same format as input (without the numbers of rows and columns); see the samples for clarity.

Sample Input 1

4 4
##.#
....
##.#
.#.#

Sample Output 1

###
###
.##

The second row and the third column in the original grid will be removed.

Sample Input 2

3 3
#..
.#.
..#

Sample Output 2

#..
.#.
..#

As there is no row or column that consists only of white squares, no operation will be performed.

Sample Input 3

4 5
.....
.....
..#..
.....

Sample Output 3

#

Sample Input 4

7 6
......
....#.
.#....
..#...
..#...
......
.#..#.

Sample Output 4

..#
#..
.#.
.#.
#.#


var
  n,m,i,j:longint;
  x,y:array[1..1000]of boolean;
  a:array[1..1000,1..1000]of char;
begin
  readln(n,m);
  for i:=1 to n do
  begin
    for j:=1 to m do
      read(a[i,j]);
    readln;
  end;
  for i:=1 to n do
  begin
    x[i]:=true;
    for j:=1 to m do
      if a[i,j]='#' then x[i]:=false;
  end;
  for j:=1 to m do
  begin
    y[j]:=true;
    for i:=1 to n do
      if a[i,j]='#' then y[j]:=false;
  end;
  for i:=1 to n do
  begin
    for j:=1 to m do
      if not(x[i] or y[j]) then write(a[i,j]);
    if not x[i] then writeln;
  end;
end.


uses
  SysUtils,
  Classes;

 function WhiteCheckFunc(const str : string) : Boolean;
	var
		i : Integer;
	begin
	Result := True;
	for i := 1 to Length(str)  do
		begin
		if str[i] = '#' then
			Result := False;
		end;
	end;
 procedure subProc(const h : Integer; const w : Integer);
	var
		i, j : Integer;
		post_height : Integer;
		str : string;
		line : string;
	begin
	line := '';
	post_height := h;
	for i := 0 to h - 1 do
		begin
		ReadLn(str);
		if WhiteCheckFunc(str) then
			post_height := post_height - 1
		else
			line := line + str;
		end;
	for i := 1 to w do
		begin
		str := '';
		for j := 0 to post_height - 1 do
			str := str + line[i + j * w];
		if WhiteCheckFunc(str) then
			begin
				for j := 0 to post_height - 1 do
					line[i + j * w] := '_';
			end;
		end;
	for i  := 0 to post_height - 1 do
		begin
		for j := 1 to w do
			if not (line[j + i * w] = '_') then
				Write(line[j + i * w]);
		Write(#13#10);
		end;
	end;

procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		heightNum : Integer;
		widthNum : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		heightNum := StrToInt(strlist.Strings[0]);
		widthNum := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
		subProc(heightNum, widthNum);
	end;

begin
MainProc;
end.


var
  a,d:array[0..100,0..100]of longint;
  b,c,e:array[0..100]of longint;
  n,m,i,j:longint;
  ch:char;
begin
  readln(n,m);
  for i:=1 to n do
    for j:=1 to m do
	  begin
	    read(ch);
		if ch='.' then
		  begin
		    a[i,j]:=0;
			{b[i]:=b[i-1]+0;
			c[j]:=c[j-1]+0;}
		  end else
		  begin
		    a[i,j]:=1;
			inc(b[i]);
			inc(c[j]);
		  end;
		 if j=m then readln;
	  end;
  for i:=1 to n do
    for j:=1 to m do
	  begin
	    if (b[i]=0)or(c[j]=0) then d[i,j]:=0 else d[i,j]:=1;
	  end;
  for i:=1 to n do
    begin
	  if b[i-1]>0 then writeln;
	  if b[i]=0 then continue;
	  for j:=1 to m do
	    begin
		  if c[j]=0 then continue;
		  if a[i,j]=0 then write('.') else write('#');
		end;
	end;
end.


var
  i,j,p,q,t:longint;
  s,st:array[1..100,1..100]of char;
begin
  readln(p,q);
  for i:=1 to p do begin
  for j:=1 to q do begin read(s[i,j]); if s[i,j]='#' then inc(t); end;
  readln;
  end;
  st:=s;
  for i:=1 to p do
  begin
     s[i,q+1]:=' ';
	 for j:=1 to q do if (s[i,j]<>s[i,j+1]) then break;
	 if (j=q)and(t>q) then for j:=1 to q do begin if (s[i,j]='#')and(st[i,j]<>' ') then dec(t); st[i,j]:=' '; end;
  end;
  for i:=1 to q do
  begin
     s[p+1,i]:=' ';
	 for j:=1 to p do if (s[j,i]<>s[j+1,i]) then break;
	 if (j=p)and(t>p) then for j:=1 to p do begin if (s[j,i]='#')and(st[j,i]<>' ') then dec(t); st[j,i]:=' '; end;
  end;
  for i:=1 to p do
  begin
	for j:=1 to q+1 do if st[i,j]<>' ' then break;
	if j=q+1 then continue;
    for j:=1 to q do if st[i,j]<>' ' then write(st[i,j]);
	writeln;
  end;
end.


var
  i,j,p,q:longint;
  s,st:array[1..100,1..100]of char;
function check:boolean;
var
  k,l:longint;
begin
  check:=false;
  for l:=1 to p do
  for k:=1 to q do if st[l,k]='#' then exit;
  check:=true;
end;
begin
  readln(p,q);
  for i:=1 to p do begin
  for j:=1 to q do read(s[i,j]);
  readln;
  end;
  st:=s;
  for i:=1 to p do
  begin
     if check then break;
     s[i,q+1]:=' ';
	 for j:=1 to q do if (s[i,j]<>s[i,j+1]) then break;
	 if j=q then for j:=1 to q do st[i,j]:=' ';
  end;
  for i:=1 to q do
  begin
     if check then break;
     s[p+1,i]:=' ';
	 for j:=1 to p do if (s[j,i]<>s[j+1,i]) then break;
	 if j=p then for j:=1 to p do st[j,i]:=' ';
  end;
  for i:=1 to p do
  begin
	for j:=1 to q+1 do if st[i,j]<>' ' then break;
	if j=q+1 then continue;
    for j:=1 to q do if st[i,j]<>' ' then write(st[i,j]);
	writeln;
  end;
end.


var
  i,j,p,q:longint;
  s,st:array[1..100,1..100]of char;
function check:boolean;
var
  k,l:longint;
begin
  check:=false;
  for l:=1 to p do
  for k:=1 to q do if st[l,k]='#' then exit;
  check:=true;
end;
begin
  readln(p,q);
  for i:=1 to p do
  for j:=1 to q do read(s[i,j]);
  st:=s;
  for i:=1 to p do
  begin
     if check then break;
     s[i,q+1]:=' ';
	 for j:=1 to q do if (s[i,j]<>s[i,j+1]) then break;
	 if j=q then for j:=1 to q do st[i,j]:=' ';
  end;
  for i:=1 to q do
  begin
     if check then break;
     s[p+1,i]:=' ';
	 for j:=1 to p do if (s[j,i]<>s[j+1,i]) then break;
	 if j=p then for j:=1 to p do st[j,i]:=' ';
  end;
  for i:=1 to p do
  begin
	for j:=1 to q+1 do if st[i,j]<>' ' then break;
	if j=q+1 then continue;
    for j:=1 to q do if st[i,j]<>' ' then write(st[i,j]);
	writeln;
  end;
end.


var
  a:array[0..100010]of int64;
  k,l,r,n,ans:int64;
  i:longint;

function min(x,y:int64):int64;
begin
  if x<y then exit(x);
  exit(y);
end;

function max(x,y:int64):int64;
begin
  if x>y then exit(x);
  exit(y);
end;

begin
  readln(n,k);
  for i:=1 to n do
    read(a[i]);
  ans:=maxlongint;
  for i:=1 to n-k+1 do
  begin
    l:=min(a[i],0);
    r:=max(a[i+k-1],0);
    ans:=min(ans,r-l*2);
    ans:=min(ans,r*2-l);
  end;
  writeln(ans);
end.

Score : 300 points

Problem Statement

There are N candles placed on a number line.
The i-th candle from the left is placed on coordinate x_i.
Here, x_1 < x_2 < ... < x_N holds.

Initially, no candles are burning.
Snuke decides to light K of the N candles.

Now, he is at coordinate 0.
He can move left and right along the line with speed 1.
He can also light a candle when he is at the same position as the candle, in negligible time.

Find the minimum time required to light K candles.

Constraints

1 \leq N \leq 10^5

1 \leq K \leq N

x_i is an integer.

|x_i| \leq 10^8

x_1 < x_2 < ... < x_N

Input

Input is given from Standard Input in the following format:

N K
x_1 x_2 ... x_N

Output

Print the minimum time required to light K candles.

Sample Input 1

5 3
-30 -10 10 20 50

Sample Output 1

40

He should move and light candles as follows:

Move from coordinate 0 to -10.

Light the second candle from the left.

Move from coordinate -10 to 10.

Light the third candle from the left.

Move from coordinate 10 to 20.

Light the fourth candle from the left.

Sample Input 2

3 2
10 20 30

Sample Output 2

20

Sample Input 3

1 1
0

Sample Output 3

0

There may be a candle placed at coordinate 0.

Sample Input 4

8 5
-9 -7 -4 -3 1 2 3 4

Sample Output 4

10


var
  n,k,ans,t:int64;
  sum,x:array[0..100000]of int64;
  i:longint;

function min(x,y:int64):int64;
begin
  if x<y then exit(x);
  exit(y);
end;

function max(x,y:int64):int64;
begin
  if x>y then exit(x);
  exit(y);
end;

begin
  readln(n,k);
  for i:=1 to n do
    read(x[i]);
  ans:=maxlongint;
  for i:=1 to n do
  begin
    if i+k-1>n then continue;
    if (x[i]+x[i+k-1])=((abs(x[i])+abs(x[i+k-1])))
      then ans:=min(ans,max(abs(x[i]),abs(x[i+k-1])))
      else ans:=min(ans,min(abs(x[i]),abs(x[i+k-1]))+abs(x[i])+abs(x[i+k-1]));
  end;
  writeln(ans);
end.


var n,k,i,aa:longint;
min,count:int64;
a:array[0..100010]of longint;
function m(x,y:int64):int64;
begin
	if x<y then exit(x)
		else exit(y);
end;
begin
	readln(n,k);
	aa:=0;
	min:=maxlongint;
	for i:=1 to n do
	begin
		read(a[i]);
		if a[i]<=0 then
		begin
			a[i]:=-a[i];aa:=i;
		end;
	end;
	if aa>=k then min:=m(min,a[aa-k+1]);
	if n-aa>=k then min:=m(min,a[aa+k]);
	for i:=0 to m(n-aa,k) do
		if (aa>=k-i) then
		begin
			if a[aa-k+i+1]>a[aa+i] then count:=a[aa-k+i+1]+2*a[aa+i]
				else count:=2*a[aa-k+i+1]+a[aa+i];
			min:=m(min,count);
		end;
	write(min);
end.


var
	a:array[0..100010]of longint;
	i,n,k,x,y:longint;
	s:int64;
function min(x,y:longint):longint;
begin
	if x<y then exit(x) else exit(y);
end;
begin
	readln(n,k);
	for i:=1 to n do read(a[i]);
	s:=maxlongint;
	for i:=1 to n-k+1 do
	begin
		x:=abs(a[i]);
		y:=abs(a[k+i-1]);
		if a[i]<=0 then
			if a[k+i-1]<=0 then s:=min(s,-a[i])
			else s:=min(s,min(x*2+y,y*2+x))
		else s:=min(s,a[k+i-1]);
	end;
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	read(n,k);
	x:=0; y:=0;
	t:=1; p:=0;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	if x=0 then
	begin
		writeln(-f[p-k+1]);
		halt;
	end;
	if y=0 then
	begin
		writeln(f[k]);
		halt;
	end;
	if p=k then
	begin
		if abs(f[1])<abs(f[p]) then writeln(abs(f[1])*2+abs(f[p]))
		else writeln(abs(f[p])*2+abs(f[1]));
		halt;
	end;
	if k=0 then
	begin
		writeln(0);
		halt;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=0-y;
	if x>=k then s:=min(s,f1[k-1]);
	if 0-y>=k then s:=min(s,f1[0-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k-1) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[0-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	read(n,k);
	x:=0; y:=0;
	t:=1; p:=0;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	if x=0 then
	begin
		writeln(-f[p-k+1]);
		halt;
	end;
	if y=0 then
	begin
		writeln(f[k]);
		halt;
	end;
	if p=k then
	begin
		if abs(f[1])<abs(f[p]) then writeln(abs(f[1])*2+abs(f[p]))
		else writeln(abs(f[p])*2+abs(f[1]));
		halt;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=0-y;
	if x>=k then s:=min(s,f1[k-1]);
	if 0-y>=k then s:=min(s,f1[0-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k-1) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[0-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	readln(n,k);
	x:=0; y:=0;
	t:=1; p:=1;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=-y;
	if x>=k then s:=min(s,f1[k-1]);
	if -y>=k then s:=min(s,f1[-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k-1) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t,a,p:longint;
	f,f1:array[-100010..100010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	readln(n,k);
	x:=0; y:=0;
	t:=1; p:=1;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(a);
		if a>0 then inc(x);
		if a<0 then inc(y);
		if a=0 then dec(k);
		if (a>0) and (f[i-1]<=0) then t:=i;
		if a<>0 then
		begin
			inc(p);
			f[p]:=a;
		end;
	end;
	s:=$7f;
	for i:=1 to p do f1[i-t]:=abs(f[i]);
	y:=-y;
	if x>=k then s:=min(s,f1[k-1]);
	if -y>=k then s:=min(s,f1[-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[-(k-i-1)]);
	writeln(s);
end.


var
	s:int64;
	n,k,i,x,y,t:longint;
	f,f1:array[-200010..200010] of longint;
function min(a,b:longint):longint;
begin
	if a<b then exit(a) else exit(b);
end;
begin
	readln(n,k);
	x:=0; y:=0;
	t:=1;
	fillchar(f1,sizeof(f1),$7f);
	for i:=1 to n do
	begin
		read(f[i]);
		if f[i]>0 then inc(x);
		if f[i]<0 then inc(y);
		if f[i]=0 then dec(k);
		if (f[i]>0) and (f[i-1]<=0) then t:=i;
	end;
	s:=$7f;
	for i:=1 to n do f1[i-t]:=abs(f[i]);
	y:=-y;
	if x>=k then s:=min(s,f1[k-1]);
	if -y>=k then s:=min(s,f1[-k]);
	for i:=-min(-y,k) to -1 do
		if i*(k+i-1)<=0 then
			s:=min(s,f1[i]*2+f1[k+i-1]);
	for i:=1 to min(x-1,k) do
		if i*(k-i-1)>=0 then
			s:=min(s,f1[i]*2+f1[-(k-i-1)]);
	writeln(s);
end.


var n,k,i,aa,x,count,min,bb:integer;
a,b:array[1..100000]of integer;
begin
	read(n,k);
	min:=maxint;
	i:=1;
	read(x);
	while x<0 do
	begin
		a[i]:=-x;
		inc(i);
		read(x);
	end;
	if x=0 then begin dec(k);dec(n);if k<>0 then read(x);end;
	aa:=i-1;bb:=n-aa;
	b[1]:=x;
	for i:=2 to n-aa do read(b[i]);
	for i:=0 to k do
		if (aa>=i) and (bb>=k-i) then
		begin
			if a[aa-i+1]>b[k-i] then count:=2*b[k-i]+a[aa-i+1]
				else count:=2*a[aa-i+1]+b[k-i];
		    if min>count then min:=count;
		end;
	write(min);
end.


var
  a,b:array[-100000..100000]of longint;
  i,n,k:longint;
  ans:qword;
Function min(x,y:longint):longint;
begin
  if x<y then exit(x) else exit(y);
end;
begin
  readln(n,k);
  for i:=1 to n do read(a[i]);
  ans:=maxlongint;
  for i:=1 to n-k+1 do
    begin
	  if a[i]<=0 then
	    begin
		  if a[i+k-1]<=0 then
		    ans:=min(ans,-a[i]) else
		  begin
		    ans:=min(ans,abs(a[i]-0)*2+abs(a[i+k-1]-0));
		    ans:=min(ans,abs(a[i]-0)+abs(a[i+k-1]-0)*2);
		  end;
		end else
		begin
		  ans:=min(ans,a[i+k-1]);
		end;
	end;
  write(ans);
end.


var
    n,k,i,x:longint;
    ans:int64;
    a:array[0..100010] of int64;
function min(a,b:int64):int64;
begin
    if a<b then exit(a) else exit(b);
end;
function max(a,b:int64):int64;
begin
    if a>b then exit(a) else exit(b);
end;
begin
    readln(n,k);
    for i:=1 to n do
        begin
            read(a[i]);
            if a[i]<=0 then x:=i;
        end;
    ans:=maxlongint;
    if x>=k then ans:=min(ans,abs(a[x-k+1]));
    if x+k<=n then ans:=min(ans,abs(a[x+k]));
    for i:=max(x,k)+1 to min(n,x+k)-1 do
        ans:=min(ans,min(abs(a[i]),abs(a[i-k+1]))*2+max(abs(a[i]),abs(a[i-k+1])));
    writeln(ans);
end.


VAR N,i,j,count,kq:INTEGER;
begin
 readln(n);
 i:=1;
 if n < 105 then writeln('0') else
 if n = 105 then writeln('1') else
 begin
   while i <= n do
    begin
    count:=0;
    for j:=1 to n do
     if i mod j = 0 then inc(Count);
    if count = 8 then inc(kq);
    inc(i,2);
    end;
  writeln(kq);
 end;
 end.

Score: 200 points

Problem Statement

The number 105 is quite special - it is odd but still it has eight divisors.
Now, your task is this: how many odd numbers with exactly eight positive divisors are there between 1 and N (inclusive)?

Constraints

N is an integer between 1 and 200 (inclusive).

Input

Input is given from Standard Input in the following format:

N

Output

Print the count.

Sample Input 1

105

Sample Output 1

1

Among the numbers between 1 and 105, the only number that is odd and has exactly eight divisors is 105.

Sample Input 2

7

Sample Output 2

0

1 has one divisor. 3, 5 and 7 are all prime and have two divisors. Thus, there is no number that satisfies the condition.


var
  i,j,s,n,ans:longint;
begin
  read(n);
  for i:=1 to n do
    if i mod 2=1 then
      begin
        s:=0;
        for j:=1 to i do
          if i mod j=0 then inc(s);
        if s=8 then inc(ans);
      end;
  write(ans);
end.


var
  a,f:array[0..1000,0..1000] of longint;
  n,m,q,i,j,k,x,y:longint;
begin
  readln(n,m,q);
  for i:=1 to m do
    begin
      readln(x,y);
      inc(a[x,y]);
    end;
  for i:=1 to n do
    for j:=i to n do
      begin
        f[i,j]:=f[i,j-1];
        for k:=i to j do
          inc(f[i,j],a[k,j]);
      end;
  for i:=1 to q do
    begin
      readln(x,y);
      writeln(f[x,y]);
    end;
end.

Score: 400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east.
A company called AtCoder Express possesses M trains, and the train i runs from City L_i to City R_i (it is possible that L_i = R_i).
Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City p_i to City q_i, that is, the number of trains j such that p_i \leq L_j and R_j \leq q_i.

Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.

Constraints

N is an integer between 1 and 500 (inclusive).

M is an integer between 1 and 200 \ 000 (inclusive).

Q is an integer between 1 and 100 \ 000 (inclusive).

1 \leq L_i \leq R_i \leq N (1 \leq i \leq M)

1 \leq p_i \leq q_i \leq N (1 \leq i \leq Q)

Input

Input is given from Standard Input in the following format:

N M Q
L_1 R_1
L_2 R_2
:
L_M R_M
p_1 q_1
p_2 q_2
:
p_Q q_Q

Output

Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p_i to City q_i.

Sample Input 1

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1

3

As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.

Sample Input 2

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2

1
1

The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1.
The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.

Sample Input 3

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

Sample Output 3

7
9
10
6
8
9
6
7
8
10


var n,x,y:integer;
m,q,i,j,count:longint;
a:array[1..200000,1..2]of integer;
begin
  read(n);
  read(m,q);
  for i:=1 to m do
    read(a[i,1],a[i,2]);
  for i:=1 to q do
  begin
    read(x,y);
    count:=0;
    for j:=1 to m do if (a[j,1]>=x) and (a[j,2]<=y) then inc(count)
      else if a[j,1]>y then break;
    writeln(count);
  end;
end.


uses crt;
type
        qd = record
           l,r:longint;
        end;
var
        a,b:array[1..500] of qd;
        i,j,n,m,q,sum:longint;
begin
        read(n,m,q);
        for i:=1 to m do
              readln(a[i].l, a[i].r);
        for i:=1 to q do
            begin
              readln(b[i].l, b[i].r);
              sum:=0;
                for j:=1 to m do
                if (a[j].l>=b[i].l) and  (a[j].r<=b[i].r)
                then inc(sum);
                writeln(sum);
              end;
      //  readln;
       // readln;
end.


uses crt;
type
        qd = record
           l,r:longint;
        end;
var
        a,b:array[1..500] of qd;
        i,j,n,m,q,sum:longint;
begin
        read(n,m,q);
        for i:=1 to m do
              readln(a[i].l, a[i].r);
        for i:=1 to q do
              readln(b[i].l, b[i].r);
        for i:=1 to q do
              begin
                sum:=0;
                for j:=1 to m do
                if (a[j].l>=b[i].l) and  (a[j].r<=b[i].r)
                then inc(sum);
                writeln(sum);
              end;
        readln;
        readln;
end.


var
	a:array[0..510,0..200000] of longint;
	i,j,k,s,n,m,q,x,y:longint;
begin
	readln(n,m,q);
	fillchar(a,sizeof(a),0);
	for i:=1 to m do
	begin
		read(x,y);
		inc(a[x,0]);
		a[x,a[x,0]]:=y;
	end;
	for i:=1 to q do
	begin
		s:=0;
		read(x,y);
		for k:=x to y do
			for j:=1 to a[k,0] do
				if a[k,j]<=y then inc(s);
		writeln(s);
	end;
end.


uses
  SysUtils,
  Classes;

 procedure MainProc;
	var
		text : string;
		strlist : TStringList;
		cracker : Integer;
		person : Integer;
	begin
	ReadLn(text);
	strlist := TStringList.Create;
	try
		strlist.Delimiter := ' ';
		strlist.DelimitedText := text;
		cracker := StrToInt(strlist.Strings[0]);
		person := StrToInt(strlist.Strings[1]);
		finally
			FreeAndNil(strlist);
			end;
	if (cracker mod person) = 0 then
		WriteLn(0)
	else
		WriteLn(1);
	end;

begin
MainProc;
end.

Score : 100 points

Problem Statement

Takahashi has decided to distribute N AtCoder Crackers to K users of as evenly as possible.
When all the crackers are distributed, find the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Constraints

1 \leq N,K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the minimum possible (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user.

Sample Input 1

7 3

Sample Output 1

1

When the users receive two, two and three crackers, respectively, the (absolute) difference between the largest number of crackers received by a user and the smallest number received by a user, is 1.

Sample Input 2

100 10

Sample Output 2

0

The crackers can be distributed evenly.

Sample Input 3

1 1

Sample Output 3

0


const
    key=100001623;
var
    n,i,j:longint;
    k,ans,m,t:int64;
    a,c,f:array[0..100010] of int64;
    b:array[0..100002000] of longint;
begin
    readln(n,m);j:=1;
    for i:=1 to n do
        begin
            read(a[j]);
            c[j]:=i;
            f[i]:=f[i-1]+a[j];
            k:=f[i] mod m mod key;
            if b[k]<>0 then dec(j);
            inc(b[k]);
            inc(j);
        end;
    for i:=1 to j-1 do
        begin
            k:=f[c[i]] mod m mod key;
            t:=b[k];
            if k=0 then ans:=ans+t;
            ans:=ans+t*(t-1) div 2;
        end;
    writeln(ans);
end.

Score : 400 points

Problem Statement

There are N boxes arranged in a row from left to right. The i-th box from the left contains A_i candies.

You will take out the candies from some consecutive boxes and distribute them evenly to M children.

Such being the case, find the number of the pairs (l, r) that satisfy the following:

l and r are both integers and satisfy 1 \leq l \leq r \leq N.

A_l + A_{l+1} + ... + A_r is a multiple of M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the number of the pairs (l, r) that satisfy the conditions.

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

Sample Input 1

3 2
4 1 5

Sample Output 1

3

The sum A_l + A_{l+1} + ... + A_r for each pair (l, r) is as follows:

Sum for (1, 1): 4

Sum for (1, 2): 5

Sum for (1, 3): 10

Sum for (2, 2): 1

Sum for (2, 3): 6

Sum for (3, 3): 5

Among these, three are multiples of 2.

Sample Input 2

13 17
29 7 5 7 9 51 7 13 8 55 42 9 81

Sample Output 2

6

Sample Input 3

10 400000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

25


const
    key=99999989;
var
    n,i,j:longint;
    k,ans,m,t:int64;
    a,c,f:array[0..100010] of int64;
    b:array[0..100000000] of longint;
begin
    readln(n,m);j:=1;
    for i:=1 to n do
        begin
            read(a[j]);
            c[j]:=i;
            f[i]:=f[i-1]+a[j];
            k:=f[i] mod m mod key;
            if b[k]<>0 then dec(j);
            inc(b[k]);
            inc(j);
        end;
    for i:=1 to j-1 do
        begin
            k:=f[c[i]] mod m mod key;
            t:=b[k];
            if k=0 then ans:=ans+t;
            ans:=ans+t*(t-1) div 2;
        end;
    writeln(ans);
end.


const
    key=99999989;
var
    n,m,i,j:longint;
    k,ans,t:int64;
    a,c,f:array[0..100010] of int64;
    b:array[0..100000000] of longint;
begin
    readln(n,m);j:=1;
    for i:=1 to n do
        begin
            read(a[j]);
            c[j]:=i;
            f[i]:=f[i-1]+a[j];
            k:=f[i] mod m mod key;
            if b[k]<>0 then dec(j);
            inc(b[k]);
            inc(j);
        end;
    for i:=1 to j-1 do
        begin
            k:=f[c[i]] mod m mod key;
            t:=b[k];
            if k=0 then ans:=ans+t;
            ans:=ans+t*(t-1) div 2;
        end;
    writeln(ans);
end.