Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
正の整数 N が与えられます。
A\leq B\leq C かつ ABC\leq N であるような正の整数の組 (A,B,C) の個数を求めてください。
なお、制約の条件下で答えは 2^{63} 未満であることが保証されます。
制約
- 1 \leq N \leq 10^{11}
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
5
条件を満たす組は (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2) の 5 つです。
入力例 2
100
出力例 2
323
入力例 3
100000000000
出力例 3
5745290566750
Score : 300 points
Problem Statement
You are given a positive integer N.
Find the number of triples of positive integers (A, B, C) such that A\leq B\leq C and ABC\leq N.
The Constraints guarantee that the answer is less than 2^{63}.
Constraints
- 1 \leq N \leq 10^{11}
- N is an integer.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
5
There are five such triples: (1,1,1),(1,1,2),(1,1,3),(1,1,4),(1,2,2).
Sample Input 2
100
Sample Output 2
323
Sample Input 3
100000000000
Sample Output 3
5745290566750
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 次元座標平面があります。x 軸正方向を右向き、y 軸正方向を上向きとします。
この平面上に自己交差のない四角形があります。
4 つの頂点の座標は反時計回りに (A_x,A_y),(B_x,B_y),(C_x,C_y),(D_x,D_y) です。
この四角形が凸であるか判定してください。
なお、四角形の 4 つの内角が全て 180 度未満であるとき、かつ、その時に限り、その四角形は凸であるといいます。
制約
- -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
- 入力に含まれる値は全て整数である
- 与えられる 4 点は四角形の 4 頂点を反時計回りに並べたものである
- 与えられる 4 点のなす四角形は自己交差がなく退化していない。すなわち
- どの 2 頂点も同じ座標にない
- どの 3 頂点も同一直線上にない
- 隣接しない 2 辺は共有点を持たない
入力
入力は以下の形式で標準入力から与えられる。
A_x A_y B_x B_y C_x C_y D_x D_y
出力
与えられる四角形が凸なら Yes、凸でないなら No を出力せよ。
入力例 1
0 0 1 0 1 1 0 1
出力例 1
Yes
与えられた四角形は正方形であり、4 つの内角は全て 90 度です。したがって、この四角形は凸です。

入力例 2
0 0 1 1 -1 0 1 -1
出力例 2
No
角 A が 270 度です。したがって、この四角形は凸ではありません。

Score : 300 points
Problem Statement
Consider a two-dimensional coordinate plane, where the x-axis is oriented to the right, and the y-axis is oriented upward.
In this plane, there is a quadrilateral without self-intersection.
The coordinates of the four vertices are (A_x,A_y), (B_x,B_y), (C_x,C_y), and (D_x,D_y), in counter-clockwise order.
Determine whether this quadrilateral is convex.
Here, a quadrilateral is convex if and only if all four interior angles are less than 180 degrees.
Constraints
- -100 \leq A_x,A_y,B_x,B_y,C_x,C_y,D_x,D_y \leq 100
- All values in input are integers.
- The given four points are the four vertices of a quadrilateral in counter-clockwise order.
- The quadrilateral formed by the given four points has no self-intersection and is non-degenerate. That is,
- no two vertices are at the same coordinates;
- no three vertices are colinear; and
- no two edges that are not adjacent have a common point.
Input
Input is given from Standard Input in the following format:
A_x A_y B_x B_y C_x C_y D_x D_y
Output
If the given quadrilateral is convex, print Yes; otherwise, print No.
Sample Input 1
0 0 1 0 1 1 0 1
Sample Output 1
Yes
The given quadrilateral is a square, whose four interior angles are all 90 degrees. Thus, this quadrilateral is convex.

Sample Input 2
0 0 1 1 -1 0 1 -1
Sample Output 2
No
The angle A is 270 degrees. Thus, this quadrilateral is not convex.

Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
数字のみからなる、長さ N の文字列 S が与えられます。
S を並べ替えてできる文字列を十進法の整数として解釈したもののうち、平方数であるようなものがいくつあるか求めてください。
より厳密には、次のようになります。
S の先頭から i 番目 (1\leq i\leq N) の数字に対応する数を s _ i とします。
(1, \ldots, N) の順列 P=(p _ 1,p _ 2,\ldots,p _ N) によって \displaystyle \sum _ {i=1} ^ N s _ {p _ i}10 ^ {N-i} と書ける整数のうち、平方数であるようなものがいくつあるか求めてください。
制約
- 1\leq N\leq 13
- S は数字のみからなる長さ N の文字列
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N S
出力
答えを 1 行で出力せよ。
入力例 1
4 4320
出力例 1
2
P=(4,2,3,1) とすると、s _ 4\times10 ^ 3+s _ 2\times10 ^ 2+s _ 3\times10 ^ 1+s _ 1=324=18 ^ 2 となります。
P=(3,2,4,1) とすると、s _ 3\times10 ^ 3+s _ 2\times10 ^ 2+s _ 4\times10 ^ 1+s _ 1=2304=48 ^ 2 となります。
これら以外の並べ替え方では平方数にならないため、2 を出力してください。
入力例 2
3 010
出力例 2
2
P=(1,3,2) もしくは P=(3,1,2) とすると、\displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=1=1 ^ 2 となります。
P=(2,1,3) もしくは P=(2,3,1) とすると、\displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=100=10 ^ 2 となります。
これら以外の並べ替え方では平方数にならないため、2 を出力してください。 異なる並べ替え方でも、並べ替えた結果の数が同じなら 1 つと数えることに注意してください。
入力例 3
13 8694027811503
出力例 3
840
Score : 425 points
Problem Statement
You are given a string S of length N consisting of digits.
Find the number of square numbers that can be obtained by interpreting a permutation of S as a decimal integer.
More formally, solve the following.
Let s _ i be the number corresponding to the i-th digit (1\leq i\leq N) from the beginning of S.
Find the number of square numbers that can be represented as \displaystyle \sum _ {i=1} ^ N s _ {p _ i}10 ^ {N-i} with a permutation P=(p _ 1,p _ 2,\ldots,p _ N) of (1, \dots, N).
Constraints
- 1\leq N\leq 13
- S is a string of length N consisting of digits.
- N is an integer.
Input
The input is given from Standard Input in the following format:
N S
Output
Print the answer in a single line.
Sample Input 1
4 4320
Sample Output 1
2
For P=(4,2,3,1), we have s _ 4\times10 ^ 3+s _ 2\times10 ^ 2+s _ 3\times10 ^ 1+s _ 1=324=18 ^ 2.
For P=(3,2,4,1), we have s _ 3\times10 ^ 3+s _ 2\times10 ^ 2+s _ 4\times10 ^ 1+s _ 1=2304=48 ^ 2.
No other permutations result in square numbers, so you should print 2.
Sample Input 2
3 010
Sample Output 2
2
For P=(1,3,2) or P=(3,1,2), we have \displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=1=1 ^ 2.
For P=(2,1,3) or P=(2,3,1), we have \displaystyle\sum _ {i=1} ^ Ns _ {p _ i}10 ^ {N-i}=100=10 ^ 2.
No other permutations result in square numbers, so you should print 2. Note that different permutations are not distinguished if they result in the same number.
Sample Input 3
13 8694027811503
Sample Output 3
840
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
AtCoder 社は、オンラインショップでグッズを販売しています。
今、N 個のグッズが社内に残っています。 ここで、i (1\leq i\leq N) 個目のグッズの重さは W_i です。
高橋君は残ったグッズをまとめて D 袋の福袋として販売する事にしました。
高橋君は各福袋に入ったグッズの重さの合計の分散を最小にしたいと考えています。
ここで、各福袋に入ったグッズの重さの合計がそれぞれ x_1,x_2,\ldots,x_D であるとき、
それらの平均を \bar{x}=\frac{1}{D}(x_1+x_2+\cdots+x_D) として、
分散は V=\frac{1}{D}\displaystyle\sum_{i=1}^D (x_i-\bar{x})^2 として定義されます。
各福袋に入ったグッズの重さの合計の分散が最小になるようにグッズを分けた時の分散の値を求めてください。
ただし、空の福袋が存在してもかまいません(この時福袋に入ったグッズの重さの合計は 0 として定義されます)が、
どのグッズも D 袋のうちちょうど 1 つの福袋に入っている ようにするものとします。
制約
- 2 \leq D\leq N\leq 15
- 1 \leq W_i\leq 10^8
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D W_1 W_2 \ldots W_N
出力
各福袋に入ったグッズの重さの合計の分散が最小になるようにグッズを分けた時の分散の値を出力せよ。
出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。
入力例 1
5 3 3 5 3 6 3
出力例 1
0.888888888888889
1 つめの福袋に 1,3 個目のグッズを、
2 つめの福袋に 2,5 個目のグッズを、
3 つめの福袋に 4 個目のグッズを入れると、
それぞれの福袋に入ったグッズの重さの合計は 6,8,6 となります。
このとき、重さの平均は \frac{1}{3}(6+8+6)=\frac{20}{3} であり、
分散は
\frac{1}{3}\left\{\left(6-\frac{20}{3}\right)^2+\left(8-\frac{20}{3}\right)^2+\left(6-\frac{20}{3}\right)^2 \right\}=\frac{8}{9}=0.888888\ldots
となり、このときが最小です。
同じ重さのグッズが複数存在し得ること、 各グッズはいずれかの福袋に入っている必要があることに注意してください。
Score : 525 points
Problem Statement
AtCoder Inc. sells merchandise on its online shop.
There are N items remaining in the company. The weight of the i-th item (1\leq i\leq N) is W_i.
Takahashi will sell these items as D lucky bags.
He wants to minimize the variance of the total weights of the items in the lucky bags.
Here, the variance is defined as V=\frac{1}{D}\displaystyle\sum_{i=1}^D (x_i-\bar{x})^2, where x_1,x_2,\ldots,x_D are the total weights of the items in the lucky bags, and \bar{x}=\frac{1}{D}(x_1+x_2+\cdots+x_D) is the average of x_1,x_2,\ldots,x_D.
Find the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
It is acceptable to have empty lucky bags (in which case the total weight of the items in that bag is defined as 0),
but each item must be in exactly one of the D lucky bags.
Constraints
- 2 \leq D\leq N\leq 15
- 1 \leq W_i\leq 10^8
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D W_1 W_2 \ldots W_N
Output
Print the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
Your output will be considered correct if the absolute or relative error from the true value is at most 10^{-6}.
Sample Input 1
5 3 3 5 3 6 3
Sample Output 1
0.888888888888889
If you put the first and third items in the first lucky bag, the second and fifth items in the second lucky bag, and the fourth item in the third lucky bag, the total weight of the items in the bags are 6, 8, and 6, respectively.
Then, the average weight is \frac{1}{3}(6+8+6)=\frac{20}{3},
and the variance is
\frac{1}{3}\left\{\left(6-\frac{20}{3}\right)^2+\left(8-\frac{20}{3}\right)^2+\left(6-\frac{20}{3}\right)^2 \right\}=\frac{8}{9}=0.888888\ldots,
which is the minimum.
Note that multiple items may have the same weight, and that each item must be in one of the lucky bags.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
長さ N の数列 A = (A_1, A_2, \ldots, A_N) が与えられます。
Q 個のクエリが与えられるので、与えられた順に処理してください。各クエリは以下の 2 種類のいずれかです。
- タイプ 1 :
1 p xの形式で与えられる。 A_p の値を x に変更する。 - タイプ 2 :
2 l rの形式で与えられる。 (A_l, A_{l+1}, \ldots, A_r) において 2 番目に大きい値の個数を出力する。より厳密には、l \leq i \leq r を満たす整数 i であって、A_l, A_{l+1}, \ldots, A_r のうち A_i より大きい値がちょうど 1 種類であるものの個数を出力する。
制約
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- タイプ 1 のクエリにおいて、1 \leq p \leq N
- タイプ 1 のクエリにおいて、1 \leq x \leq 10^9
- タイプ 2 のクエリにおいて、1 \leq l \leq r \leq N
- タイプ 2 のクエリが 1 つ以上存在する
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N Q
A_1 A_2 \ldots A_N
\text{query}_{1}
\vdots
\text{query}_{Q}
ただし、\text{query}_{i} は i 個目のクエリであり、以下のいずれかの形式で与えられる。
1 p x
2 l r
出力
タイプ 2 のクエリの個数を q として、q 行出力せよ。 i 行目には i 個目のタイプ 2 のクエリに対する答えを出力せよ。
入力例 1
5 4 3 3 1 4 5 2 1 3 2 5 5 1 3 3 2 2 4
出力例 1
1 0 2
はじめ、A = (3, 3, 1, 4, 5) です。
1 個目のクエリでは、(3, 3, 1) において 2 番目に大きい値は 1 であり、3, 3, 1 の中に 1 は 1 個あるので 1 を出力します。
2 個目のクエリでは、(5) において 2 番目に大きい値は存在しないので 0 を出力します。
3 個目のクエリでは、A = (3, 3, 3, 4, 5) となります。
4 個目のクエリでは、(3, 3, 4) において 2 番目に大きい値は 3 であり、3, 3, 4 の中に 3 は 2 個あるので 2 を出力します。
入力例 2
1 1 1000000000 2 1 1
出力例 2
0
入力例 3
8 9 2 4 4 3 9 1 1 2 1 5 4 2 7 7 2 2 6 1 4 4 2 2 5 2 2 7 1 1 1 1 8 1 2 1 8
出力例 3
0 1 0 2 4
Score: 525 points
Problem Statement
You are given a sequence A = (A_1, A_2, \ldots, A_N) of length N.
Process Q queries in the order they are given. Each query is of one of the following two types:
- Type 1: Given in the form
1 p x. Change the value of A_p to x. - Type 2: Given in the form
2 l r. print the number of occurrences of the second largest value in (A_l, A_{l+1}, \ldots, A_r). More precisely, print the number of integers i satisfying l \leq i \leq r such that there is exactly one distinct value greater than A_i among A_l, A_{l+1}, \ldots, A_r.
Constraints
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- For type-1 queries, 1 \leq p \leq N.
- For type-1 queries, 1 \leq x \leq 10^9.
- For type-2 queries, 1 \leq l \leq r \leq N.
- There is at least one type-2 query.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q
A_1 A_2 \ldots A_N
\text{query}_{1}
\vdots
\text{query}_{Q}
Here, \text{query}_{i} is the i-th query and given in one of the following formats:
1 p x
2 l r
Output
Let q be the number of type-2 queries. Print q lines. The i-th line should contain the response to the i-th type-2 query.
Sample Input 1
5 4 3 3 1 4 5 2 1 3 2 5 5 1 3 3 2 2 4
Sample Output 1
1 0 2
Initially, A = (3, 3, 1, 4, 5).
For the first query, the second largest value in (3, 3, 1) is 1, which appears once in 3, 3, 1, so print 1.
For the second query, there is no second largest value in (5), so print 0.
The third query makes A = (3, 3, 3, 4, 5).
For the fourth query, the second largest value in (3, 3, 4), is 3, which appears twice in 3, 3, 4, so print 2.
Sample Input 2
1 1 1000000000 2 1 1
Sample Output 2
0
Sample Input 3
8 9 2 4 4 3 9 1 1 2 1 5 4 2 7 7 2 2 6 1 4 4 2 2 5 2 2 7 1 1 1 1 8 1 2 1 8
Sample Output 3
0 1 0 2 4