Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
N 個の袋があります。
袋 i には L_i 個のボールが入っていて、袋 i の j(1\leq j\leq L_i) 番目のボールには正の整数 a_{i,j} が書かれています。
それぞれの袋から 1 つずつボールを取り出します。
取り出したボールに書かれた数の総積が X になるような取り出し方は何通りありますか?
ただし、書かれた数が同じであっても全てのボールは区別します。
制約
- N \geq 2
- L_i \geq 2
- 袋に入っているボールの個数の総積は 10^5 を超えない。すなわち、\displaystyle\prod_{i=1}^{N}L_i \leq 10^5
- 1 \leq a_{i,j} \leq 10^9
- 1 \leq X \leq 10^{18}
- 入力に含まれる値は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}
出力
答えを出力せよ。
入力例 1
2 40 3 1 8 4 2 10 5
出力例 1
2
袋 1 の 3 番目のボールと袋 2 の 1 番目のボールを選ぶと、a_{1,3} \times a_{2,1} = 4 \times 10 = 40 となります。
袋 1 の 2 番目のボールと袋 2 の 2 番目のボールを選ぶと、a_{1,2} \times a_{2,2} = 8 \times 5 = 40 となります。
これ以外に総積が 40 になる取り出し方は存在しないので、答えは 2 です。
入力例 2
3 200 3 10 10 10 3 10 10 10 5 2 2 2 2 2
出力例 2
45
書かれた数が同じであっても全てのボールは区別することに注意してください。
入力例 3
3 1000000000000000000 2 1000000000 1000000000 2 1000000000 1000000000 2 1000000000 1000000000
出力例 3
0
総積が X になる取り出し方が 1 つも存在しないこともあります。
Score : 300 points
Problem Statement
We have N bags.
Bag i contains L_i balls. The j-th ball (1\leq j\leq L_i) in Bag i has a positive integer a_{i,j} written on it.
We will pick out one ball from each bag.
How many ways are there to pick the balls so that the product of the numbers written on the picked balls is X?
Here, we distinguish all balls, even with the same numbers written on them.
Constraints
- N \geq 2
- L_i \geq 2
- The product of the numbers of balls in the bags is at most 10^5: \displaystyle\prod_{i=1}^{N}L_i \leq 10^5.
- 1 \leq a_{i,j} \leq 10^9
- 1 \leq X \leq 10^{18}
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N X
L_1 a_{1,1} a_{1,2} \ldots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \ldots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \ldots a_{N,L_N}
Output
Print the answer.
Sample Input 1
2 40 3 1 8 4 2 10 5
Sample Output 1
2
When choosing the 3-rd ball in Bag 1 and 1-st ball in Bag 2, we have a_{1,3} \times a_{2,1} = 4 \times 10 = 40.
When choosing the 2-nd ball in Bag 1 and 2-nd ball in Bag 2, we have a_{1,2} \times a_{2,2} = 8 \times 5 = 40.
There are no other ways to make the product 40, so the answer is 2.
Sample Input 2
3 200 3 10 10 10 3 10 10 10 5 2 2 2 2 2
Sample Output 2
45
Note that we distinguish all balls, even with the same numbers written on them.
Sample Input 3
3 1000000000000000000 2 1000000000 1000000000 2 1000000000 1000000000 2 1000000000 1000000000
Sample Output 3
0
There may be no way to make the product X.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 350 点
問題文
正の整数 X は、次の条件をみたすときかつその時に限り、良い整数と呼ばれます。
- 正の整数の組 (a,b) を用いて、X=2^a\times b^2 と書ける。
例えば、400 は 400=2^2\times 10^2 と書けるため、良い整数です。
正の整数 N が与えられるので、1 以上 N 以下の良い整数の個数を求めてください。
制約
- 1 \leq N \leq 10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
1 以上 N 以下の良い整数の個数を出力せよ。
入力例 1
20
出力例 1
5
1 以上 20 以下の良い整数は 2,4,8,16,18 の 5 つです。
よって、5 を出力します。
入力例 2
400
出力例 2
24
入力例 3
1234567890
出力例 3
42413
入力が 32bit 整数型に収まるとは限らないことに注意してください。
Score : 350 points
Problem Statement
A positive integer X is called a good integer if and only if it satisfies the following condition:
- There exists a pair of positive integers (a,b) such that X = 2^a \times b^2.
For example, 400 is a good integer because 400 = 2^2 \times 10^2.
Given a positive integer N, find the number of good integers between 1 and N, inclusive.
Constraints
- 1 \leq N \leq 10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the number of good integers between 1 and N, inclusive.
Sample Input 1
20
Sample Output 1
5
There are five good integers between 1 and 20: 2, 4, 8, 16, and 18.
Thus, print 5.
Sample Input 2
400
Sample Output 2
24
Sample Input 3
1234567890
Sample Output 3
42413
Note that the input might not fit in a 32-bit integer type.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
正整数列 A=(a_1,a_2,\ldots,a_N) が与えられます。
あなたは以下の操作のうち 1 つを選んで行うことを 0 回以上何度でも繰り返せます。
- 1 \leq i \leq N かつ a_i が 2 の倍数であるような整数 i を選び、a_i を \frac{a_i}{2} に置き換える
- 1 \leq i \leq N かつ a_i が 3 の倍数であるような整数 i を選び、a_i を \frac{a_i}{3} に置き換える
あなたの目標は A が a_1=a_2=\ldots=a_N を満たす状態にすることです。
目標を達成するために必要な操作の回数の最小値を求めてください。ただし、どのように操作を行っても目標を達成できない場合、代わりに -1 と出力してください。
制約
- 2 \leq N \leq 1000
- 1 \leq a_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 a_2 \ldots a_N
出力
答えを出力せよ。
入力例 1
3 1 4 3
出力例 1
3
次のように操作をすると 3 回で目標を達成でき、これが最小の回数です。
- a_i が 2 の倍数であるような整数 i として 2 を選び、a_2 を \frac{a_2}{2} に置き換える。A は (1,2,3) となる。
- a_i が 2 の倍数であるような整数 i として 2 を選び、a_2 を \frac{a_2}{2} に置き換える。A は (1,1,3) となる。
- a_i が 3 の倍数であるような整数 i として 3 を選び、a_3 を \frac{a_3}{3} に置き換える。A は (1,1,1) となる。
入力例 2
3 2 7 6
出力例 2
-1
どのように操作を行っても目標を達成できません。
入力例 3
6 1 1 1 1 1 1
出力例 3
0
Score : 400 points
Problem Statement
You are given a sequence of positive integers: A=(a_1,a_2,\ldots,a_N).
You can choose and perform one of the following operations any number of times, possibly zero.
- Choose an integer i such that 1 \leq i \leq N and a_i is a multiple of 2, and replace a_i with \frac{a_i}{2}.
- Choose an integer i such that 1 \leq i \leq N and a_i is a multiple of 3, and replace a_i with \frac{a_i}{3}.
Your objective is to make A satisfy a_1=a_2=\ldots=a_N.
Find the minimum total number of times you need to perform an operation to achieve the objective. If there is no way to achieve the objective, print -1 instead.
Constraints
- 2 \leq N \leq 1000
- 1 \leq a_i \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N a_1 a_2 \ldots a_N
Output
Print the answer.
Sample Input 1
3 1 4 3
Sample Output 1
3
Here is a way to achieve the objective in three operations, which is the minimum needed.
- Choose an integer i=2 such that a_i is a multiple of 2, and replace a_2 with \frac{a_2}{2}. A becomes (1,2,3).
- Choose an integer i=2 such that a_i is a multiple of 2, and replace a_2 with \frac{a_2}{2}. A becomes (1,1,3).
- Choose an integer i=3 such that a_i is a multiple of 3, and replace a_3 with \frac{a_3}{3}. A becomes (1,1,1).
Sample Input 2
3 2 7 6
Sample Output 2
-1
There is no way to achieve the objective.
Sample Input 3
6 1 1 1 1 1 1
Sample Output 3
0
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
N 頂点 M 辺の単純連結無向グラフ G が与えられます。
G の頂点は頂点 1, 頂点 2, \ldots, 頂点 N と番号付けられており、
i (1\leq i\leq M) 本目の辺は頂点 U_i と頂点 V_i を結んでいます。
G における頂点 X から頂点 Y への単純パスのうち辞書順最小のものを求めてください。
すなわち、以下の条件をみたす整数列 P=(P_1,P_2,\ldots,P_{\lvert P\rvert}) の中で辞書順最小のものを求めてください。
- 1\leq P_i\leq N
- i\neq j ならば P_i\neq P_j
- P_1=X かつ P_{\lvert P\rvert}=Y
- 1\leq i\leq \lvert P\rvert-1 について、頂点 P_i と頂点 P_{i+1} を結ぶ辺が存在する。
なお、本問題の制約下で条件をみたすようなものが必ず存在することが証明できます。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
整数列の辞書順 とは
整数列 S=(S_1,S_2,\ldots,S_{\lvert S\rvert}) が整数列 T=(T_1,T_2,\ldots,T_{\lvert T\rvert}) より辞書順で小さいとは、下記の 1. と 2. のどちらかが成り立つことを言います。 ここで、\lvert S\rvert, \lvert T\rvert はそれぞれ S,T の長さを表します。- \lvert S\rvert<\lvert T\rvert かつ (S_1,S_2,\ldots,S_{\lvert S\rvert})=(T_1,T_2,\ldots,T_{\lvert S\rvert})。
- ある 1\leq i\leq \min(\lvert S\rvert,\lvert T\rvert) が存在して (S_1,S_2,\ldots,S_{i-1})=(T_1,T_2,\ldots,T_{i-1}) かつ S_i< T_i。
制約
- 1\leq T\leq 500
- 2\leq N\leq 1000
- N-1\leq M\leq \min\left( \frac{N(N-1)}{2},5\times 10^4\right)
- 1\leq X,Y \leq N
- X\neq Y
- 1\leq U_i<V_i \leq N
- i\neq j ならば (U_i,V_i)\neq (U_j,V_j)
- 与えられるグラフは連結である。
- 1 つの入力における N の総和は 1000 以下である。
- 1 つの入力における M の総和は 5\times 10^4 以下である。
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i は i 番目のテストケースを表す。 各テストケースは以下の形式で与えられる。
N M X Y U_1 V_1 U_2 V_2 \vdots U_M V_M
出力
T 行出力せよ。
i 行目 (1\leq i\leq T) には、i 個目のテストケースの答えとなる単純パス上の頂点番号を、順に空白区切りで出力せよ。
すなわち i 個目のテストケースに対する答えが P=(P_1,P_2,\ldots,P_{\lvert P\rvert}) であるとき、
P_1, P_2, \ldots, P_{\lvert P\rvert} を i 行目にこの順に空白区切りで出力せよ。
入力例 1
2 6 10 3 5 1 2 1 3 1 5 1 6 2 4 2 5 2 6 3 4 3 5 5 6 3 2 3 2 1 3 2 3
出力例 1
3 1 2 5 3 2
1 つめのテストケースについて、グラフ G は次のようになっています。

G 上の頂点 3 から頂点 5 への単純パスを辞書順に列挙すると、次のとおりになります。
- (3,1,2,5)
- (3,1,2,6,5)
- (3,1,5)
- (3,1,6,2,5)
- (3,1,6,5)
- (3,4,2,1,5)
- (3,4,2,1,6,5)
- (3,4,2,5)
- (3,4,2,6,1,5)
- (3,4,2,6,5)
- (3,5)
このうち、辞書順最小のものは (3,1,2,5) であるため、1 行目には 3,1,2,5 を空白区切りで出力します。
2 つめのテストケースにおいては、(3,2) が頂点 3 から頂点 2 への唯一の単純パスです。
Score : 475 points
Problem Statement
You are given a simple connected undirected graph G with N vertices and M edges.
The vertices of G are numbered vertex 1, vertex 2, \ldots, vertex N, and
the i-th (1\leq i\leq M) edge connects vertices U_i and V_i.
Find the lexicographically smallest simple path from vertex X to vertex Y in G.
That is, find the lexicographically smallest among the integer sequences P=(P_1,P_2,\ldots,P_{\lvert P\rvert}) that satisfy the following conditions:
- 1\leq P_i\leq N
- If i\neq j, then P_i\neq P_j.
- P_1=X and P_{\lvert P\rvert}=Y.
- For 1\leq i\leq \lvert P\rvert-1, there exists an edge connecting vertices P_i and P_{i+1}.
One can prove that such a path always exists under the constraints of this problem.
You are given T test cases, so find the answer for each.
Lexicographic order on integer sequences
An integer sequence S=(S_1,S_2,\ldots,S_{\lvert S\rvert}) is lexicographically smaller than an integer sequence T=(T_1,T_2,\ldots,T_{\lvert T\rvert}) if either of the following 1. or 2. holds. Here, \lvert S\rvert and \lvert T\rvert represent the lengths of S and T, respectively.- \lvert S\rvert<\lvert T\rvert and (S_1,S_2,\ldots,S_{\lvert S\rvert})=(T_1,T_2,\ldots,T_{\lvert S\rvert}).
- There exists some 1\leq i\leq \min(\lvert S\rvert,\lvert T\rvert) such that (S_1,S_2,\ldots,S_{i-1})=(T_1,T_2,\ldots,T_{i-1}) and S_i< T_i.
Constraints
- 1\leq T\leq 500
- 2\leq N\leq 1000
- N-1\leq M\leq \min\left( \frac{N(N-1)}{2},5\times 10^4\right)
- 1\leq X,Y \leq N
- X\neq Y
- 1\leq U_i<V_i \leq N
- If i\neq j, then (U_i,V_i)\neq (U_j,V_j).
- The given graph is connected.
- The sum of N over all test cases in each input is at most 1000.
- The sum of M over all test cases in each input is at most 5\times 10^4.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T
\mathrm{case}_i represents the i-th test case. Each test case is given in the following format:
N M X Y U_1 V_1 U_2 V_2 \vdots U_M V_M
Output
Output T lines.
The i-th line (1\leq i\leq T) should contain the vertex numbers on the simple path that is the answer to the i-th test case, in order, separated by spaces.
That is, when the answer to the i-th test case is P=(P_1,P_2,\ldots,P_{\lvert P\rvert}),
output P_1, P_2, \ldots, P_{\lvert P\rvert} on the i-th line in this order, separated by spaces.
Sample Input 1
2 6 10 3 5 1 2 1 3 1 5 1 6 2 4 2 5 2 6 3 4 3 5 5 6 3 2 3 2 1 3 2 3
Sample Output 1
3 1 2 5 3 2
For the first test case, graph G is as follows:

The simple paths from vertex 3 to vertex 5 on G, listed in lexicographic order, are as follows:
- (3,1,2,5)
- (3,1,2,6,5)
- (3,1,5)
- (3,1,6,2,5)
- (3,1,6,5)
- (3,4,2,1,5)
- (3,4,2,1,6,5)
- (3,4,2,5)
- (3,4,2,6,1,5)
- (3,4,2,6,5)
- (3,5)
Among these, the lexicographically smallest is (3,1,2,5), so output 3,1,2,5 separated by spaces on the first line.
For the second test case, (3,2) is the only simple path from vertex 3 to vertex 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 N が与えられます。
1, +, *, (, ) のみからなる数式のうち、値が N となるものの中で、文字列としての長さが最小のものを一つ求めてください。
より厳密には、以下の条件をすべて満たす文字列 S のうち長さが最小のものを一つ求めてください。
- S は下の BNF 記法 の
<expr>シンボルに従う文字列である。 - S が表す数式の値は N である。
<expr> ::= <term> | <expr> "+" <term>
<term> ::= <factor> | <term> "*" <factor>
<factor> ::= <number> | "(" <expr> ")"
<number> ::= "1" | "1" <number>
<expr> シンボルに従う文字列として、以下のようなものがあります。
1111+111: 1111+111 を表します。(1+1)*(1+1): (1+1)\times (1+1) を表します。(11+(1+1)*(1+1))+1: (11+(1+1)\times (1+1))+1 を表します。
一方、以下の文字列は <expr> シンボルに従いません。
(1+1)(1+1)1+21-11/1)1(1++1+1(+1)1*+1
制約
- 1\leq N\leq 2000
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
9
出力例 1
(1+1+1)*(1+1+1)
値が 9 となるような数式は例えば以下のようなものがあります。
(1+1+1)*(1+1+1)1+1+1+1+1+1+1+1+1(1+1)*(1+1)*(1+1)+1
値が 9 となるような数式のうち、長さが最小となるものは (1+1+1)*(1+1+1) です。
入力例 2
11
出力例 2
11
入力例 3
403
出力例 3
1+(1+1+1)*(1+11+11+111)
Score : 500 points
Problem Statement
You are given a positive integer N.
Among all valid arithmetic expressions consisting of the characters 1, +, *, (, and ), find one of the minimum length whose value is N.
More formally, among the strings S satisfying all of the following conditions, find one of the minimum length:
- S conforms to the symbol
<expr>in the BNF below. - The value of the expression represented by S is N.
<expr> ::= <term> | <expr> "+" <term>
<term> ::= <factor> | <term> "*" <factor>
<factor> ::= <number> | "(" <expr> ")"
<number> ::= "1" | "1" <number>
Strings that conform to <expr> include:
1111+111representing 1111+111.(1+1)*(1+1)representing (1+1)\times(1+1).(11+(1+1)*(1+1))+1representing (11+(1+1)\times(1+1))+1.
Strings that do not conform to <expr> include:
(1+1)(1+1)1+21-11/1)1(1++1+1(+1)1*+1
Constraints
- 1 \le N \le 2000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print a solution.
Sample Input 1
9
Sample Output 1
(1+1+1)*(1+1+1)
Expressions whose value is 9 include:
(1+1+1)*(1+1+1)1+1+1+1+1+1+1+1+1(1+1)*(1+1)*(1+1)+1
Among them, a shortest is (1+1+1)*(1+1+1).
Sample Input 2
11
Sample Output 2
11
Sample Input 3
403
Sample Output 3
1+(1+1+1)*(1+11+11+111)