0% found this document useful (0 votes)
212 views

Dynamic Programming

The problem describes a grid with H rows and W columns. Each square in the grid is designated as either empty or blocked. The goal is to find the number of paths from the top left square (1,1) to the bottom right square (H,W) where each step can only move right or down through empty squares. Constraints and sample inputs/outputs are provided.

Uploaded by

Nikita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
212 views

Dynamic Programming

The problem describes a grid with H rows and W columns. Each square in the grid is designated as either empty or blocked. The goal is to find the number of paths from the top left square (1,1) to the bottom right square (H,W) where each step can only move right or down through empty squares. Constraints and sample inputs/outputs are provided.

Uploaded by

Nikita
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

A - Frog 1

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N stones, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), the height of Stone i is hi .

There is a frog who is ini ally on Stone 1. He will repeat the following ac on some number of mes to
reach Stone N :

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |hi − hj | is
incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N .

Constraints
All values in input are integers.
5
2 ≤ N ≤ 10
4
1 ≤ hi ≤ 10

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

h1 h2 … hN

Output
Print the minimum possible total cost incurred.

Sample Input 1
4
10 30 40 20
Sample Output 1
30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 − 30| + |30 − 20| = 30 .

Sample Input 2
2
10 10

Sample Output 2
0

If we follow the path 1 → 2, the total cost incurred would be |10 − 10| = 0 .

Sample Input 3
6
30 10 60 10 60 50

Sample Output 3
40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be


|30 − 60| + |60 − 60| + |60 − 50| = 40 .
B - Frog 2

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N stones, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), the height of Stone i is hi .

There is a frog who is ini ally on Stone 1. He will repeat the following ac on some number of mes to
reach Stone N :

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, … , i + K .


Here, a cost of |hi − hj | is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N .

Constraints
All values in input are integers.
5
2 ≤ N ≤ 10

1 ≤ K ≤ 100
4
1 ≤ hi ≤ 10

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

N K

h1 h2 … hN

Output
Print the minimum possible total cost incurred.

Sample Input 1
5 3
10 30 40 50 20
Sample Output 1
30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 − 30| + |30 − 20| = 30 .

Sample Input 2
3 1
10 20 10

Sample Output 2
20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 − 20| + |20 − 10| = 20 .

Sample Input 3
2 100
10 10

Sample Output 3
0

If we follow the path 1 → 2, the total cost incurred would be |10 − 10| = 0 .

Sample Input 4
10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4
40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be


|40 − 70| + |70 − 70| + |70 − 60| = 40 .
C - Vaca on

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
Taro's summer vaca on starts tomorrow, and he has decided to make plans for it now.

The vaca on consists of N days. For each i (1 ≤ i ≤ N ), Taro will choose one of the following ac vi es
and do it on the i-th day:

A: Swim in the sea. Gain ai points of happiness.


B: Catch bugs in the mountains. Gain bi points of happiness.
C: Do homework at home. Gain ci points of happiness.

As Taro gets bored easily, he cannot do the same ac vi es for two or more consecu ve days.

Find the maximum possible total points of happiness that Taro gains.

Constraints
All values in input are integers.
5
1 ≤ N ≤ 10
4
1 ≤ ai , bi , ci ≤ 10

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

a1 b1 c1

a2 b2 c2

aN bN cN

Output
Print the maximum possible total points of happiness that Taro gains.
Sample Input 1
3
10 40 70
20 50 80
30 60 90

Sample Output 1
210

If Taro does ac vi es in the order C, B, C, he will gain 70 + 50 + 90 = 210 points of happiness.

Sample Input 2
1
100 10 1

Sample Output 2
100

Sample Input 3
7
6 7 8
8 8 3
2 5 2
7 8 6
4 6 8
2 3 4
7 5 1

Sample Output 3
46

Taro should do ac vi es in the order C, A, B, A, C, B, A.


D - Knapsack 1

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N items, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), Item i has a weight of wi and a
value of vi .

Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of
the knapsack is W , which means that the sum of the weights of items taken must be at most W .

Find the maximum possible sum of the values of items that Taro takes home.

Constraints
All values in input are integers.
1 ≤ N ≤ 100
5
1 ≤ W ≤ 10

1 ≤ wi ≤ W
9
1 ≤ vi ≤ 10

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

N W

w1 v1

w2 v2

wN vN

Output
Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1
3 8
3 30
4 50
5 60
Sample Output 1
90

Items 1 and 3 should be taken. Then, the sum of the weights is 3 + 5 = 8 , and the sum of the values is
30 + 60 = 90 .

Sample Input 2
5 5
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 2
5000000000

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

Sample Input 3
6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3
17

Items 2, 4 and 5 should be taken. Then, the sum of the weights is 5 + 6 + 3 = 14 , and the sum of the
values is 6 + 6 + 5 = 17 .
E - Knapsack 2

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N items, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), Item i has a weight of wi and a
value of vi .

Taro has decided to choose some of the N items and carry them home in a knapsack. The capacity of
the knapsack is W , which means that the sum of the weights of items taken must be at most W .

Find the maximum possible sum of the values of items that Taro takes home.

Constraints
All values in input are integers.
1 ≤ N ≤ 100
9
1 ≤ W ≤ 10

1 ≤ wi ≤ W
3
1 ≤ vi ≤ 10

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

N W

w1 v1

w2 v2

wN vN

Output
Print the maximum possible sum of the values of items that Taro takes home.

Sample Input 1
3 8
3 30
4 50
5 60
Sample Output 1
90

Items 1 and 3 should be taken. Then, the sum of the weights is 3 + 5 = 8 , and the sum of the values is
30 + 60 = 90 .

Sample Input 2
1 1000000000
1000000000 10

Sample Output 2
10

Sample Input 3
6 15
6 5
5 6
6 4
6 6
3 5
7 2

Sample Output 3
17

Items 2, 4 and 5 should be taken. Then, the sum of the weights is 5 + 6 + 3 = 14 , and the sum of the
values is 6 + 6 + 5 = 17 .
F - LCS

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
You are given strings s and t. Find one longest string that is a subsequence of both s and t.

Notes
A subsequence of a string x is the string obtained by removing zero or more characters from x and
concatena ng the remaining characters without changing the order.

Constraints
s and t are strings consis ng of lowercase English le ers.
1 ≤ |s|, |t| ≤ 3000

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

Output
Print one longest string that is a subsequence of both s and t. If there are mul ple such strings, any of
them will be accepted.

Sample Input 1
axyb
abyxb

Sample Output 1
axb

The answer is ' axb ' or ' ayb '; either will be accepted.
Sample Input 2
aa
xayaz

Sample Output 2
aa

Sample Input 3
a
z

Sample Output 3

The answer is ' ' (an empty string).

Sample Input 4
abracadabra
avadakedavra

Sample Output 4
aaadara
G - Longest Path

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a directed graph G with N ver ces and M edges. The ver ces are numbered 1, 2, … , N , and
for each i (1 ≤ i ≤ M ), the i-th directed edge goes from Vertex xi to yi . G does not contain directed
cycles.

Find the length of the longest directed path in G. Here, the length of a directed path is the number of
edges in it.

Constraints
All values in input are integers.
5
2 ≤ N ≤ 10
5
1 ≤ M ≤ 10

1 ≤ xi , yi ≤ N

All pairs (xi , yi ) are dis nct.


G does not contain directed cycles.

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

N M

x1 y1

x2 y2

xM yM

Output
Print the length of the longest directed path in G.
Sample Input 1
4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1
3

The red directed path in the following figure is the longest:

Sample Input 2
6 3
2 3
4 5
5 6

Sample Output 2
2

The red directed path in the following figure is the longest:


Sample Input 3
5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3
3

The red directed path in the following figure is one of the longest:
H - Grid 1

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a grid with H horizontal rows and W ver cal columns. Let (i, j) denote the square at the i-th
row from the top and the j-th column from the le .

For each i and j (1 ,


≤ i ≤ H 1 ≤ j ≤ W ), Square (i, j) is described by a character ai,j . If ai,j is ' . ',
Square (i, j) is an empty square; if ai,j is ' # ', Square (i, j) is a wall square. It is guaranteed that
Squares (1, 1) and (H , W ) are empty squares.

Taro will start from Square (1, 1) and reach (H , W ) by repeatedly moving right or down to an adjacent
empty square.

Find the number of Taro's paths from Square (1, 1) to (H , W ). As the answer can be extremely large,
9
find the count modulo 10 + 7 .

Constraints
H and W are integers.
2 ≤ H , W ≤ 1000

ai,j is ' . ' or ' # '.


Squares (1, 1) and (H , W ) are empty squares.

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

H W

a1,1 …a1,W

aH ,1 …aH ,W

Output
Print the number of Taro's paths from Square (1, 1) to (H , W ), modulo 109 + 7 .
Sample Input 1
3 4
...#
.#..
....

Sample Output 1
3

There are three paths as follows:

Sample Input 2
5 2
..
#.
..
.#
..

Sample Output 2
0

There may be no paths.

Sample Input 3
5 5
..#..
.....
#...#
.....
..#..
Sample Output 3
24

Sample Input 4
20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

Sample Output 4
345263555

9
Be sure to print the count modulo 10 + 7 .
I - Coins

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
Let N be a posi ve odd number.

There are N coins, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), when Coin i is tossed, it comes up
heads with probability pi and tails with probability 1 − pi .

Taro has tossed all the N coins. Find the probability of having more heads than tails.

Constraints
N is an odd number.
1 ≤ N ≤ 2999

pi is a real number and has two decimal places.


0 < pi < 1

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

p1 p2 … pN

Output
Print the probability of having more heads than tails. The output is considered correct when the absolute
error is not greater than 10−9 .

Sample Input 1
3
0.30 0.60 0.80
Sample Output 1
0.612

The probability of each case where we have more heads than tails is as follows:

The probability of having (Coin1, Coin2, Coin3) = (H ead, H ead, H ead) is


0.3 × 0.6 × 0.8 = 0.144;

The probability of having (Coin1, Coin2, Coin3) = (T ail, H ead, H ead) is


0.7 × 0.6 × 0.8 = 0.336;

The probability of having (Coin1, Coin2, Coin3) = (H ead, T ail, H ead) is


0.3 × 0.4 × 0.8 = 0.096;

The probability of having (Coin1, Coin2, Coin3) = (H ead, H ead, T ail) is


0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612 .

Sample Input 2
1
0.50

Sample Output 2
0.5

Outputs such as ' 0.500 ', ' 0.500000001 ' and ' 0.499999999 ' are also considered correct.

Sample Input 3
5
0.42 0.01 0.42 0.99 0.42

Sample Output 3
0.3821815872
J - Sushi

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N dishes, numbered 1, 2, … , N . Ini ally, for each i (1 ≤ i ≤ N ), Dish i has ai (1 ≤ ai ≤ 3 )
pieces of sushi on it.

Taro will perform the following opera on repeatedly un l all the pieces of sushi are eaten:

Roll a die that shows the numbers 1, 2, … , N with equal probabili es, and let i be the outcome. If
there are some pieces of sushi on Dish i, eat one of them; if there is none, do nothing.

Find the expected number of mes the opera on is performed before all the pieces of sushi are eaten.

Constraints
All values in input are integers.
1 ≤ N ≤ 300

1 ≤ ai ≤ 3

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

a1 a2 … aN

Output
Print the expected number of mes the opera on is performed before all the pieces of sushi are eaten.
The output is considered correct when the rela ve difference is not greater than 10−9 .

Sample Input 1
3
1 1 1
Sample Output 1
5.5

The expected number of opera ons before the first piece of sushi is eaten, is 1. A er that, the expected
number of opera ons before the second sushi is eaten, is 1.5. A er that, the expected number of
opera ons before the third sushi is eaten, is 3. Thus, the expected total number of opera ons is
1 + 1.5 + 3 = 5.5 .

Sample Input 2
1
3

Sample Output 2
3

Outputs such as ' 3.00 ', ' 3.000000003 ' and ' 2.999999997 ' will also be accepted.

Sample Input 3
2
1 2

Sample Output 3
4.5

Sample Input 4
10
1 3 2 3 3 2 3 2 1 3

Sample Output 4
54.48064457488221
K - Stones

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a set A = {a1 , a2 , … , aN } consis ng of N posi ve integers. Taro and Jiro will play the
following game against each other.

Ini ally, we have a pile consis ng of K stones. The two players perform the following opera on
alternately, star ng from Taro:

Choose an element x in A, and remove exactly x stones from the pile.

A player loses when he becomes unable to play. Assuming that both players play op mally, determine
the winner.

Constraints
All values in input are integers.
1 ≤ N ≤ 100
5
1 ≤ K ≤ 10

1 ≤ a1 < a2 < ⋯ < aN ≤ K

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

N K

a1 a2 … aN

Output
If Taro will win, print ' First '; if Jiro will win, print ' Second '.

Sample Input 1
2 4
2 3
Sample Output 1
First

If Taro removes three stones, Jiro cannot make a move. Thus, Taro wins.

Sample Input 2
2 5
2 3

Sample Output 2
Second

Whatever Taro does in his opera on, Jiro wins, as follows:

If Taro removes two stones, Jiro can remove three stones to make Taro unable to make a move.
If Taro removes three stones, Jiro can remove two stones to make Taro unable to make a move.

Sample Input 3
2 7
2 3

Sample Output 3
First

Taro should remove two stones. Then, whatever Jiro does in his opera on, Taro wins, as follows:

If Jiro removes two stones, Taro can remove three stones to make Jiro unable to make a move.
If Jiro removes three stones, Taro can remove two stones to make Jiro unable to make a move.

Sample Input 4
3 20
1 2 3

Sample Output 4
Second
Sample Input 5
3 21
1 2 3

Sample Output 5
First

Sample Input 6
1 100000
1

Sample Output 6
Second
L - Deque

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
Taro and Jiro will play the following game against each other.

Ini ally, they are given a sequence a = (a1 , a2 , … , aN ) . Un l a becomes empty, the two players
perform the following opera on alternately, star ng from Taro:

Remove the element at the beginning or the end of a. The player earns x points, where x is the
removed element.

Let X and Y be Taro's and Jiro's total score at the end of the game, respec vely. Taro tries to maximize
X − Y , while Jiro tries to minimize X − Y .

Assuming that the two players play op mally, find the resul ng value of X − Y .

Constraints
All values in input are integers.
1 ≤ N ≤ 3000
9
1 ≤ ai ≤ 10

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

a1 a2 … aN

Output
Print the resul ng value of X − Y , assuming that the two players play op mally.

Sample Input 1
4
10 80 90 30
Sample Output 1
10

The game proceeds as follows when the two players play op mally (the element being removed is
wri en bold):

Taro: (10, 80, 90, 30) → (10, 80, 90)


Jiro: (10, 80, 90) → (10, 80)
Taro: (10, 80) → (10)
Jiro: (10) → ()

Here, X = 30 + 80 = 110 and Y = 90 + 10 = 100 .

Sample Input 2
3
10 100 10

Sample Output 2
-80

The game proceeds, for example, as follows when the two players play op mally:

Taro: (10, 100, 10) → (100, 10)


Jiro: (100, 10) → (10)
Taro: (10) → ()

Here, X = 10 + 10 = 20 and Y = 100 .

Sample Input 3
1
10

Sample Output 3
10
Sample Input 4
10
1000000000 1 1000000000 1 1000000000 1 1000000000 1 1000000000 1

Sample Output 4
4999999995

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

Sample Input 5
6
4 2 9 7 1 5

Sample Output 5
2

The game proceeds, for example, as follows when the two players play op mally:

Taro: (4, 2, 9, 7, 1, 5) → (4, 2, 9, 7, 1)


Jiro: (4, 2, 9, 7, 1) → (2, 9, 7, 1)
Taro: (2, 9, 7, 1) → (2, 9, 7)
Jiro: (2, 9, 7) → (2, 9)
Taro: (2, 9) → (2)
Jiro: (2) → ()

Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13 .
M - Candies

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N children, numbered 1, 2, … , N .

They have decided to share K candies among themselves. Here, for each i (1 ≤ i ≤ N ), Child i must
receive between 0 and ai candies (inclusive). Also, no candies should be le over.
9
Find the number of ways for them to share candies, modulo 10 + 7 . Here, two ways are said to be
different when there exists a child who receives a different number of candies.

Constraints
All values in input are integers.
1 ≤ N ≤ 100
5
0 ≤ K ≤ 10

0 ≤ ai ≤ K

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

N K

a1 a2 … aN

Output
9
Print the number of ways for the children to share candies, modulo 10 + 7 .

Sample Input 1
3 4
1 2 3
Sample Output 1
5

There are five ways for the children to share candies, as follows:

(0, 1, 3)

(0, 2, 2)

(1, 0, 3)

(1, 1, 2)

(1, 2, 1)

Here, in each sequence, the i-th element represents the number of candies that Child i receives.

Sample Input 2
1 10
9

Sample Output 2
0

There may be no ways for the children to share candies.

Sample Input 3
2 0
0 0

Sample Output 3
1

There is one way for the children to share candies, as follows:

(0, 0)

Sample Input 4
4 100000
100000 100000 100000 100000
Sample Output 4
665683269

Be sure to print the answer modulo 109 + 7 .


N - Slimes

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N slimes lining up in a row. Ini ally, the i-th slime from the le has a size of ai .

Taro is trying to combine all the slimes into a larger slime. He will perform the following opera on
repeatedly un l there is only one slime:

Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of
x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is

incurred. The posi onal rela onship of the slimes does not change while combining slimes.

Find the minimum possible total cost incurred.

Constraints
All values in input are integers.
2 ≤ N ≤ 400
9
1 ≤ ai ≤ 10

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

a1 a2 … aN

Output
Print the minimum possible total cost incurred.

Sample Input 1
4
10 20 30 40
Sample Output 1
190

Taro should do as follows (slimes being combined are shown in bold):

(10, 20, 30, 40) → (30, 30, 40)


(30, 30, 40) → (60, 40)
(60, 40) → (100)

Sample Input 2
5
10 10 10 10 10

Sample Output 2
120

Taro should do, for example, as follows:

(10, 10, 10, 10, 10) → (20, 10, 10, 10)


(20, 10, 10, 10) → (20, 20, 10)
(20, 20, 10) → (20, 30)
(20, 30) → (50)

Sample Input 3
3
1000000000 1000000000 1000000000

Sample Output 3
5000000000

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

Sample Input 4
6
7 6 8 6 1 1
Sample Output 4
68

Taro should do, for example, as follows:

(7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)
(7, 6, 8, 6, 2) → (7, 6, 8, 8)
(7, 6, 8, 8) → (13, 8, 8)
(13, 8, 8) → (13, 16)
(13, 16) → (29)
O - Matching

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N men and N women, both numbered 1, 2, … , N .

For each i, j (1 ≤ i, j ≤ N ), the compa bility of Man i and Woman j is given as an integer ai,j . If
ai,j = 1 , Man i and Woman j are compa ble; if ai,j = 0 , they are not.

Taro is trying to make N pairs, each consis ng of a man and a woman who are compa ble. Here, each
man and each woman must belong to exactly one pair.
9
Find the number of ways in which Taro can make N pairs, modulo 10 + 7 .

Constraints
All values in input are integers.
1 ≤ N ≤ 21

ai,j is 0 or 1.

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

a1,1 … a1,N

aN ,1 … aN ,N

Output
Print the number of ways in which Taro can make N pairs, modulo 109 + 7 .

Sample Input 1
3
0 1 1
1 0 1
1 1 1
Sample Output 1
3

There are three ways to make pairs, as follows ((i, j) denotes a pair of Man i and Woman j):

(1, 2), (2, 1), (3, 3)

(1, 2), (2, 3), (3, 1)

(1, 3), (2, 1), (3, 2)

Sample Input 2
4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0

Sample Output 2
1

There is one way to make pairs, as follows:

(1, 2), (2, 4), (3, 1), (4, 3)

Sample Input 3
1
0

Sample Output 3
0
Sample Input 4
21
0 0 0 0 0 0 0 1 1 0 1 1 1 1 0 0 0 1 0 0 1
1 1 1 0 0 1 0 0 0 1 0 0 0 0 1 1 1 0 1 1 0
0 0 1 1 1 1 0 1 1 0 0 1 0 0 1 1 0 0 0 1 1
0 1 1 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 1 1 0
1 1 0 0 1 0 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 0 1
0 1 0 0 0 1 0 1 0 0 0 1 1 1 0 0 1 1 0 1 0
0 0 0 0 1 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1
0 0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1
0 0 0 0 1 1 0 0 1 1 1 0 0 0 0 1 1 0 0 0 1
0 1 1 0 1 1 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0
0 0 1 0 0 1 1 1 1 0 1 1 0 1 1 1 0 0 0 0 1
0 1 1 0 0 1 1 1 1 0 0 0 1 0 1 1 0 1 0 1 1
1 1 1 1 1 0 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1
0 0 0 1 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 1
1 0 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 0 1 1 0
0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1
0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1
0 0 0 0 1 1 1 0 1 0 1 1 1 0 1 1 0 0 1 1 0
1 1 0 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 1 1 1 0 1 0 1 1 0 0 0 0 0

Sample Output 4
102515160

9
Be sure to print the number modulo 10 + 7 .
P - Independent Set

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a tree with N ver ces, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N − 1 ), the i-th edge
connects Vertex xi and yi .

Taro has decided to paint each vertex in white or black. Here, it is not allowed to paint two adjacent
ver ces both in black.
9
Find the number of ways in which the ver ces can be painted, modulo 10 + 7 .

Constraints
All values in input are integers.
5
1 ≤ N ≤ 10

1 ≤ xi , yi ≤ N

The given graph is a tree.

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

x1 y1

x2 y2

xN −1 yN −1

Output
9
Print the number of ways in which the ver ces can be painted, modulo 10 + 7 .

Sample Input 1
3
1 2
2 3
Sample Output 1
5

There are five ways to paint the ver ces, as follows:

Sample Input 2
4
1 2
1 3
1 4

Sample Output 2
9

There are nine ways to paint the ver ces, as follows:

Sample Input 3
1

Sample Output 3
2
Sample Input 4
10
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4
157
Q - Flowers

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N flowers arranged in a row. For each i (1 ≤ i ≤ N ), the height and the beauty of the i-th
flower from the le is hi and ai , respec vely. Here, h1 , h2 , … , hN are all dis nct.

Taro is pulling out some flowers so that the following condi on is met:

The heights of the remaining flowers are monotonically increasing from le to right.

Find the maximum possible sum of the beau es of the remaining flowers.

Constraints
All values in input are integers.
5
1 ≤ N ≤ 2 × 10

1 ≤ hi ≤ N

h1 , h2 , … , hN are all dis nct.


9
1 ≤ ai ≤ 10

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

h1 h2 … hN

a1 a2 … aN

Output
Print the maximum possible sum of the beau es of the remaining flowers.

Sample Input 1
4
3 1 4 2
10 20 30 40
Sample Output 1
60

We should keep the second and fourth flowers from the le . Then, the heights would be 1, 2 from le to
right, which is monotonically increasing, and the sum of the beau es would be 20 + 40 = 60 .

Sample Input 2
1
1
10

Sample Output 2
10

The condi on is met already at the beginning.

Sample Input 3
5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3
5000000000

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

Sample Input 4
9
4 2 5 8 3 6 1 7 9
6 8 8 4 6 3 5 7 5

Sample Output 4
31

We should keep the second, third, sixth, eighth and ninth flowers from the le .
R - Walk

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a simple directed graph G with N ver ces, numbered 1, 2, … , N .

For each i and j (1 ≤ i, j ≤ N ), you are given an integer ai,j that represents whether there is a directed
edge from Vertex i to j. If ai,j = 1 , there is a directed edge from Vertex i to j; if ai,j = 0 , there is not.
9
Find the number of different directed paths of length K in G, modulo 10 + 7 . We will also count a path
that traverses the same edge mul ple mes.

Constraints
All values in input are integers.
1 ≤ N ≤ 50
18
1 ≤ K ≤ 10

ai,j is 0 or 1.
ai,i = 0

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

N K

a1,1 … a1,N

aN ,1 … aN ,N

Output
9
Print the number of different directed paths of length K in G, modulo 10 + 7 .

Sample Input 1
4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0
Sample Output 1
6

G is drawn in the figure below:

There are six directed paths of length 2:

1 →2→3
1 → 2 → 4

2 → 3 → 4

2 → 4 → 1

3 → 4 → 1

4 → 1 → 2

Sample Input 2
3 3
0 1 0
1 0 1
0 0 0

Sample Output 2
3

G is drawn in the figure below:

There are three directed paths of length 3:

1 →2→1→2
2 → 1 → 2 → 1

2 → 1 → 2 → 3
Sample Input 3
6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

Sample Output 3
1

G is drawn in the figure below:

There is one directed path of length 2:

4 →5→6

Sample Input 4
1 1
0

Sample Output 4
0
Sample Input 5
10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

Sample Output 5
957538352

9
Be sure to print the count modulo 10 + 7 .
S - Digit Sum

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
Find the number of integers between 1 and K (inclusive) sa sfying the following condi on, modulo
9
10 + 7 :

The sum of the digits in base ten is a mul ple of D.

Constraints
All values in input are integers.
10000
1 ≤ K < 10

1 ≤ D ≤ 100

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

Output
Print the number of integers sa sfying the condi on, modulo 109 + 7 .

Sample Input 1
30
4

Sample Output 1
6

Those six integers are: 4, 8, 13, 17, 22 and 26.


Sample Input 2
1000000009
1

Sample Output 2
2

9
Be sure to print the number modulo 10 + 7 .

Sample Input 3
98765432109876543210
58

Sample Output 3
635270834
T - Permuta on

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
Let N be a posi ve integer. You are given a string s of length N − 1 , consis ng of ' < ' and ' > '.

Find the number of permuta ons (p1 , p2 , … , pN ) of (1, 2, … , N ) that sa sfy the following condi on,
9
modulo 10 + 7 :

For each i (1 ≤ i ≤ N − 1 ), pi < pi+1 if the i-th character in s is ' < ', and pi > pi+1 if the i-th
character in s is ' > '.

Constraints
N is an integer.
2 ≤ N ≤ 3000

s is a string of length N − 1.
s consists of ' < ' and ' > '.

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

Output
Print the number of permuta ons that sa sfy the condi on, modulo 109 + 7 .

Sample Input 1
4
<><
Sample Output 1
5

There are five permuta ons that sa sfy the condi on, as follows:

(1, 3, 2, 4)

(1, 4, 2, 3)

(2, 3, 1, 4)

(2, 4, 1, 3)

(3, 4, 1, 2)

Sample Input 2
5
<<<<

Sample Output 2
1

There is one permuta on that sa sfies the condi on, as follows:

(1, 2, 3, 4, 5)

Sample Input 3
20
>>>><>>><>><>>><<>>

Sample Output 3
217136290

9
Be sure to print the number modulo 10 + 7 .
U - Grouping

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N rabbits, numbered 1, 2, … , N .

For each i, j (1 ≤ i, j ≤ N ), the compa bility of Rabbit i and j is described by an integer ai,j . Here,
ai,i = 0 for each i (1 ≤ i ≤ N ), and ai,j = aj,i for each i and j (1 ≤ i, j ≤ N ).

Taro is dividing the N rabbits into some number of groups. Here, each rabbit must belong to exactly one
group. A er grouping, for each i and j (1 ≤ i < j ≤ N ), Taro earns ai,j points if Rabbit i and j belong
to the same group.

Find Taro's maximum possible total score.

Constraints
All values in input are integers.
1 ≤ N ≤ 16
9
|ai,j | ≤ 10

ai,i = 0

ai,j = aj,i

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

a1,1 … a1,N

aN ,1 … aN ,N

Output
Print Taro's maximum possible total score.
Sample Input 1
3
0 10 20
10 0 -100
20 -100 0

Sample Output 1
20

The rabbits should be divided as {1, 3}, {2}.

Sample Input 2
2
0 -10
-10 0

Sample Output 2
0

The rabbits should be divided as {1}, {2}.

Sample Input 3
4
0 1000000000 1000000000 1000000000
1000000000 0 1000000000 1000000000
1000000000 1000000000 0 -1
1000000000 1000000000 -1 0

Sample Output 3
4999999999

The rabbits should be divided as {1, 2, 3, 4}. Note that the answer may not fit into a 32-bit integer type.
Sample Input 4
16
0 5 -4 -5 -8 -4 7 2 -4 0 7 0 2 -3 7 7
5 0 8 -9 3 5 2 -7 2 -7 0 -1 -4 1 -1 9
-4 8 0 -9 8 9 3 1 4 9 6 6 -6 1 8 9
-5 -9 -9 0 -7 6 4 -1 9 -3 -5 0 1 2 -4 1
-8 3 8 -7 0 -5 -9 9 1 -9 -6 -3 -8 3 4 3
-4 5 9 6 -5 0 -6 1 -2 2 0 -5 -2 3 1 2
7 2 3 4 -9 -6 0 -2 -2 -9 -3 9 -2 9 2 -5
2 -7 1 -1 9 1 -2 0 -6 0 -6 6 4 -1 -7 8
-4 2 4 9 1 -2 -2 -6 0 8 -6 -2 -4 8 7 7
0 -7 9 -3 -9 2 -9 0 8 0 0 1 -3 3 -6 -6
7 0 6 -5 -6 0 -3 -6 -6 0 0 5 7 -1 -5 3
0 -1 6 0 -3 -5 9 6 -2 1 5 0 -2 7 -8 0
2 -4 -6 1 -8 -2 -2 4 -4 -3 7 -2 0 -9 7 1
-3 1 1 2 3 3 9 -1 8 3 -1 7 -9 0 -6 -8
7 -1 8 -4 4 1 2 -7 7 -6 -5 -8 7 -6 0 -9
7 9 9 1 3 2 -5 8 7 -6 3 0 1 -8 -9 0

Sample Output 4
132
V - Subtree

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a tree with N ver ces, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N − 1 ), the i-th edge
connects Vertex xi and yi .

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any
other black vertex by passing through only black ver ces.

You are given a posi ve integer M . For each v (1 ≤ v ≤ N ), answer the following ques on:

Assuming that Vertex v has to be black, find the number of ways in which the ver ces can be
painted, modulo M .

Constraints
All values in input are integers.
5
1 ≤ N ≤ 10
9
2 ≤ M ≤ 10

1 ≤ xi , yi ≤ N

The given graph is a tree.

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

N M

x1 y1

x2 y2

xN −1 yN −1

Output
Print N lines. The v-th (1 ≤ v ≤ N ) line should contain the answer to the following ques on:

Assuming that Vertex v has to be black, find the number of ways in which the ver ces can be
painted, modulo M .
Sample Input 1
3 100
1 2
2 3

Sample Output 1
3
4
3

There are seven ways to paint the ver ces, as shown in the figure below. Among them, there are three
ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3
is black.

Sample Input 2
4 100
1 2
1 3
1 4

Sample Output 2
8
5
5
5

Sample Input 3
1 100

Sample Output 3
1
Sample Input 4
10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4
0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M .


W - Intervals

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
Consider a string of length N consis ng of ' 0 ' and ' 1 '. The score for the string is calculated as
follows:

For each i (1 ≤ i ≤ M ), ai is added to the score if the string contains ' 1 ' at least once between
the li -th and ri -th characters (inclusive).

Find the maximum possible score of a string.

Constraints
All values in input are integers.
5
1 ≤ N ≤ 2 × 10
5
1 ≤ M ≤ 2 × 10

1 ≤ li ≤ r i ≤ N
9
|ai | ≤ 10

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

N M

l1 r1 a1

l2 r2 a2

lM rM aM

Output
Print the maximum possible score of a string.

Sample Input 1
5 3
1 3 10
2 4 -10
3 5 10
Sample Output 1
20

The score for ' 10001 ' is a1 + a3 = 10 + 10 = 20 .

Sample Input 2
3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30

Sample Output 2
90

The score for ' 100 ' is a1 + a2 = 100 + (−10) = 90 .

Sample Input 3
1 1
1 1 -10

Sample Output 3
0

The score for ' 0 ' is 0.

Sample Input 4
1 5
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
Sample Output 4
5000000000

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

Sample Input 5
6 8
5 5 3
1 1 10
1 6 -8
3 6 5
3 4 9
5 5 -2
1 3 -6
4 6 -7

Sample Output 5
10

For example, the score for ' 101000 ' is a2 + a3 + a4 + a5 + a7 = 10 + (−8) + 5 + 9 + (−6) = 10 .
X - Tower

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N blocks, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), Block i has a weight of wi , a
solidness of si and a value of vi .

Taro has decided to build a tower by choosing some of the N blocks and stacking them ver cally in
some order. Here, the tower must sa sfy the following condi on:

For each Block i contained in the tower, the sum of the weights of the blocks stacked above it is
not greater than si .

Find the maximum possible sum of the values of the blocks contained in the tower.

Constraints
All values in input are integers.
3
1 ≤ N ≤ 10
4
1 ≤ wi , si ≤ 10
9
1 ≤ vi ≤ 10

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

w1 s1 v1

w2 s2 v2

wN sN vN

Output
Print the maximum possible sum of the values of the blocks contained in the tower.
Sample Input 1
3
2 2 20
2 1 30
3 1 40

Sample Output 1
50

If Blocks 2, 1 are stacked in this order from top to bo om, this tower will sa sfy the condi on, with the
total value of 30 + 20 = 50 .

Sample Input 2
4
1 2 10
3 1 10
2 4 10
1 6 10

Sample Output 2
40

Blocks 1, 2, 3, 4 should be stacked in this order from top to bo om.

Sample Input 3
5
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000

Sample Output 3
5000000000

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


Sample Input 4
8
9 5 7
6 2 7
5 7 3
7 8 8
1 9 6
3 3 3
4 1 7
4 5 5

Sample Output 4
22

We should, for example, stack Blocks 5, 6, 8, 4 in this order from top to bo om.
Y - Grid 2

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There is a grid with H horizontal rows and W ver cal columns. Let (i, j) denote the square at the i-th
row from the top and the j-th column from the le .

In the grid, N Squares (r1 , c1 ), (r2 , c2 ), … , (rN , cN ) are wall squares, and the others are all empty
squares. It is guaranteed that Squares (1, 1) and (H , W ) are empty squares.

Taro will start from Square (1, 1) and reach (H , W ) by repeatedly moving right or down to an adjacent
empty square.
9
Find the number of Taro's paths from Square (1, 1) to (H , W ), modulo 10 + 7 .

Constraints
All values in input are integers.
5
2 ≤ H , W ≤ 10

1 ≤ N ≤ 3000

1 ≤ ri ≤ H

1 ≤ ci ≤ W

Squares (ri , ci ) are all dis nct.


Squares (1, 1) and (H , W ) are empty squares.

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

H W N

r1 c1

r2 c2

rN cN

Output
9
Print the number of Taro's paths from Square (1, 1) to (H , W ), modulo 10 + 7 .
Sample Input 1
3 4 2
2 2
1 4

Sample Output 1
3

There are three paths as follows:

Sample Input 2
5 2 2
2 1
4 2

Sample Output 2
0

There may be no paths.

Sample Input 3
5 5 4
3 1
3 5
1 3
5 3

Sample Output 3
24
Sample Input 4
100000 100000 1
50000 50000

Sample Output 4
123445622

9
Be sure to print the count modulo 10 + 7 .
Z - Frog 3

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 100 points

Problem Statement
There are N stones, numbered 1, 2, … , N . For each i (1 ≤ i ≤ N ), the height of Stone i is hi . Here,
h1 < h2 < ⋯ < hN holds.

There is a frog who is ini ally on Stone 1. He will repeat the following ac on some number of mes to
reach Stone N :

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, … , N . Here, a
cost of (hj − hi )2 + C is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N .

Constraints
All values in input are integers.
5
2 ≤ N ≤ 2 × 10
12
1 ≤ C ≤ 10
6
1 ≤ h1 < h2 < ⋯ < hN ≤ 10

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

N C

h1 h2 … hN

Output
Print the minimum possible total cost incurred.

Sample Input 1
5 6
1 2 3 4 5
Sample Output 1
20

If we follow the path 1 → 3 → 5, the total cost incurred would be


2 2
((3 − 1) + 6) + ((5 − 3) + 6) = 20 .

Sample Input 2
2 1000000000000
500000 1000000

Sample Output 2
1250000000000

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

Sample Input 3
8 5
1 3 4 5 10 11 12 13

Sample Output 3
62

If we follow the path 1 → 2 → 4 → 5 → 8, the total cost incurred would be


((3 − 1)
2
+ 5) + ((5 − 3)
2
+ 5) + ((10 − 5)
2
+ 5) + ((13 − 10)
2
+ 5) = 62 .

You might also like