Dynamic Programming
Dynamic Programming
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
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 :
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
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:
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
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
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
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
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
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
Sample Input 4
abracadabra
avadakedavra
Sample Output 4
aaadara
G - Longest Path
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
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
Sample Input 2
6 3
2 3
4 5
5 6
Sample Output 2
2
Sample Output 3
3
The red directed path in the following figure is one of the longest:
H - Grid 1
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 .
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
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
Sample Input 2
5 2
..
#.
..
.#
..
Sample Output 2
0
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
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
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:
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
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
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:
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
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
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
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):
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:
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
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:
Here, X = 5 + 1 + 9 = 15 and Y = 4 + 7 + 2 = 13 .
M - Candies
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
Sample Input 3
2 0
0 0
Sample Output 3
1
(0, 0)
Sample Input 4
4 100000
100000 100000 100000 100000
Sample Output 4
665683269
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.
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
Sample Input 2
5
10 10 10 10 10
Sample Output 2
120
Sample Input 3
3
1000000000 1000000000 1000000000
Sample Output 3
5000000000
Sample Input 4
6
7 6 8 6 1 1
Sample Output 4
68
(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
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):
Sample Input 2
4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0
Sample Output 2
1
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
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
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
Sample Input 2
4
1 2
1 3
1 4
Sample Output 2
9
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
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
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
Sample Input 3
5
1 2 3 4 5
1000000000 1000000000 1000000000 1000000000 1000000000
Sample Output 3
5000000000
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
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
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
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
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
Problem Statement
Find the number of integers between 1 and K (inclusive) sa sfying the following condi on, modulo
9
10 + 7 :
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
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
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
(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
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.
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
Sample Input 2
2
0 -10
-10 0
Sample Output 2
0
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
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
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
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).
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
Sample Input 2
3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30
Sample Output 2
90
Sample Input 3
1 1
1 1 -10
Sample Output 3
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
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
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
Sample Input 3
5
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
1 10000 1000000000
Sample Output 3
5000000000
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
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
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
Sample Input 2
5 2 2
2 1
4 2
Sample Output 2
0
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
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
Sample Input 2
2 1000000000000
500000 1000000
Sample Output 2
1250000000000
Sample Input 3
8 5
1 3 4 5 10 11 12 13
Sample Output 3
62