C - Many Formulas

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

1 以上 9 以下の数字のみからなる文字列 S が与えられます。 この文字列の中で、あなたはこれら文字と文字の間のうち、いくつかの場所に + を入れることができます。 一つも入れなくてもかまいません。 ただし、+ が連続してはいけません。

このようにして出来る全ての文字列を数式とみなし、和を計算することができます。

ありうる全ての数式の値を計算し、その合計を出力してください。

制約

  • 1 \leq |S| \leq 10
  • S に含まれる文字は全て 19 の数字

入力

入力は以下の形式で標準入力から与えられる。

S

出力

ありうる全ての数式の値の総和を 1 行に出力せよ。


入力例 1

125

出力例 1

176

考えられる数式としては、 1251+2512+51+2+54 通りがあります。それぞれの数式を計算すると、

  • 125
  • 1+25=26
  • 12+5=17
  • 1+2+5=8

となり、これらの総和は 125+26+17+8=176 となります。


入力例 2

9999999999

出力例 2

12656242944

Score : 300 points

Problem Statement

You are given a string S consisting of digits between 1 and 9, inclusive. You can insert the letter + into some of the positions (possibly none) between two letters in this string. Here, + must not occur consecutively after insertion.

All strings that can be obtained in this way can be evaluated as formulas.

Evaluate all possible formulas, and print the sum of the results.

Constraints

  • 1 \leq |S| \leq 10
  • All letters in S are digits between 1 and 9, inclusive.

Input

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

S

Output

Print the sum of the evaluated value over all possible formulas.


Sample Input 1

125

Sample Output 1

176

There are 4 formulas that can be obtained: 125, 1+25, 12+5 and 1+2+5. When each formula is evaluated,

  • 125
  • 1+25=26
  • 12+5=17
  • 1+2+5=8

Thus, the sum is 125+26+17+8=176.


Sample Input 2

9999999999

Sample Output 2

12656242944
D - Snuke's Coloring

Time Limit: 3 sec / Memory Limit: 256 MB

配点 : 400

問題文

H 行、横 W 列のマス目からなる盤があります。最初、どのマス目も白く塗られています。

すぬけ君が、このうち N マスを黒く塗りつぶしました。i 回目 ( 1 \leq i \leq N ) に塗りつぶしたのは、 上から a_i 行目で左から b_i 列目のマスでした。

すぬけ君がマス目を塗りつぶした後の盤の状態について、以下のものの個数を計算してください。

  • 各整数 j ( 0 \leq j \leq 9 ) について、盤の中に完全に含まれるすべての 33 列の連続するマス目のうち、黒いマスがちょうど j 個あるもの。

制約

  • 3 \leq H \leq 10^9
  • 3 \leq W \leq 10^9
  • 0 \leq N \leq min(10^5,H×W)
  • 1 \leq a_i \leq H (1 \leq i \leq N)
  • 1 \leq b_i \leq W (1 \leq i \leq N)
  • (a_i, b_i) \neq (a_j, b_j) (i \neq j)

入力

入力は以下の形式で標準入力から与えられる。

H W N
a_1 b_1
:
a_N b_N

出力

出力は 10 行からなる。 j+1 行目 ( 0 \leq j \leq 9 ) には、盤の中に完全に含まれるすべての 33 列の連続するマス目のうち、黒いマスがちょうど j 個あるものの 総数を出力せよ。


入力例 1

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

出力例 1

0
0
0
2
4
0
0
0
0
0

この盤に含まれる 3×3 の正方形は全部で 6 個ありますが、これらのうち 2 個の内部には黒いマスが 3 個、残りの 4 個の内部には黒いマスが 4 個含まれています。


入力例 2

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

出力例 2

4
26
22
10
2
0
0
0
0
0

入力例 3

1000000000 1000000000 0

出力例 3

999999996000000004
0
0
0
0
0
0
0
0
0

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. At first, all cells were painted white.

Snuke painted N of these cells. The i-th ( 1 \leq i \leq N ) cell he painted is the cell at the a_i-th row and b_i-th column.

Compute the following:

  • For each integer j ( 0 \leq j \leq 9 ), how many subrectangles of size 3×3 of the grid contains exactly j black cells, after Snuke painted N cells?

Constraints

  • 3 \leq H \leq 10^9
  • 3 \leq W \leq 10^9
  • 0 \leq N \leq min(10^5,H×W)
  • 1 \leq a_i \leq H (1 \leq i \leq N)
  • 1 \leq b_i \leq W (1 \leq i \leq N)
  • (a_i, b_i) \neq (a_j, b_j) (i \neq j)

Input

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

H W N
a_1 b_1
:
a_N b_N

Output

Print 10 lines. The (j+1)-th ( 0 \leq j \leq 9 ) line should contain the number of the subrectangles of size 3×3 of the grid that contains exactly j black cells.


Sample Input 1

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

Sample Output 1

0
0
0
2
4
0
0
0
0
0

There are six subrectangles of size 3×3. Two of them contain three black cells each, and the remaining four contain four black cells each.


Sample Input 2

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

Sample Output 2

4
26
22
10
2
0
0
0
0
0

Sample Input 3

1000000000 1000000000 0

Sample Output 3

999999996000000004
0
0
0
0
0
0
0
0
0
E - Snuke's Subway Trip

Time Limit: 3 sec / Memory Limit: 256 MB

配点 : 600

問題文

すぬけ君の住んでいる街には地下鉄が走っています。駅は全部で N 個あり、路線は全部で M 本あります。 駅には 1 から N までの整数が付けられています。また、それぞれの路線はある 1 つの会社によって運営されており、 それぞれの会社には会社をあらわす整数がつけられています。

i 番目 ( 1 \leq i \leq M ) の路線は、駅 p_i と 駅 q_i を相互に結んでいます。途中に他の駅はありません。 また、この路線は会社 c_i によって運営されています。 同じ駅を通る路線が複数あるときは、その駅で乗り換えることができます。

それぞれの会社について、同じ会社の路線を使い続ける限り料金は 1 ですが、別の会社の路線に乗り換えるたびに新たに料金が 1 かかります。 ある会社を利用し、別の会社を利用してからまた最初の会社を利用する場合でも、再び料金を払う必要があります。

すぬけ君は、駅 1 を出発し、地下鉄を利用して駅 N に行きたいです。移動にかかる料金の最小値を求めてください。

制約

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 2×10^5
  • 1 \leq p_i \leq N (1 \leq i \leq M)
  • 1 \leq q_i \leq N (1 \leq i \leq M)
  • 1 \leq c_i \leq 10^6 (1 \leq i \leq M)
  • p_i \neq q_i (1 \leq i \leq M)

入力

入力は以下の形式で標準入力から与えられる。

N M
p_1 q_1 c_1
:
p_M q_M c_M

出力

移動にかかる料金の最小値を出力せよ。すぬけ君が駅 N に到達することが不可能な場合には、代わりに -1 を出力せよ。


入力例 1

3 3
1 2 1
2 3 1
3 1 2

出力例 1

1

123 と会社 1 の路線を使って移動することができ、この場合必要なコストは 1 です。


入力例 2

8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

出力例 2

2

13256 と会社 1 の路線を利用し、その後 678 と会社 5 の路線を利用することで、コスト 2 で目的地に到達できます。


入力例 3

2 0

出力例 3

-1

Score : 600 points

Problem Statement

Snuke's town has a subway system, consisting of N stations and M railway lines. The stations are numbered 1 through N. Each line is operated by a company. Each company has an identification number.

The i-th ( 1 \leq i \leq M ) line connects station p_i and q_i bidirectionally. There is no intermediate station. This line is operated by company c_i.

You can change trains at a station where multiple lines are available.

The fare system used in this subway system is a bit strange. When a passenger only uses lines that are operated by the same company, the fare is 1 yen (the currency of Japan). Whenever a passenger changes to a line that is operated by a different company from the current line, the passenger is charged an additional fare of 1 yen. In a case where a passenger who changed from some company A's line to another company's line changes to company A's line again, the additional fare is incurred again.

Snuke is now at station 1 and wants to travel to station N by subway. Find the minimum required fare.

Constraints

  • 2 \leq N \leq 10^5
  • 0 \leq M \leq 2×10^5
  • 1 \leq p_i \leq N (1 \leq i \leq M)
  • 1 \leq q_i \leq N (1 \leq i \leq M)
  • 1 \leq c_i \leq 10^6 (1 \leq i \leq M)
  • p_i \neq q_i (1 \leq i \leq M)

Input

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

N M
p_1 q_1 c_1
:
p_M q_M c_M

Output

Print the minimum required fare. If it is impossible to get to station N by subway, print -1 instead.


Sample Input 1

3 3
1 2 1
2 3 1
3 1 2

Sample Output 1

1

Use company 1's lines: 123. The fare is 1 yen.


Sample Input 2

8 11
1 3 1
1 4 2
2 3 1
2 5 1
3 4 3
3 6 3
3 7 3
4 8 4
5 6 1
6 7 5
7 8 5

Sample Output 2

2

First, use company 1's lines: 13256. Then, use company 5's lines: 678. The fare is 2 yen.


Sample Input 3

2 0

Sample Output 3

-1
F - Card Game for Three

Time Limit: 3 sec / Memory Limit: 256 MB

配点 : 1100

問題文

A さん、B さん、C さんの 3 人が以下のようなカードゲームをプレイしています。

  • 最初、3 人はそれぞれ abc いずれかの文字が書かれたカードを、A さんは N 枚、B さんは M 枚、C さんは K 枚持っている。3 人は、持っているカードを並べ替えることはできない。
  • A さんのターンから始まる。
  • 現在自分のターンである人がカードを 1 枚以上持っているならば、そのうち先頭のカードを捨てる。その後、捨てられたカードに書かれているアルファベットと同じ名前の人 (例えば、カードに a と書かれていたならば A さん) のターンとなる。
  • 現在自分のターンである人がカードを 1 枚も持っていないならば、その人がゲームの勝者となり、ゲームは終了する。

3 人が最初に配られるカードに書いてある文字の並びは、全部で 3^{N+M+K} 通りの組み合わせがあります。このうち、A さんが勝者となってゲームが終了するのが何通りあるかを求めてください。

答えは大きくなる可能性があるので、1\,000\,000\,007 (=10^9+7) で割った余りを出力してください。

制約

  • 1 \leq N \leq 3×10^5
  • 1 \leq M \leq 3×10^5
  • 1 \leq K \leq 3×10^5

部分点

  • 1 \leq N \leq 10001 \leq M \leq 10001 \leq K \leq 1000 を満たすデータセットに正解した場合は、 500 点が与えられる。

入力

入力は以下の形式で標準入力から与えられる。

N M K

出力

答えを 1\,000\,000\,007 (=10^9+7) で割った余りを出力せよ。


入力例 1

1 1 1

出力例 1

17
  • A さんが a を持っている場合は、他の 2 人の持っているカードに関わらず A さんが勝ちます。これは 3×3=9 通りあります。
  • A さんが b を持っている場合は、B さんが a を持っているか、 B さんが c を持っていてかつ C さんが a を持っている場合に限り A さんが勝ちます。これは 3+1=4 通りあります。
  • A さんが c を持っている場合は、C さんが a を持っているか、 C さんが b を持っていてかつ B さんが a を持っている場合に限り A さんが勝ちます。これは 3+1=4 通りあります。

合計すると、 9+4+4=17 通りとなります。


入力例 2

4 2 2

出力例 2

1227

入力例 3

1000 1000 1000

出力例 3

261790852

Score : 1100 points

Problem Statement

Alice, Bob and Charlie are playing Card Game for Three, as below:

  • At first, each of the three players has a deck consisting of some number of cards. Alice's deck has N cards, Bob's deck has M cards, and Charlie's deck has K cards. Each card has a letter a, b or c written on it. The orders of the cards in the decks cannot be rearranged.
  • The players take turns. Alice goes first.
  • If the current player's deck contains at least one card, discard the top card in the deck. Then, the player whose name begins with the letter on the discarded card, takes the next turn. (For example, if the card says a, Alice takes the next turn.)
  • If the current player's deck is empty, the game ends and the current player wins the game.

There are 3^{N+M+K} possible patters of the three player's initial decks. Among these patterns, how many will lead to Alice's victory?

Since the answer can be large, print the count modulo 1\,000\,000\,007 (=10^9+7).

Constraints

  • 1 \leq N \leq 3×10^5
  • 1 \leq M \leq 3×10^5
  • 1 \leq K \leq 3×10^5

Partial Scores

  • 500 points will be awarded for passing the test set satisfying the following: 1 \leq N \leq 1000, 1 \leq M \leq 1000, 1 \leq K \leq 1000.

Input

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

N M K

Output

Print the answer modulo 1\,000\,000\,007 (=10^9+7).


Sample Input 1

1 1 1

Sample Output 1

17
  • If Alice's card is a, then Alice will win regardless of Bob's and Charlie's card. There are 3×3=9 such patterns.
  • If Alice's card is b, Alice will only win when Bob's card is a, or when Bob's card is c and Charlie's card is a. There are 3+1=4 such patterns.
  • If Alice's card is c, Alice will only win when Charlie's card is a, or when Charlie's card is b and Bob's card is a. There are 3+1=4 such patterns.

Thus, there are total of 9+4+4=17 patterns that will lead to Alice's victory.


Sample Input 2

4 2 2

Sample Output 2

1227

Sample Input 3

1000 1000 1000

Sample Output 3

261790852