Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
正整数 N,M が与えられます。
N 行出力してください。
i 行目 (1\leq i\leq N) には、i\leq M のとき OK を、
そうでないとき、Too Many Requests を出力してください。
制約
- 1 \leq N \leq 10
- 1 \leq M \leq 10
- N,M は整数
入力
入力は以下の形式で標準入力から与えられる。
N M
出力
問題文の指示に従って、N 行出力せよ。
入力例 1
5 3
出力例 1
OK OK OK Too Many Requests Too Many Requests
N=5, M=3 のため、
1,2,3 行目には OK を、
4,5 行目には Too Many Requests を出力します。
入力例 2
3 5
出力例 2
OK OK OK
N=3, M=5 のため、
1,2,3 行目に OK を出力します。
Score : 100 points
Problem Statement
You are given positive integers N and M.
Print N lines.
The i-th line (1\leq i\leq N) should contain OK if i\leq M,
and Too Many Requests otherwise.
Constraints
- 1 \leq N \leq 10
- 1 \leq M \leq 10
- N and M are integers.
Input
The input is given from Standard Input in the following format:
N M
Output
Print N lines according to the instructions in the problem statement.
Sample Input 1
5 3
Sample Output 1
OK OK OK Too Many Requests Too Many Requests
Since N=5 and M=3,
print OK on the 1st, 2nd, and 3rd lines,
and print Too Many Requests on the 4th and 5th lines.
Sample Input 2
3 5
Sample Output 2
OK OK OK
Since N=3 and M=5,
print OK on the 1st, 2nd, and 3rd lines.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) と整数 M が与えられます。
A の N 個の要素から 1 個を取り除くことで、残りの N-1 個の要素の和をちょうど M にできるか判定してください。
制約
- 2\le N\le 100
- 0\le M\le 10000
- 0\le A_i\le 100
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N
出力
A の N 個の要素から 1 個を取り除くことで、残りの N-1 個の要素の和をちょうど M にできる場合は Yes を、そうでない場合は No を出力せよ。
入力例 1
4 10 3 2 3 4
出力例 1
Yes
A_1,A_3,A_4 を選ぶと総和は 3+3+4=10 となります。したがって、 Yes を出力してください。
入力例 2
5 16 3 3 4 2 5
出力例 2
No
どのように 4 つの要素を選んでもその総和を 16 にすることはできません。したがって、 No を出力してください。
入力例 3
6 16 0 8 0 2 6 8
出力例 3
Yes
Score : 200 points
Problem Statement
You are given an integer sequence of length N, A=(A_1,A_2,\ldots,A_N), and an integer M.
Determine whether it is possible to remove one of the N elements of A so that the sum of the remaining (N-1) elements is exactly M.
Constraints
- 2\le N\le 100
- 0\le M\le 10000
- 0\le A_i\le 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N
Output
If it is possible to remove one of the N elements of A so that the sum of the remaining (N-1) elements is exactly M, print Yes; otherwise, print No.
Sample Input 1
4 10 3 2 3 4
Sample Output 1
Yes
If you choose A_1,A_3,A_4, the sum is 3+3+4=10. Therefore, print Yes.
Sample Input 2
5 16 3 3 4 2 5
Sample Output 2
No
No matter how you choose four elements, their sum is not equal to 16. Therefore, print No.
Sample Input 3
6 16 0 8 0 2 6 8
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
1\leq i<j<k\leq N をみたす整数の組 (i,j,k) であって、
次の条件をみたすものの個数を求めてください。
A_i,A_j,A_k の中にちょうど 2 種類の値が含まれる。すなわち、A_i,A_j,A_k の中のいずれか 2 つは等しく、残りの 1 つは異なる。
制約
- 3 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
条件をみたす整数の組の個数を出力せよ。
入力例 1
5 3 2 5 2 2
出力例 1
6
例えば、(i,j,k)=(1,2,4) は、A_1=3, A_2=2, A_4=2 の中に 2,3 のちょうど 2 種類の値が含まれるため、条件をみたします。
これを含めて、(i,j,k)=(1,2,4),(1,2,5),(1,4,5),(2,3,4),(2,3,5),(3,4,5) の 6 組が条件をみたします。
よって、6 を出力します。
入力例 2
3 1 1 1
出力例 2
0
条件をみたす組が存在しない可能性もあります。
Score : 300 points
Problem Statement
You are given an integer sequence of length N, A=(A_1,A_2,\ldots,A_N).
Find the number of triples of integers (i,j,k) satisfying 1\leq i<j<k\leq N that satisfy the following condition:
Exactly two distinct values are contained in A_i,A_j,A_k. That is, two of A_i,A_j,A_k are equal, and the remaining one is different.
Constraints
- 3 \leq N \leq 2\times 10^5
- 1 \leq A_i \leq N
- All input values 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 number of triples of integers that satisfy the condition.
Sample Input 1
5 3 2 5 2 2
Sample Output 1
6
For example, (i,j,k)=(1,2,4) satisfies the condition because exactly two distinct values, 2 and 3, are contained in A_1=3, A_2=2, and A_4=2.
Including this, the six triples (i,j,k)=(1,2,4),(1,2,5),(1,4,5),(2,3,4),(2,3,5),(3,4,5) satisfy the condition.
Therefore, print 6.
Sample Input 2
3 1 1 1
Sample Output 2
0
There may be no triples that satisfy the condition.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
1 周の長さが M である池があり、その周上に 1 つの小屋と N 人の人が立っています。
実数 x (0\leq x <M) について、小屋から時計回りに x だけ進んだところを地点 x と定義します。
i 番目の人は、 地点 A_i にいます。
複数の人が同じ場所に立っていることもあります。
また、N 以下の整数 C が与えられます。 i=0,1,\ldots,M-1 について、X_i を次のように定義します。
- 高橋君は地点 (i+0.5) からスタートして、時計回りに動き始める。
- 高橋君は出会った人数の合計が C 未満である限り(時計回りに)動き続け、C 以上になったら止まる。ここで、「地点 y にいる人と出会う」とは、高橋君が地点 y に到達することを指す。
- 高橋君が止まるまでに出会った人数を X_i とする。ここで、高橋君が止まった地点に複数の人がいる場合、そこにいた人はすべて出会った人として数えられ、特に X_i は C より大きくなる可能性がある。
i=0,1,\ldots,M-1 にわたる X_i の総和、すなわち \displaystyle\sum_{i=0}^{M-1} X_i を求めてください。
制約
- 1\leq N\leq 5\times 10^5
- 1\leq M\leq 10^{12}
- 0\leq A_i\leq M-1
- 1\leq C\leq N
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M C A_1 A_2 \ldots A_N
出力
i=0,1,\ldots,M-1 にわたる X_i の総和を一行に出力せよ。
入力例 1
5 3 2 1 2 1 0 1
出力例 1
9
i=0 のとき、高橋君は地点 0.5 からスタートして時計回りに動きます。その後、次のようになります。
- 地点 1 で 1,3,5 番目の 3 人と出会い、今まで出会った人数の合計は 3 となる。これは C=2 以上であるため、高橋くんはそこで止まる。よって、X_0=3 である。
i=1 のとき、高橋君は地点 1.5 からスタートして時計回りに動きます。その後、次のようになります。
- 地点 2 で 2 番目の人と出会う。今まで出会った人数の合計は 1 であるため、動き続ける。
- 地点 0 で 4 番目の人と出会い、今まで出会った人数の合計は 2 となる。これは C=2 以上であるため、高橋くんはそこで止まる。よって、X_1=2 である。
i=2 のとき、高橋君は地点 2.5 からスタートして時計回りに動きます。その後、次のようになります。
- 地点 0 で 4 番目の人と会う。今まで出会った人数の合計は 1 であるため、動き続ける。
- 地点 1 で 1,3,5 番目の 3 人と会い、今まで出会った人数の合計は 4 となる。これは C=2 以上であるため、高橋くんはそこで止まる。よって、X_2=4 である。
よって、答えは X_0+X_1+X_2=3+2+4=9 となります。
入力例 2
1 1000000000000 1 1
出力例 2
1000000000000
高橋君はスタートする位置によらず、池の周りに立っている唯一の人である、地点 1 にいる人に出会った時に止まります。
よって、i によらず X_i=1 であり、答えは 10^{12} となります。
Score : 425 points
Problem Statement
There is a pond with a circumference of M, and on its shore stand one hut and N people.
For a real number x (0\leq x <M), define point x as the location that is x distance clockwise from the hut.
The i-th person is at point A_i.
Multiple people may be standing at the same location.
Additionally, an integer C not greater than N is given. For i=0,1,\ldots,M-1, define X_i as follows:
- Takahashi starts at point (i+0.5) and begins moving clockwise.
- Takahashi continues moving (clockwise) as long as the total number of people he has met is less than C, and stops when it becomes C or more. Here, "meeting a person at point y" means that Takahashi reaches point y.
- Let X_i be the number of people Takahashi met before stopping. Here, if there are multiple people at the point where Takahashi stopped, all the people there are counted as people he met, and particularly, X_i may be greater than C.
Find the sum of X_i over i=0,1,\ldots,M-1, that is, \displaystyle\sum_{i=0}^{M-1} X_i.
Constraints
- 1\leq N\leq 5\times 10^5
- 1\leq M\leq 10^{12}
- 0\leq A_i\leq M-1
- 1\leq C\leq N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M C A_1 A_2 \ldots A_N
Output
Print the sum of X_i over i=0,1,\ldots,M-1 on a single line.
Sample Input 1
5 3 2 1 2 1 0 1
Sample Output 1
9
When i=0, Takahashi starts at point 0.5 and moves clockwise. Then, the following happens:
- At point 1, he meets the 1st, 3rd, and 5th people, a total of three people, and the total number of people met so far is 3. This is not less than C=2, so Takahashi stops there. Therefore, X_0=3.
When i=1, Takahashi starts at point 1.5 and moves clockwise. Then, the following happens:
- At point 2, he meets the 2nd person. The total number of people met so far is 1, so he continues moving.
- At point 0, he meets the 4th person, and the total number of people met so far is 2. This is not less than C=2, so Takahashi stops there. Therefore, X_1=2.
When i=2, Takahashi starts at point 2.5 and moves clockwise. Then, the following happens:
- At point 0, he meets the 4th person. The total number of people met so far is 1, so he continues moving.
- At point 1, he meets the 1st, 3rd, and 5th people, a total of three people, and the total number of people met so far is 4. This is not less than C=2, so Takahashi stops there. Therefore, X_2=4.
Therefore, the answer is X_0+X_1+X_2=3+2+4=9.
Sample Input 2
1 1000000000000 1 1
Sample Output 2
1000000000000
Regardless of the starting position, Takahashi stops when he meets the only person standing around the pond, who is at point 1.
Therefore, X_i=1 regardless of i, and the answer is 10^{12}.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
N 頂点 M 辺の単純連結無向グラフ G が与えられます。
G の頂点および辺はそれぞれ頂点 1,2,\ldots,N、辺 1,2,\ldots,M と番号づけられており、
辺 i は頂点 U_i と頂点 V_i を結んでいます。
辺で結ばれている頂点の間は双方向に時間 1 で移動することができます。
また、各頂点は安全か危険かのどちらかであり、その状態は S と D のみからなる長さ N の文字列 S によって与えられます。
具体的には、S の i 文字目 (1\leq i\leq N) が S のとき頂点 i は安全であり、D のとき頂点 i は危険です。
ここで、安全な頂点が 2 つ以上、危険な頂点が 1 つ以上あることが保証されます。
危険な頂点 v それぞれについて、次の値を求めてください。
ある安全な頂点から出発し、v を経由し、 出発した頂点と異なる 安全な頂点へ移動するのにかかる時間としてあり得る最小値
制約
- 3\leq N\leq 2\times 10^5
- N-1\leq M\leq 2\times 10^5
- 1\leq U_i,V_i\leq N
- U_i\neq V_i
- i\neq j ならば \{ U_i,V_i \}\neq \{ U_j,V_j \}
- S は
SとDのみからなる長さ N の文字列 - N,M,U_i,V_i はすべて整数
- G は連結である。
- 安全な頂点が 2 つ以上存在する。
- 危険な頂点が 1 つ以上存在する。
入力
入力は以下の形式で標準入力から与えられる。
N M U_1 V_1 U_2 V_2 \vdots U_M V_M S
出力
G 上の危険な頂点の個数を K として、K 行出力せよ。
i 行目 (1\leq i\leq K) には、危険な頂点を頂点番号の昇順に並べた時に i 番目に来る頂点についての答えを出力せよ。
入力例 1
5 5 1 2 1 3 2 3 3 4 4 5 SSDDS
出力例 1
2 3
危険な頂点は(頂点番号について昇順に)頂点 3 と 4 です。
頂点 3 については、頂点 1 \to 頂点 3 \to 頂点 2 と移動するときなどが条件をみたします。
この移動にかかる時間は 2 であり、このときが最小です。
よって、1 行目に 2 を出力します。
頂点 4 については、頂点 1 \to 頂点 3 \to 頂点 4 \to 頂点 5 と移動するときなどが条件をみたします。
この移動にかかる時間は 3 であり、かかる時間が 2 以下の条件をみたす移動方法がないため、このときが最小です。
よって、2 行目に 3 を出力します。
入力例 2
3 2 1 2 2 3 SSD
出力例 2
3
危険な頂点は頂点 3 です。
頂点 1 \to 頂点 2 \to 頂点 3 \to 頂点 2 と移動するときなどが条件をみたします。
この移動にかかる時間は 3 であり、このときが最小です。
頂点 2 \to 頂点 3 \to 頂点 2 などの移動は、「出発した頂点と異なる」という条件をみたしていないことに注意してください。
Score : 450 points
Problem Statement
You are given a simple connected undirected graph G with N vertices and M edges.
The vertices and edges of G are numbered as vertices 1,2,\ldots,N and edges 1,2,\ldots,M, respectively,
and edge i connects vertices U_i and V_i.
You can move bidirectionally between vertices connected by an edge in time 1.
Additionally, each vertex is either safe or dangerous, and this state is given by a string S of length N consisting of S and D.
Specifically, vertex i is safe when the i-th character (1\leq i\leq N) of S is S, and vertex i is dangerous when it is D.
It is guaranteed that there are at least two safe vertices and at least one dangerous vertex.
For each dangerous vertex v, find the following value:
The minimum possible time to start from some safe vertex, pass through v, and move to a safe vertex different from the starting vertex.
Constraints
- 3\leq N\leq 2\times 10^5
- N-1\leq M\leq 2\times 10^5
- 1\leq U_i,V_i\leq N
- U_i\neq V_i
- If i\neq j, then \{ U_i,V_i \}\neq \{ U_j,V_j \}.
- S is a string of length N consisting of
SandD. - N,M,U_i,V_i are all integers.
- G is connected.
- There are at least two safe vertices.
- There is at least one dangerous vertex.
Input
The input is given from Standard Input in the following format:
N M U_1 V_1 U_2 V_2 \vdots U_M V_M S
Output
Let K be the number of dangerous vertices in G, and print K lines.
On the i-th line (1\leq i\leq K), print the answer for the i-th dangerous vertex when the dangerous vertices are arranged in ascending order of vertex number.
Sample Input 1
5 5 1 2 1 3 2 3 3 4 4 5 SSDDS
Sample Output 1
2 3
The dangerous vertices are (in ascending order of vertex number) vertices 3 and 4.
For vertex 3, moving from vertex 1 \to vertex 3 \to vertex 2 (for example) satisfies the condition.
The time required for this movement is 2, and this is the minimum.
Therefore, print 2 on the 1st line.
For vertex 4, moving from vertex 1 \to vertex 3 \to vertex 4 \to vertex 5 (for example) satisfies the condition.
The time required for this movement is 3, and there is no way to move that satisfies the condition with time 2 or less, so this is the minimum.
Therefore, print 3 on the 2nd line.
Sample Input 2
3 2 1 2 2 3 SSD
Sample Output 2
3
The dangerous vertex is vertex 3.
Moving from vertex 1 \to vertex 2 \to vertex 3 \to vertex 2 (for example) satisfies the condition.
The time required for this movement is 3, and this is the minimum.
Note that movements such as vertex 2 \to vertex 3 \to vertex 2 do not satisfy the condition that the destination is "different from the starting vertex".
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 525 点
問題文
3 行 N 列のグリッドが与えられます。上から i 行目、左から j 列目のマスをマス (i,j) と表します。マス (i,j) には S_{i,j} が # ならば壁マスで、 . ならば空きマスであり通行可能です。
Q 個のクエリが与えられるので、順に処理してください。
各クエリでは整数 r,c が与えられるので、マス (r,c) の状態を反転させてください。つまり、マス (r,c) が壁マスならば空きマスにし、空きマスならば壁マスにしてください。その後、以下の問題の答えを出力してください。
マス (1,1) から上下左右に隣接する空きマスに移動する操作を繰り返してマス (3,N) に移動することを考えます。このとき、マス (3,N) に到達できるか判定し、到達できる場合は操作回数の最小値を求めてください。
制約
- 2\le N\le 2\times 10^5
- S_{i,j} は
#または. - S_{1,1}=S_{3,N}=
. - 1\le Q\le 2\times 10^5
- 1\le r\le 3
- 1\le c\le N
- (r,c) \neq (1,1),(3,N)
- N,Q,r,c は整数
入力
入力は以下の形式で標準入力から与えられる。
N
S_{1,1}S_{1,2}\ldots S_{1,N}
S_{2,1}S_{2,2}\ldots S_{2,N}
S_{3,1}S_{3,2}\ldots S_{3,N}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
各クエリは以下の形式で与えられる。
r c
出力
Q 行出力せよ。
i 行目 (1\le i\le Q) には、 i 番目のクエリにおいてマス (1,1) からマス (3,N) に到達不可能ならば -1 を、到達可能ならば操作回数の最小値を出力せよ。
入力例 1
5 .#... .#.#. ...#. 3 1 2 1 2 2 3
出力例 1
6 10 -1
1 つ目のクエリではマス (1,2) の状態を反転させます。その結果、各マスの状態は以下のようになります。
..... .#.#. ...#.
このとき、マス (1,1) から順にマス (1,2),(1,3),(1,4),(1,5),(2,5),(3,5) と移動することで 6 回の操作でマス (3,5) に到達することができます。
2 つ目のクエリではマス (1,2) の状態を反転させます。その結果、各マスの状態は以下のようになります。
.#... .#.#. ...#.
このとき、マス (1,1) から順にマス (2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,4),(1,5),(2,5),(3,5) と移動することで 10 回の操作でマス (3,5) に到達することができます。
3 つ目のクエリではマス (2,3) の状態を反転させます。その結果、各マスの状態は以下のようになります。
.#... .###. ...#.
このとき、どのように操作してもマス (1,1) からマス (3,5) に到達することはできません。
入力例 2
7 .#..... .#..#.. ...#... 6 2 5 3 4 3 5 2 5 1 4 1 4
出力例 2
10 8 10 12 -1 12
Score : 525 points
Problem Statement
You are given a grid with three rows and N columns. Denote the cell at the i-th row from the top and j-th column from the left as cell (i,j). Cell (i,j) is a wall cell if S_{i,j} is #, and an empty cell and passable if it is ..
You are given Q queries, which you should process in order.
Each query gives integers r and c, and you should flip the state of cell (r,c). That is, if cell (r,c) is a wall cell, make it an empty cell, and if it is an empty cell, make it a wall cell. Then, output the answer to the following problem:
Consider moving from cell (1,1) to cell (3,N) by repeatedly moving to an empty cell adjacent up, down, left, or right. Determine whether cell (3,N) is reachable, and if reachable, find the minimum number of moves.
Constraints
- 2\le N\le 2\times 10^5
- S_{i,j} is
#or.. - S_{1,1}=S_{3,N}=
. - 1\le Q\le 2\times 10^5
- 1\le r\le 3
- 1\le c\le N
- (r,c) \neq (1,1),(3,N)
- N,Q,r,c are integers.
Input
The input is given from Standard Input in the following format:
N
S_{1,1}S_{1,2}\ldots S_{1,N}
S_{2,1}S_{2,2}\ldots S_{2,N}
S_{3,1}S_{3,2}\ldots S_{3,N}
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
Each query is given in the following format:
r c
Output
Print Q lines.
On the i-th line (1\le i\le Q), if cell (3,N) is unreachable from cell (1,1) in the i-th query, print -1; if reachable, print the minimum number of moves.
Sample Input 1
5 .#... .#.#. ...#. 3 1 2 1 2 2 3
Sample Output 1
6 10 -1
In the first query, flip the state of cell (1,2). As a result, the state of each cell becomes:
..... .#.#. ...#.
At this time, by moving from cell (1,1) through cells (1,2),(1,3),(1,4),(1,5),(2,5),(3,5) in order, you can reach cell (3,5) in six moves.
In the second query, flip the state of cell (1,2). As a result, the state of each cell becomes:
.#... .#.#. ...#.
At this time, by moving from cell (1,1) through cells (2,1),(3,1),(3,2),(3,3),(2,3),(1,3),(1,4),(1,5),(2,5),(3,5) in order, you can reach cell (3,5) in ten moves.
In the third query, flip the state of cell (2,3). As a result, the state of each cell becomes:
.#... .###. ...#.
At this time, no matter how you move, you cannot reach cell (3,5) from cell (1,1).
Sample Input 2
7 .#..... .#..#.. ...#... 6 2 5 3 4 3 5 2 5 1 4 1 4
Sample Output 2
10 8 10 12 -1 12
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 625 点
問題文
整数 N,M,A,B,X,R が与えられます。
\displaystyle \sum_{k=0}^{N-1} X^{(Ak+B) \bmod M} を R で割ったあまりを求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\le T\le 100
- 1\le N,M,R\le 10^9
- 0\le A,B < M
- 1\le X < R
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
各テストケース \text{case}_i は以下の形式で与えられる。
N M A B X R
出力
T 行出力せよ。
i 行目には i 番目のテストケースについて、\displaystyle \sum_{k=0}^{N-1} X^{(Ak+B) \bmod M} を R で割ったあまりを出力せよ。
入力例 1
3 4 5 2 1 2 1000000000 777 429 33 58 1 1000000000 20251025 429429 777 1025 575757 998244353
出力例 1
15 777 445271630
1 つ目のテストケースについて考えます。
- k=0 のとき: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 0+1) \bmod 5}=2^1=2 です。
- k=1 のとき: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 1+1) \bmod 5}=2^3=8 です。
- k=2 のとき: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 2+1) \bmod 5}=2^0=1 です。
- k=3 のとき: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 3+1) \bmod 5}=2^2=4 です。
以上より、求める値は 2+8+1+4 を 1000000000 で割ったあまりである 15 です。したがって、 1 行目には 15 を出力してください。
Score : 625 points
Problem Statement
You are given integers N,M,A,B,X,R.
Find the remainder when \displaystyle \sum_{k=0}^{N-1} X^{(Ak+B) \bmod M} is divided by R.
You are given T test cases, so find the answer for each of them.
Constraints
- 1\le T\le 100
- 1\le N,M,R\le 10^9
- 0\le A,B < M
- 1\le X < R
- All input values are integers.
Input
The input is given from Standard Input in the following format:
T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
Each test case \text{case}_i is given in the following format:
N M A B X R
Output
Print T lines.
On the i-th line, print the remainder when \displaystyle \sum_{k=0}^{N-1} X^{(Ak+B) \bmod M} is divided by R for the i-th test case.
Sample Input 1
3 4 5 2 1 2 1000000000 777 429 33 58 1 1000000000 20251025 429429 777 1025 575757 998244353
Sample Output 1
15 777 445271630
Consider the first test case.
- When k=0: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 0+1) \bmod 5}=2^1=2.
- When k=1: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 1+1) \bmod 5}=2^3=8.
- When k=2: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 2+1) \bmod 5}=2^0=1.
- When k=3: \displaystyle X^{(Ak+B) \bmod M}=2^{(2\times 3+1) \bmod 5}=2^2=4.
From the above, the desired value is the remainder when 2+8+1+4 is divided by 1000000000, which is 15. Therefore, print 15 on the 1st line.