Search is not available for this dataset
name
stringlengths 2
88
| description
stringlengths 31
8.62k
| public_tests
dict | private_tests
dict | solution_type
stringclasses 2
values | programming_language
stringclasses 5
values | solution
stringlengths 1
983k
|
|---|---|---|---|---|---|---|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int num, l, r, d, x;
x = 0;
cin >> num;
for (int i = 0; i < num; i++) {
cin >> l >> r >> d;
x = d;
while (true) {
if (x <= r && x >= l) {
x = ceil((double)r / d) * d;
break;
} else
break;
}
cout << x << endl;
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.*;
public class MinimumInteger {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-- > 0) {
int l = sc.nextInt();
int r = sc.nextInt();
int d = sc.nextInt();
if (d == 1) {
System.out.println(1);
continue;
}
if (d < l) {
System.out.println(l - l%d);
} else {
System.out.println(r - r%d + d);
}
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimension must be greater than zero!");
template <typename... Args>
Vec(int n = 0, Args... args)
: vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, const T& val = T()) : vector<T>(n, val) {}
};
long long int power(long long int x, long long int y, long long int p) {
long long int res = 1;
x = x % p;
if (y == 0) return 1;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
long long int modinv(long long int a, long long int p) {
return power(a, p - 2, p);
}
void solve() {
long double l, r, d;
cin >> l >> r >> d;
if (l / d > 1) {
cout << d << endl;
} else {
long long int x = r, y = d;
if (x % y == 0) {
cout << x + d << endl;
} else {
cout << (x / y + 1) * y << endl;
}
}
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int T = 1;
cin >> T;
for (long long int c = 0; c < T; c++) {
solve();
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class A58
{
public static void main(String[] args) {
int q, l, r, d, z, a, b;
Scanner sc = new Scanner(System.in);
q=sc.nextInt();
while(q-->0)
{
l=sc.nextInt();
r=sc.nextInt();
d=sc.nextInt();
if(d==1)
System.out.println(d);
else if(d>=l && d<=r)
{
z=r/d;
System.out.println(d*(z+1));
}
else
System.out.println(d);
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int i = 0 ; i < t ; i++){
int l = input.nextInt();
int r = input.nextInt();
int d = input.nextInt();
int x = d;
if(d < l || d > r){
System.out.println(d);
continue;
}
int dif = r - l;
int rem = dif / d;
System.out.println(d * (rem + 1) + d);
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
int a, b, c;
int n;
cin >> a >> b >> c;
if (c < a)
arr[i] = c;
else {
n = b / c;
arr[i] = (n + 1) * c;
}
}
for (int i = 0; i < n; i++) cout << arr[i];
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class main
{
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
int q=s.nextInt();
for(int i=0;i<q;i++)
{
int l,r,d,ans=0;
l=s.nextInt();
r=s.nextInt();
d=s.nextInt();
if(d<l || d>r)
{
ans=d;
}
else
{
int k=2,t=d;
while(t<=r)
{
if(d*k>r)
{
break;
}
else
{
t=d*k;
k++;
}
}
ans=t*k;
}
System.out.println(ans);
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python3
|
q = int(input())
while q>0:
l,r,d = map(int,input().split())
if d == 1:
print("1")
else:
if l > d:
print(l - l%d)
else:
print(r+d-r%d)
q = q - 1
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long t;
cin >> t;
while (t--) {
long l, r, d;
cin >> l >> r >> d;
long r1 = l / d;
long r2 = r / d;
if (r1 == 0 && r2 != 0) {
cout << (d * (r2 + 1)) << endl;
} else if (r1 != 0 && r2 != 0) {
cout << (d * (r2 + 1)) << endl;
} else if (r1 != 0 && r2 == 0) {
cout << (d * (r1 - 1)) << endl;
} else if (r1 == 0 && r2 == 0) {
cout << d << endl;
}
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python3
|
import math
q = int(input())
def next_divsor(r, d):
pass
for i in range(q):
l,r, d = list(map(int, input().split()))
if d < l or d > r:
print(d)
else:
if r % d == 0:
print(r + d)
else: print((r - (r % d)) * 2)
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t, l, r, d, ans;
cin >> t;
while (t--) {
cin >> l >> r >> d;
if (l > d) {
cout << d << endl;
} else if (l <= d) {
if (r < d)
cout << d << endl;
else {
if (r % d == 0)
cout << r + d << endl;
else {
ans = ((r % d) * d) + d;
cout << ans << endl;
}
}
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int q, l, r, d;
cin >> q;
while (q != 0) {
if (d < l)
cout << d;
else
cout << l + d - l % d;
q--;
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
while(q-->0)
{
long l = sc.nextLong();
long r = sc.nextLong();
long d = sc.nextLong();
long num = r/d +1;
long num2 = l/d;
long result = num;
if(num2*d<l&&num2!=0) result = num2;
else result = num;
if(d==1&&l>=2) result = 1;
System.out.println(result*d);
//System.out.println(num2);
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
struct node {
long long l;
long long r;
long long d;
long long x;
} x[505];
int i, j, k, t;
int main() {
int q;
scanf("%d", &q);
for (i = 0; i < q; i++) {
scanf("%lld%lld%lld", &x[i].l, &x[i].r, &x[i].d);
}
for (i = 0; i < q; i++) {
if (x[i].d > x[i].r) {
x[i].x = x[i].d;
} else if (x[i].d < x[i].l) {
x[i].x = x[i].d;
} else {
for (j = x[i].r + 1;; j++) {
if (j % x[i].d == 0) {
x[i].x = j;
break;
}
}
}
printf("%d", x[i].x);
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python2
|
entrada = int(raw_input())
for num in range(entrada):
valor = list(map(int,raw_input().split()))
if (valor[0]) > (valor[2]):
resultado = (valor[2])
else:
resultado2 = ((valor[1]) / (valor[2]) + 1)
resultado = (resultado2 * (valor[2]))
print(resultado)
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class codeforces
{
public static void main( String[] args ) throws IOException
{
Reader.init(System.in);
int query = Reader.nextInt();
for( int q =0 ; q < query ; q++ )
{
long l = Reader.nextLong();
long r = Reader.nextLong();
long d = Reader.nextLong();
long res = 0;
if ( d == 1 )
{
System.out.println(1);
}
else if ( d > r || d < l )
{
System.out.println(d);
}
else
{
long r1 = ((r/d)+1)*d;
System.out.println(r1);
}
}
}
}
class Reader
{
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input)
{
reader = new BufferedReader(new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
static String next() throws IOException
{
while ( ! tokenizer.hasMoreTokens() )
{
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException
{
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException
{
return Double.parseDouble( next() );
}
static long nextLong() throws IOException
{
return Long.parseLong( next() );
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int i = 0 ; i < t ; i++){
int l = input.nextInt();
int r = input.nextInt();
int d = input.nextInt();
int x = d;
if(d < l || d > r){
System.out.println(d);
continue;
}
int dif = r - l;
int div = dif / d;
int rem = 0;
if(dif > d)
rem = dif % d;
System.out.println(d * (div + 1) + d + rem);
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python2
|
from sys import stdin
import math
def minInt(x):
return 6
if len(x) < 3:
return -1
li = int(x[0])
ri = int(x[1])
di = int(x[2])
if di < li:
return di
elif di >= li and di < ri:
return int(math.ceil(ri/di+1)*di)
else:
return int(math.ceil((ri-1)/di+1)*di)
queries = int(raw_input())
result = []
while queries > 0:
args = raw_input()
x = minInt(args.split(' '))
result.append(x)
queries -= 1
for r in result:
print int(r)
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long i = 0, j = 0, r = 0, n, a, b, c, y = 0, l = 0, u = 0, d = 0,
min1 = 0, min2 = 0;
vector<long long> vec, sor(100001, -10);
cin >> n;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if (a > c)
cout << c;
else {
if (b % c == 0)
cout << b + c;
else
cout << b + c - b % c;
}
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char **argv) {
int n, a;
cin >> n;
int l, r, d;
for (int i = 0; i < n; i++) {
cin >> l >> r >> d;
cout << (l - d <= 0 ? r - (r % d) + d : (l % d == 0 ? 1 : d)) << endl;
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long l, r, d, check, flag, res, div;
cin >> l >> r >> d;
if (d == 1) {
if (l > 1)
cout << "1" << endl;
else
cout << r + 1 << endl;
continue;
}
if (l > d) {
check = l % d;
if (check == 0)
cout << l - d << endl;
else
cout << l - check << endl;
} else {
check = r % d;
if (check == 0)
cout << r + d << endl;
else
cout << r + (d - check) << endl;
}
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5, N2 = 2e5, N1 = 1e6, M = 1e2;
long long binpow(long long a, long long n) {
long long ans = 1;
while (n) {
if (n & 1) ans *= a;
a *= a;
n >>= 1;
}
return ans;
}
string s, s1, s2;
long long n, m, t, x, y, z;
long long k, k1, k2, g, g1, g2, ans, kol, kol1;
char c;
long long mxx = 0, mxy = 0;
int main() {
cin.tie(0);
cin >> t;
while (t--) {
long long l, r, d;
cin >> l >> r >> d;
while (true) {
if (d < l) break;
if (d > r) break;
d *= 2;
}
cout << d << endl;
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python3
|
a= int(input())
for i in range(a):
b=input()
actual= b.split()
l=int(actual[0])
r=int(actual[1])
d=int(actual[2])
num=1
while l<d*num<r:
num+=1
print(d*num)
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
/**
* @author: miaolei
* @date: 2019/1/18
* @description:
*/
public class SolutionCF1101A {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
for (int i = 0; i < q; i++) {
int l = scan.nextInt();
int r = scan.nextInt();
int d = scan.nextInt();
if (d < l){
System.out.println(d);
}else {
if (d < l || d > r){
System.out.println(d);
}else if(d == 1000000000 && r != 1000000000){
System.out.println(d+1);
}else if(d == 1000000000 && r == 1000000000){
System.out.println(1000000000);
}else if(d==1){
System.out.println(1);
}else {
int res = d;
while (res >= l && res<=r && res<1000000000){
for (int j = 2; j < 1000000000; j++) {
res = d*j;
if (res>=100000000 || res>r){
break;
}
}
}
System.out.println(res);
}
}
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
public class ads {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int q=sc.nextInt();
for(int i=0;i<q;i++) {
int l=sc.nextInt();
int r=sc.nextInt();
int d=sc.nextInt();
if(l>d) {
System.out.println(d);
}
else {
if(d>r)
System.out.println(d%r+r);
else
System.out.println((d-r%d)+r);
}
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution
{
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static long modInverse(long a,long m)
{
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
long q = a / m;
long t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
public static void main(String[] args) throws IOException
{
InputReader in=new InputReader(System.in);
PrintWriter w=new PrintWriter(System.out);
int q=in.nextInt();
for(int a=0;a<q;a++)
{
long l=in.nextLong();
long r=in.nextLong();
long d=in.nextLong();
if(d!=1)
w.println((r*d));
else
w.println((r+1));
}
w.close();
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class codeforces
{
public static void main( String[] args ) throws IOException
{
Reader.init(System.in);
int query = Reader.nextInt();
for( int q =0 ; q < query ; q++ )
{
long l = Reader.nextLong();
long r = Reader.nextLong();
long d = Reader.nextLong();
long res = 0;
if ( d == 1 )
{
System.out.println(1);
}
else if ( d > r || d < l )
{
System.out.println(d);
}
else
{
long r1 = r-(r%d)+d;
long r2 = l-(l%d)-d;
if ( r2 > 0 )
{
res = r2;
}
else
{
res = r1;
}
System.out.println(res);
}
}
}
}
class Reader
{
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input)
{
reader = new BufferedReader(new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
static String next() throws IOException
{
while ( ! tokenizer.hasMoreTokens() )
{
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException
{
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException
{
return Double.parseDouble( next() );
}
static long nextLong() throws IOException
{
return Long.parseLong( next() );
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python3
|
q = int(input())
intervalo = []
contador = 1
for i in range(q):
entrada = input().split(" ")
l = int(entrada[0])
r = int(entrada[1])
d = int(entrada[2])
if(l > d):
print(d)
else:
print(int((r/d+1)*d))
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class minimuminteger {
public static long minimum_integer(long l, long r, long d) {
long min = 0, tmp = 0;
int i = 0;
while (true) {
++i;
tmp = d * i;
if ((tmp < l && tmp < r) || (tmp > l && tmp > r)) {
min = tmp;
break;
}
}
++i;
tmp = d * i;
if ((tmp < l && tmp < r) || (tmp > l && tmp > r))
min = tmp;
return min;
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int test_cases = sc.nextInt();
while (test_cases > 0) {
long l = sc.nextInt();
long r = sc.nextInt();
long d = sc.nextInt();
if (d == 1 && l == 1)
System.out.println(r + 1);
else {
long min = minimum_integer(l, r, d);
System.out.println(min);
}
--test_cases;
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
var q=parseInt(readline());
for(var i=0;i<q;i++){
var s=readline().split(" ");
var l=parseInt(s[0]),r=parseInt(s[1]),d=parseInt(s[2]);
if(l<=d){
var y=r+1;
while(y%d!==0){y++;}print(y);}
else if(l>d){
var y=l-1;
while(y%d!==0){y--;}print(y);}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int l, r, k;
int o = r / k;
for (int i = 0; i < n; i++) {
cin >> l >> r >> k;
if (k > r || k < l) {
cout << k << endl;
} else if (k <= r && k >= l && r / k == 1) {
cout << k * (o + 1) << endl;
}
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long l, r, x = 1, d;
int q;
cin >> q;
while (q--) {
long long i = l;
cin >> l >> r >> d;
if (l != d && l / d >= d)
cout << d << endl;
else
cout << (r / d + 1) * d << endl;
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
inclass in = new inclass(inputStream);
PrintWriter out = new PrintWriter(outputStream);
AMinimumInteger solver = new AMinimumInteger();
solver.solve(1, in, out);
out.close();
}
static class AMinimumInteger {
public void solve(int testNumber, inclass in, PrintWriter out) {
int q = in.nextInt();
label:
while (q-- > 0) {
int l = in.nextInt(), r = in.nextInt(), d = in.nextInt();
int left = l - (l % d);
if (d == 1) out.println(1);
if (d == 1) continue label;
if (l % d == 0) {
left = l - (d);
if (left > 0) {
out.println(left);
continue label;
}
} else {
if (left > 0) {
out.println(left);
continue label;
}
}
long right = r - (r % d);
right = right + (long) (d);
out.println(right);
}
}
}
static class inclass {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private inclass.SpaceCharFilter filter;
public inclass(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int l, r, d;
cin >> l >> r >> d;
if (d < l)
cout << d;
else if (d > r)
cout << d;
else {
r /= d;
r++;
cout << r * d;
}
}
return 0;
}
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python2
|
t=input()
while t:
t=t-1
l,r,d=raw_input().split()
l=int(l)
r=int(r)
d=int(d)
for w in range(1,501,1):
if l<=d*w<=r:
continue
else:
print d*w
break
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
python2
|
t = input()
while t:
t -= 1
ip = map(int, raw_input().split())
l, r, d = ip[0], ip[1], ip[2]
flag = False
for i in range(1, l):
if i % d == 0:
print i
flag = True
break
if flag:
break
m = 1000000005L
i = r + 1
while i < m:
if i % d == 0:
print i
flag = True
break
i += 1
|
1101_A. Minimum Integer
|
You are given q queries in the following form:
Given three integers l_i, r_i and d_i, find minimum positive integer x_i such that it is divisible by d_i and it does not belong to the segment [l_i, r_i].
Can you answer all the queries?
Recall that a number x belongs to segment [l, r] if l β€ x β€ r.
Input
The first line contains one integer q (1 β€ q β€ 500) β the number of queries.
Then q lines follow, each containing a query given in the format l_i r_i d_i (1 β€ l_i β€ r_i β€ 10^9, 1 β€ d_i β€ 10^9). l_i, r_i and d_i are integers.
Output
For each query print one integer: the answer to this query.
Example
Input
5
2 4 2
5 10 4
3 10 1
1 2 3
4 6 5
Output
6
4
1
3
10
|
{
"input": [
"5\n2 4 2\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n"
],
"output": [
"6\n4\n1\n3\n10\n"
]
}
|
{
"input": [
"20\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n1 1000000000 2\n",
"1\n78 79 79\n",
"1\n6 6 6\n",
"20\n1 1 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n1 999999999 1\n",
"1\n78 1000 1\n",
"1\n77 10000 1\n",
"20\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"10\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n78 80 1\n",
"20\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n1 1000000000 3\n",
"1\n1 1 123456789\n",
"1\n80 100 1\n",
"5\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n1000000000 1000000000 1\n",
"1\n78 10000 1\n",
"1\n79 80 100\n",
"5\n1 1000000000 1\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n1 999999999 2\n",
"1\n78 89 34\n",
"1\n1 1 1\n",
"1\n1 3 2\n",
"10\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n1 999999998 1\n",
"4\n1 999999999 1\n1 999999998 1\n1 999999997 1\n1 999999996 1\n",
"5\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"2\n1 1 2\n1 1 2\n",
"1\n80 100 80\n",
"25\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n1 1000000000 1\n1 1000000000 1000000000\n2 1000000000 1\n1 999999999 1000000000\n5 6 5\n",
"30\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"16\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n1 1000000000 1\n",
"1\n1 1000000000 6\n",
"1\n5 5 5\n",
"1\n2 5 6\n",
"8\n1 999999998 1\n1 999999997 1\n1 999999996 1\n1 999999995 1\n1 999999994 1\n1 999999993 1\n1 999999992 1\n1 999999991 1\n",
"5\n80 100 10\n5 10 4\n3 10 1\n1 2 3\n4 6 5\n",
"1\n1 1000000000 1017\n",
"1\n1 1000000000 2\n"
],
"output": [
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"158\n",
"12\n",
"2\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"1\n",
"1\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1\n",
"1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n1000000002\n",
"123456789\n",
"1\n",
"1\n1\n1\n1\n1\n",
"1\n",
"100\n",
"1000000001\n1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n1000000000\n",
"34\n",
"2\n",
"4\n",
"999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n999999999\n",
"1000000000\n999999999\n999999998\n999999997\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"2\n2\n",
"160\n",
"1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n1000000001\n2000000000\n1\n1000000000\n10\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n1000000001\n",
"1000000002\n",
"10\n",
"6\n",
"999999999\n999999998\n999999997\n999999996\n999999995\n999999994\n999999993\n999999992\n",
"10\n4\n1\n3\n10\n",
"1000000845\n",
"1000000002\n"
]
}
|
IN-CORRECT
|
java
|
import java.util.Scanner;
import java.lang.Math;
public class MinimumInteger{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
int[] l = new int[t];
int[] r = new int[t];
int[] d = new int[t];
int flag =1;
for(int i=0;i<t;i++){
l[i] = sc.nextInt();
r[i] = sc.nextInt();
d[i] = sc.nextInt();
}
for(int h=0;h<t;h++){
flag =1;
for(int k=1;k<l[h];k++){
if(k%d[h]==0){
System.out.println(k);
flag =0;
break;
}
}
if(flag==0){
}else{
for(int j=r[h]+1;j<Math.pow(10,9);j++){
if(j%d[h]==0){
System.out.println(j);
flag =2;
break;
}
}
}
}
}
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
int bb[1 + 100000], dp[1 + 100000], ss[((100000 + 500 - 1) / 500)],
dq[((100000 + 500 - 1) / 500)][500 + 1 + 500];
void update(int h) {
int *qq = dq[h];
int i, t, c;
t = 0;
memset(qq, 0, (500 + 1 + 500) * sizeof *qq);
for (i = (h + 1) * 500; i > h * 500; i--) {
t += bb[i];
qq[500 + t] = (qq[500 + t] + dp[i - 1]) % 998244353;
}
for (c = 1; c <= 500 + 500; c++) qq[c] = (qq[c] + qq[c - 1]) % 998244353;
}
int main() {
static int pp[1 + 100000], ii[1 + 100000];
int n, m, k, h, i, j;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
pp[i] = ii[a];
ii[a] = i;
}
dp[0] = 1;
for (j = 1; j <= n; j++) {
int p, x, t;
m = (j - 1) / 500;
ss[m] += 1 - bb[j];
bb[j] = 1;
if ((p = pp[j])) {
h = (p - 1) / 500;
ss[h] += -1 - bb[p];
bb[p] = -1;
if (p <= m * 500) update(h);
if ((p = pp[p])) {
h = (p - 1) / 500;
ss[h] += 0 - bb[p];
bb[p] = 0;
if (p <= m * 500) update(h);
}
}
x = 0;
t = 0;
for (i = j; i > m * 500; i--) {
t += bb[i];
if (t <= k) x = (x + dp[i - 1]) % 998244353;
}
for (h = m - 1; h >= 0; h--) {
if (k - t >= -500)
x = (x + dq[h][500 + (500 < k - t ? 500 : k - t)]) % 998244353;
t += ss[h];
}
dp[j] = x;
if (j % 500 == 0) update(m);
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
struct bucket {
long long size, minm;
vector<long long> off;
vector<long long> dps;
vector<long long> pre;
bucket(long long b) {
size = b;
off = vector<long long>(b);
pre = vector<long long>(b);
dps = vector<long long>(b);
minm = 0;
}
void updoff(long long p, long long l, long long r) {
assert(l >= 0 && l < size && r >= 0 && r < size && l <= r);
long long nmnm = INT_MAX;
for (long long j = l; j <= r; ++j) nmnm = min(nmnm, minm + off[j] + p);
for (long long j = 0; j < l; ++j) nmnm = min(nmnm, minm + off[j]);
for (long long j = r + 1; j < size; ++j) nmnm = min(nmnm, minm + off[j]);
for (long long j = 0; j < l; ++j) off[j] = off[j] + minm - nmnm;
for (long long j = l; j <= r; ++j) off[j] = off[j] + minm + p - nmnm;
for (long long j = r + 1; j < size; ++j) off[j] = off[j] + minm - nmnm;
minm = nmnm;
pre = vector<long long>(size);
for (long long j = 0; j < size; ++j)
pre[off[j]] = (pre[off[j]] + dps[j]) % mod;
for (long long j = 1; j < size; ++j) pre[j] = (pre[j] + pre[j - 1]) % mod;
}
void upddp(long long idx, long long newdp) {
dps[idx] = newdp;
pre = vector<long long>(size);
for (long long j = 0; j < size; ++j)
pre[off[j]] = (pre[off[j]] + dps[j]) % mod;
for (long long j = 1; j < size; ++j) pre[j] = (pre[j] + pre[j - 1]) % mod;
}
};
void update_offset(vector<bucket> &ds, long long n, long long l, long long r,
long long p, long long b) {
assert(l <= r);
long long j = r / b;
ds[j].updoff(p, max(l, j * b) - j * b, r - j * b);
j--;
while (j * b > l) {
ds[j].minm += p;
j--;
}
if (l >= j * b && l < (j + 1) * b) ds[j].updoff(p, l - j * b, b - 1);
}
long long query(vector<bucket> &ds, long long n, long long r, long long b,
long long k) {
long long j = r / b;
long long ret = 0;
while (j >= 0) {
long long idx = k - ds[j].minm;
if (idx >= 0) ret = (ret + ds[j].pre[min(idx, b - 1)]) % mod;
j--;
}
return ret;
}
int32_t main() {
long long n, k;
cin >> n >> k;
long long b = sqrt(n);
long long m = ceil((double)n / b);
vector<long long> a(n);
for (auto &x : a) cin >> x;
vector<bucket> ds;
for (long long i = 0; i < m; ++i) {
bucket B = bucket(b);
ds.push_back(B);
}
vector<long long> lo(n + 1, -1);
vector<long long> llo(n + 1, -1);
vector<long long> dp(n);
dp[0] = 1;
lo[a[0]] = 0;
ds[0].updoff(1, 0, 0);
ds[0].upddp(0, 1);
if ((1 / b) < m) ds[1 / b].upddp(1 - (1 / b) * b, 1);
for (long long i = 1; i < n; ++i) {
if (lo[a[i]] != -1) update_offset(ds, n, llo[a[i]] + 1, lo[a[i]], -1, b);
update_offset(ds, n, lo[a[i]] + 1, i, 1, b);
dp[i] = query(ds, n, i, b, k);
long long j = (i + 1) / b;
if (j < m) ds[j].upddp((i + 1) - j * b, dp[i]);
llo[a[i]] = lo[a[i]];
lo[a[i]] = i;
}
cout << dp[n - 1];
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class CF1129D {
static final int MD = 998244353, A = 100000, B = 500;
static int[] bb, dp, ss;
static int[][] dq;
static void update(int h) {
int[] qq = dq[h];
Arrays.fill(qq, 0);
int t = 0;
for (int i = (h + 1) * B; i > h * B; i--) {
t += bb[i];
qq[B + t] = (qq[B + t] + dp[i - 1]) % MD;
}
for (int c = 1; c <= B + B; c++)
qq[c] = (qq[c] + qq[c - 1]) % MD;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] pp = new int[1 + n];
int[] ii = new int[1 + A];
for (int i = 1; i <= n; i++) {
int a = Integer.parseInt(st.nextToken());
pp[i] = ii[a]; ii[a] = i;
}
bb = new int[1 + n];
dp = new int[1 + n];
dp[0] = 1;
int m = (n + B - 1) / B;
ss = new int[m];
dq = new int[m][B + 1 + B];
for (int j = 1; j <= n; j++) {
int p;
m = (j - 1) / B;
ss[m] += 1 - bb[j]; bb[j] = 1;
if ((p = pp[j]) != 0) {
int h = (p - 1) / B;
ss[h] += -1 - bb[p]; bb[p] = -1;
if (p <= m * B)
update(h);
if ((p = pp[p]) != 0) {
h = (p - 1) / B;
ss[h] += 0 - bb[p]; bb[p] = 0;
if (p <= m * B)
update(h);
}
}
int x = 0, t = 0;
for (int i = j; i > m * B; i--)
if ((t += bb[i]) <= k)
x = (x + dp[i - 1]) % MD;
for (int h = m - 1; h >= 0; h--) {
if (k - t >= -B)
x = (x + dq[h][B + Math.min(B, k - t)]) % MD;
t += ss[h];
}
dp[j] = x;
if (j % B == 0)
update(m);
}
System.out.println(dp[n]);
}
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100002;
const int MOD = 998244353;
const int MAGIC = 200;
const int MAX_BLOCK = MAX_N / MAGIC + 7;
const int INF = 1e9;
int n, k, prv[MAX_N], pos[MAX_N], a[MAX_N];
int nBlock, L[MAX_BLOCK], R[MAX_BLOCK], blockID[MAX_N];
int v[MAX_N], offset[MAX_BLOCK], head[MAX_BLOCK], ps[MAX_BLOCK][MAX_N];
int f[MAX_N];
vector<pair<int, int> > all, comp[MAX_BLOCK];
void readInput() {
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> a[i];
}
void init() {
for (int i = 1; i <= n; ++i) {
prv[i] = pos[a[i]];
pos[a[i]] = i;
}
}
void sqrtDecompostion() {
for (int i = 1; i <= n; ++i) {
if (i % MAGIC == 1) {
R[nBlock] = i - 1;
L[++nBlock] = i;
}
}
R[nBlock] = n;
for (int i = 1; i <= nBlock; ++i) {
for (int j = L[i]; j <= R[i]; ++j) blockID[j] = i;
}
}
void upd(int idx, int l, int r, int delta) {
for (int i = L[idx]; i <= R[idx]; ++i) {
v[i] += offset[idx];
if (l <= i && i <= r) v[i] += delta;
}
offset[idx] = 0;
all.clear();
comp[idx].clear();
for (int i = L[idx]; i <= R[idx]; ++i) all.push_back({v[i], f[i - 1]});
sort(all.begin(), all.end());
comp[idx].push_back({-INF, 0});
for (auto x : all) {
if (x.first != comp[idx].back().first)
comp[idx].push_back(x);
else
comp[idx].back().second = (comp[idx].back().second + x.second) % MOD;
}
for (int i = 1; i < comp[idx].size(); ++i)
ps[idx][i] = (comp[idx][i].second + ps[idx][i - 1]) % MOD;
for (int i = 0; i < comp[idx].size(); ++i) {
if (comp[idx][i].first <= k) head[idx] = i;
}
}
void upd(int l, int r, int delta) {
if (l > r) return;
if (blockID[l] == blockID[r]) return upd(blockID[l], l, r, delta);
for (int i = blockID[l] + 1; i < blockID[r]; ++i) {
offset[i] += delta;
if (delta == -1) {
while (head[i] + 1 < comp[i].size() &&
comp[i][head[i] + 1].first + offset[i] <= k)
++head[i];
} else {
while (head[i] > 0 && comp[i][head[i]].first + offset[i] > k) --head[i];
}
}
upd(l, R[blockID[l]], delta);
upd(L[blockID[r]], r, delta);
}
int get() {
int res = 0;
for (int i = 1; i <= nBlock; ++i) res = (res + ps[i][head[i]]) % MOD;
return res;
}
void solve() {
for (int i = 1; i <= nBlock; ++i) upd(L[i], R[i], 0);
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int x1 = prv[i];
int x2 = prv[x1];
upd(x1 + 1, i, 1);
upd(x2 + 1, x1, -1);
f[i] = get();
upd(L[blockID[i]], R[blockID[i]], 0);
}
cout << f[n];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
readInput();
init();
sqrtDecompostion();
solve();
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
int const MOD = 998244353;
void add(int& a, int b) {
assert(0 <= a and a < MOD);
assert(0 <= b and b < MOD);
a += b;
if (a >= MOD) a -= MOD;
}
int main() {
std::ios::sync_with_stdio(0);
std::cin.tie(0);
int n, maxu;
std::cin >> n >> maxu;
std::vector<int> a(n);
for (int& x : a) std::cin >> x;
std::vector<int> dp(n + 1);
std::vector<int> b(n);
std::map<int, std::pair<int, int>> lastind;
int const BLOCKSIZE = 300;
int const nblock = (n - 1) / BLOCKSIZE + 1;
std::vector<int> boffset(nblock);
std::vector<std::vector<int>> bsum(nblock);
auto const updateb = [&](int n, int delta) {
int const blockn = n / BLOCKSIZE;
for (int i = blockn; i--;) boffset[i] += delta;
if (n % BLOCKSIZE != 0) {
for (int j = n % BLOCKSIZE; j; --j) {
b[n - j] += delta;
}
auto first = begin(b) + BLOCKSIZE * blockn;
auto last = begin(b) + std::min((int)b.size(), BLOCKSIZE * (blockn + 1));
int of1 = *std::min_element(first, last);
if (of1) {
std::transform(first, last, first, [of1](int x) { return x - of1; });
boffset[blockn] += of1;
}
auto& bsum_cur = bsum[blockn];
bsum_cur.assign(*std::max_element(first, last) + 1, 0);
std::for_each(first, last,
[&](int& x) { add(bsum_cur[x], dp[&x - &b[0]]); });
std::for_each(++begin(bsum_cur), end(bsum_cur),
[&](int& x) { add(x, (&x)[-1]); });
}
};
auto const getle = [&]() {
int64_t ans = 0;
for (int blocki = 0; blocki < nblock; ++blocki)
if (not bsum[blocki].empty()) {
int x = maxu - boffset[blocki];
if (x >= 0)
ans += bsum[blocki][std::min(x, (int)bsum[blocki].size() - 1)];
}
return int(ans % MOD);
};
auto const setdp = [&](int i, int val) {
int const blocki = i / BLOCKSIZE;
assert(b[i] == 0 ||
!(std::cerr << b[i] << ' ' << i << ' ' << boffset[blocki] << '\n'));
assert(boffset[blocki] == 0);
if (bsum[blocki].empty())
bsum[blocki].assign(1, val);
else
std::transform(begin(bsum[blocki]), end(bsum[blocki]),
begin(bsum[blocki]), [val](int x) {
add(x, val);
return x;
});
};
setdp(0, dp[0] = 1);
for (int i = 1; i <= n; ++i) {
auto [iter, success] = lastind.insert({a[i - 1], {i - 1, -1}});
if (!success) {
updateb(iter->second.first + 1, -2);
if (iter->second.second >= 0) {
updateb(iter->second.second + 1, 1);
} else {
}
iter->second = {i - 1, iter->second.first};
} else {
}
updateb(i, 1);
dp[i] = getle();
if (i < n) {
setdp(i, dp[i]);
}
}
std::cout << dp[n] << '\n';
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
struct bucket {
int sz, szq, ptr, lz;
pair<int, int> u[250 + 5];
pair<int, int> el[250 + 5];
} B[100005 / 250 + 5];
int n, k, tot;
int a[100005], dp[100005], lnk[100005];
int add(int x, int y) {
x += y;
if (x >= 998244353) x -= 998244353;
if (x < 0) x += 998244353;
return x;
}
void lzit(int l, int r, int sg) {
for (int i = l; i <= r; i++) {
pair<int, int>* u = B[i].u;
int& szq = B[i].szq;
int& ptr = B[i].ptr;
int& lz = B[i].lz;
lz += sg;
if (sg == 1) {
while (ptr && u[ptr].first + lz > k) {
tot = add(tot, -u[ptr--].second);
}
} else {
while (ptr < szq && u[ptr + 1].first + lz <= k) {
tot = add(tot, u[++ptr].second);
}
}
}
}
void pure(int buc, int l, int r, int sg) {
vector<pair<int, int> > a, b;
pair<int, int>* el = B[buc].el;
pair<int, int>* u = B[buc].u;
int& sz = B[buc].sz;
int& szq = B[buc].szq;
int& ptr = B[buc].ptr;
int& lz = B[buc].lz;
for (int i = 1; i <= sz; i++) {
if (el[i].second >= l && el[i].second <= r)
b.push_back(el[i]);
else
a.push_back(el[i]);
}
for (int i = 1; i <= ptr; i++) {
tot = add(tot, -u[i].second);
}
ptr = 0;
for (pair<int, int>& x : b) x.first += sg;
int ca = 0, cb = 0;
sz = szq = 0;
while (ca < ((int)a.size()) || cb < ((int)b.size())) {
if (cb == ((int)b.size()) ||
(ca != ((int)a.size()) && a[ca].first < b[cb].first)) {
el[++sz] = a[ca++];
} else {
el[++sz] = b[cb++];
}
}
for (int i = 1; i <= sz; i++) {
int sum = 0;
while (i + 1 <= sz && el[i].first == el[i + 1].first) {
sum = add(sum, dp[el[i++].second - 1]);
}
sum = add(sum, dp[el[i].second - 1]);
u[++szq] = {el[i].first, sum};
if (el[i].first + lz <= k) ptr = szq, tot = add(tot, sum);
}
}
void chng(int l, int r, int sg) {
int bl = (l + 250 - 1) / 250;
int br = (r + 250 - 1) / 250;
if (bl == br)
pure(bl, l, r, sg);
else {
pure(bl, l, bl * 250, sg);
pure(br, (br - 1) * 250 + 1, r, sg);
lzit(bl + 1, br - 1, sg);
}
}
int main(int argc, char* argv[]) {
for (int i = 1; i < 100005 / 250 + 5; i++) B[i].sz = B[i].ptr = B[i].lz;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
}
map<int, int> prv;
dp[0] = 1;
for (int i = 1; i <= n; i++) {
int pr = prv[a[i]];
if (pr) chng(lnk[pr] + 1, pr, -1);
chng(pr + 1, i, 1);
lnk[i] = pr;
prv[a[i]] = i;
B[(i + 250 - 1) / 250].el[++B[(i + 250 - 1) / 250].sz] = {0, i};
chng(i, i, 1);
dp[i] = tot;
}
printf("%d", dp[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[100007];
long long dp[100007];
int prv1[100007];
int prv2[100007];
class bucket {
public:
int st, en;
long long pref[300 + 2];
int val[300 + 2];
int mn = 0;
void recalc() {
int curr = val[0];
for (int i = st; i <= en; ++i) {
curr = (curr < val[i - st]) ? curr : val[i - st];
}
mn += curr;
for (int i = st; i <= en; ++i) {
val[i - st] -= curr;
}
for (int i = 0; i <= 300; ++i) {
pref[i] = 0;
}
for (int i = st; i <= en; ++i) {
pref[val[i - st]] = pref[val[i - st]] + ((i > 0) ? dp[i - 1] : 1);
if (pref[val[i - st]] >= 998244353) {
pref[val[i - st]] -= 998244353;
}
}
for (int i = 1; i <= 300; ++i) {
pref[i] += pref[i - 1];
if (pref[i] >= 998244353) {
pref[i] -= 998244353;
}
}
}
void update(int add) { mn += add; }
long long ask() {
if (mn > k) {
return 0;
}
int id = (k - mn);
if (id > 300) {
id = 300;
}
return pref[id];
}
};
bucket f[2 * 300];
int NUM_BUCKETS;
void input() {
scanf("%d%d", &n, &k);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
prv1[a[i]] = prv2[a[i]] = -1;
}
NUM_BUCKETS = (n / 300) + ((n % 300) != 0);
for (int i = 0; i < NUM_BUCKETS; ++i) {
f[i].st = i * 300;
f[i].en = min(f[i].st + 300 - 1, n - 1);
}
}
void upd_int(int st, int en, int val) {
if (en < st) {
return;
}
if (en - st <= 300) {
for (int i = st; i <= en; ++i) {
int id = (i / 300);
f[id].val[i - f[id].st] += val;
}
f[(st / 300)].recalc();
for (int i = st + 1; i <= en; ++i) {
if ((i / 300) != ((i - 1) / 300)) {
f[(i / 300)].recalc();
}
}
return;
}
for (int i = st; i <= en; ++i) {
if ((i / 300) != (st / 300)) {
break;
}
int id = (i / 300);
f[id].val[i - f[id].st] += val;
}
f[(st / 300)].recalc();
for (int i = en; i >= st; --i) {
if ((i / 300) != (en / 300)) {
break;
}
int id = (i / 300);
f[id].val[i - f[id].st] += val;
}
f[(en / 300)].recalc();
for (int i = (st / 300) + 1; i < (en / 300); ++i) {
f[i].update(val);
}
}
void solve() {
for (int i = 0; i < n; ++i) {
upd_int(prv2[a[i]] + 1, prv1[a[i]], -1);
prv2[a[i]] = prv1[a[i]];
upd_int(prv1[a[i]] + 1, i, 1);
prv1[a[i]] = i;
for (int j = 0; j <= (i / 300); ++j) {
dp[i] += f[j].ask();
dp[i] = (dp[i] < 998244353) ? dp[i] : dp[i] - 998244353;
}
}
printf("%I64d\n", dp[n - 1]);
}
int main() {
ios_base ::sync_with_stdio(false);
cin.tie(NULL);
input();
solve();
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
inline void read(int &x) {
int v = 0, f = 1;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void read(long long &x) {
long long v = 0ll, f = 1ll;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-')
f = -1;
else
v = (c & 15);
while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15);
x = v * f;
}
inline void readc(char &x) {
char c;
while (((c = getchar()) == ' ') || c == '\n')
;
x = c;
}
const int mod = 998244353;
int n, m, i, j, blk, bel[100005], a[100005], dp[100005], lst[100005],
cnt[100005], pos[100005];
int sum[455][100005], ans[455], tag[455];
void upd(int x, int y) {
int t = bel[x];
sum[t][cnt[x]] = (sum[t][cnt[x]] - dp[x] + mod) % mod;
if (cnt[x] + tag[t] <= m) ans[t] = (ans[t] - dp[x] + mod) % mod;
cnt[x] += y;
sum[t][cnt[x]] = (sum[t][cnt[x]] + dp[x]) % mod;
if (cnt[x] + tag[t] <= m) ans[t] = (ans[t] + dp[x]) % mod;
}
void update(int l, int r, int x) {
if (l > r) return;
int i;
if (bel[l] == bel[r]) {
for ((i) = (l); (i) <= (r); (i)++) upd(i, x);
return;
}
while (bel[l] == bel[l + 1]) {
upd(l, x);
l++;
}
while (bel[r] == bel[r - 1]) {
upd(r, x);
r--;
}
upd(l, x);
l++;
upd(r, x);
r--;
for ((i) = (bel[l]); (i) <= (bel[r]); (i)++) {
if (x > 0 && m - tag[i] >= 0) {
ans[i] = (ans[i] - sum[i][m - tag[i]] + mod) % mod;
}
tag[i] += x;
if (x < 0 && m - tag[i] >= 0) {
ans[i] = (ans[i] + sum[i][m - tag[i]]) % mod;
}
}
}
int main() {
read(n);
read(m);
blk = sqrt(n);
bel[0] = 1;
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
read(a[i]);
bel[i] = i / blk + 1;
}
dp[0] = sum[1][0] = ans[1] = 1;
for (((i)) = (1); ((i)) <= ((n)); ((i))++) {
lst[i] = pos[a[i]];
update(lst[lst[i]], lst[i] - 1, -1);
update(lst[i], i - 1, 1);
for (j = i - 1; j >= 0 && bel[j] == bel[i]; j--) {
if (cnt[j] + tag[bel[j]] <= m) {
dp[i] = (dp[i] + dp[j]) % mod;
}
}
for (j = bel[i] - 1; j; j--) {
dp[i] = (dp[i] + ans[j]) % mod;
}
sum[bel[i]][0] = (sum[bel[i]][0] + dp[i]) % mod;
if (tag[bel[i]] <= m) {
ans[bel[i]] = (ans[bel[i]] + dp[i]) % mod;
}
pos[a[i]] = i;
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chkMax(T &x, T y) {
if (y > x) x = y;
}
template <typename T>
void chkMin(T &x, T y) {
if (y < x) x = y;
}
template <typename T>
void inline read(T &x) {
int f = 1;
x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-') f = -1;
s = getchar();
}
while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
x *= f;
}
template <typename T>
void print(T x) {
if (x < 0) {
putchar('-');
print(-x);
return;
}
if (x >= 10) print(x / 10);
putchar((x % 10) + '0');
}
const int N = 1e5 + 5, S = 325, P = 998244353;
int n, k, a[N], tag[N], c[N], L[N], R[N], pos[N], t, pre[N], cnt[N];
int f[N];
vector<pair<int, int> > b[N];
void inline add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
void inline rebuild(int p) {
b[p].clear();
for (int i = L[p]; i <= R[p]; i++) {
c[i] += tag[p];
b[p].push_back(make_pair(c[i], f[i]));
}
tag[p] = 0;
sort(b[p].begin(), b[p].end());
for (int i = 1; i < b[p].size(); i++) add(b[p][i].second, b[p][i - 1].second);
}
void inline add(int l, int r, int k) {
if (l == 0) l++;
int p = pos[l], q = pos[r];
if (p == q) {
for (int i = l; i <= r; i++) {
c[i] += k;
}
rebuild(p);
} else {
for (int i = l; i <= R[p]; i++) c[i] += k;
rebuild(p);
for (int i = p + 1; i < q; i++) {
tag[i] += k;
}
for (int i = L[q]; i <= r; i++) c[i] += k;
rebuild(q);
}
}
int inline query(int l, int r) {
int p = pos[l], q = pos[r], res = 0;
if (p == q) {
for (int i = l; i <= r; i++)
if (c[i] + tag[p] <= k) add(res, f[i]);
} else {
for (int i = l; i <= R[p]; i++)
if (c[i] + tag[p] <= k) add(res, f[i]);
for (int i = L[q]; i <= r; i++)
if (c[i] + tag[p] <= k) add(res, f[i]);
for (int i = p + 1; i < q; i++) {
int t =
upper_bound(b[i].begin(), b[i].end(), make_pair(k - tag[i] + 1, 0)) -
b[i].begin() - 1;
if (t >= 0) add(res, b[i][t].second);
}
}
return res;
}
int main() {
read(n), read(k);
++n;
t = sqrt(n);
for (int i = 1; i <= n; i++) {
pos[i] = (i - 1) / t + 1;
if (!L[pos[i]]) L[pos[i]] = i;
R[pos[i]] = i;
}
for (int i = 2; i <= n; i++) {
read(a[i]);
pre[i] = cnt[a[i]];
cnt[a[i]] = i;
}
f[1] = 1;
for (int i = 2; i <= n; i++) {
if (pre[i]) {
int t = pre[i];
add(pre[t], t - 1, -1);
}
add(pre[i], i - 1, 1);
f[i] = query(1, i - 1);
if (R[pos[i]] == i) rebuild(pos[i]);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int N = 1e5 + 5, M = N / 183 + 3;
int bel[N], f[N], g[N], tag[M], s[M][183 + 3 << 1];
inline int read() {
int now = 0;
register char c = getchar();
for (; !isdigit(c); c = getchar())
;
for (; isdigit(c); now = now * 10 + c - 48, c = getchar())
;
return now;
}
void Update(int p, int v) {
int *s = ::s[bel[p]];
for (int i = 183; i <= 183 << 1; ++i)
(s[i] += v) >= 998244353 && (s[i] -= 998244353);
}
void Modify(int p, int v) {
int bel = ::bel[p], *s = ::s[bel];
tag[bel] += v;
for (int i = bel * 183 + 1; i <= p; ++i) {
if (v == 1)
(s[g[i] + 183] += 998244353 - f[i - 1]) >= 998244353 &&
(s[g[i] + 183] -= 998244353);
else
(s[g[i] - 1 + 183] += f[i - 1]) >= 998244353 &&
(s[g[i] - 1 + 183] -= 998244353),
(s[g[i] - 2 + 183] += f[i - 1]) >= 998244353 &&
(s[g[i] - 2 + 183] -= 998244353);
g[i] += v;
}
}
int Query(int p, int K) {
int bel = ::bel[p], sum = tag[bel];
long long res = 0;
for (int i = bel * 183 + 1; i <= p; ++i) g[i] <= K && (res += f[i - 1]);
while (bel--) {
assert(sum >= 0);
if (std::abs(sum - K) <= 183)
res += s[bel][K - sum + 183];
else if (sum < K)
res += s[bel][183 << 1];
sum += tag[bel];
}
return res % 998244353;
}
int main() {
static int las[N], pre[N];
int n = read(), K = read();
for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / 183;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int a = read();
las[i] = pre[a], pre[a] = i;
Update(i, f[i - 1]), Modify(i, 1);
if (las[i]) {
Modify(las[i], -2);
if (las[las[i]]) Modify(las[las[i]], 1);
}
f[i] = Query(i, K);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int N = 1e5 + 5, M = N / 140 + 3;
int bel[N], f[N], g[N], tag[M], s[M][140 + 3 << 1];
inline int read() {
int now = 0;
register char c = getchar();
for (; !isdigit(c); c = getchar())
;
for (; isdigit(c); now = now * 10 + c - 48, c = getchar())
;
return now;
}
void Update(int p, int v) {
int *s = ::s[bel[p]];
for (int i = 140; i <= 140 << 1; ++i)
(s[i] += v) >= 998244353 && (s[i] -= 998244353);
}
void Modify(int p, int v) {
int bel = ::bel[p], *s = ::s[bel];
tag[bel] += v;
for (int i = bel * 140 + 1; i <= p; ++i) {
if (v == 1)
(s[g[i] + 140] += 998244353 - f[i - 1]) >= 998244353 &&
(s[g[i] + 140] -= 998244353);
else
(s[g[i] - 1 + 140] += f[i - 1]) >= 998244353 &&
(s[g[i] - 1 + 140] -= 998244353),
(s[g[i] - 2 + 140] += f[i - 1]) >= 998244353 &&
(s[g[i] - 2 + 140] -= 998244353);
g[i] += v;
}
}
int Query(int p, int K) {
int bel = ::bel[p], sum = tag[bel];
long long res = 0;
for (int i = bel * 140 + 1; i <= p; ++i) g[i] <= K && (res += f[i - 1]);
while (bel--) {
if (std::abs(sum - K) <= 140)
res += s[bel][K - sum + 140];
else if (sum < K)
res += s[bel][140 << 1];
sum += tag[bel];
}
return res % 998244353;
}
int main() {
static int las[N], pre[N];
int n = read(), K = read();
for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / 140;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int a = read();
las[i] = pre[a], pre[a] = i;
Update(i, f[i - 1]), Modify(i, 1);
if (las[i]) {
Modify(las[i], -2);
if (las[las[i]]) Modify(las[las[i]], 1);
}
f[i] = Query(i, K);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, mod = 998244353;
int n, m, B;
int a[N], pos[N], bel[N], lst[N], f[N], cnt[N], delta[440], ans[440],
sum[440][N];
inline int read() {
int x = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while ('0' <= c && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x;
}
void update(int u, int v) {
int t = bel[u];
sum[t][cnt[u]] = (sum[t][cnt[u]] - f[u] + mod) % mod;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] - f[u] + mod) % mod;
cnt[u] += v;
sum[t][cnt[u]] = (sum[t][cnt[u]] + f[u]) % mod;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] + f[u]) % mod;
}
void add(int u, int v, int w) {
if (u > v) return;
int p = bel[u], q = bel[v];
if (p + 1 >= q) {
for (int i = u; i <= v; i++) update(i, w);
return;
}
for (int i = u; bel[i] == p; i++) update(i, w);
for (int i = v; bel[i] == q; i--) update(i, w);
for (int i = p + 1; i < q; i++) {
if (w > 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] - sum[i][m - delta[i]] + mod) % mod;
delta[i] += w;
if (w < 0)
if (m - delta[i] >= 0) ans[i] = (ans[i] + sum[i][m - delta[i]]) % mod;
}
}
int main() {
n = read();
m = read();
B = sqrt(n);
bel[0] = 1;
for (int i = 1; i <= n; i++) a[i] = read(), bel[i] = i / B + 1;
f[0] = sum[1][0] = ans[1] = 1;
for (int i = 1; i <= n; i++) {
lst[i] = pos[a[i]];
add(lst[lst[i]], lst[i] - 1, -1);
add(lst[i], i - 1, 1);
for (int j = i - 1; j >= 0 && bel[i] == bel[j]; j--)
if (cnt[j] + delta[bel[i]] <= m) f[i] = (f[i] + f[j]) % mod;
for (int j = bel[i] - 1; j; j--) f[i] = (f[i] + ans[j]) % mod;
sum[bel[i]][0] = (sum[bel[i]][0] + f[i]) % mod;
if (delta[bel[i]] <= m) ans[bel[i]] = (ans[bel[i]] + f[i]) % mod;
pos[a[i]] = i;
}
printf("%d", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2000 * 100 + 1;
const int Q = 100;
const int MOD = 998244353;
const int UNDEF = -10;
int a[MAXN];
int b[MAXN];
int pr[MAXN];
int prpr[MAXN];
int dp[MAXN];
int sum_dp[MAXN / Q + 10][2 * Q + 1];
int sum[MAXN / Q + 10];
int n, k;
void relax(int& x) {
while (x >= MOD) x -= MOD;
while (x < 0) x += MOD;
}
void up(int ind) {
int l = ind * Q;
int r = (ind + 1) * Q;
for (int i = 0; i < 2 * Q + 1; i++) {
sum_dp[ind][i] = 0;
}
int& sum = ::sum[ind] = 0;
for (int i = r - 1; i >= l; i--) {
if (b[i] != UNDEF) {
sum_dp[ind][sum + Q] += dp[i];
relax(sum_dp[ind][sum + Q]);
sum += b[i];
}
}
for (int i = 1; i < 2 * Q + 1; i++) {
sum_dp[ind][i] += sum_dp[ind][i - 1];
relax(sum_dp[ind][i]);
}
}
int get(int ind, int cur_sum) {
int up = max(-Q, min(Q, k - cur_sum));
return sum_dp[ind][up + Q];
}
int main(int argc, const char* argv[]) {
for (int i = 0; i < MAXN / Q + 10; i++) {
sum[i] = 0;
}
for (int i = 0; i < MAXN; i++) {
b[i] = UNDEF;
pr[i] = -1;
prpr[i] = -1;
}
cin >> n >> k;
for (int i = 1; i <= n; i++) {
scanf("%i", a + i);
a[i]--;
}
dp[0] = 1;
b[0] = 0;
up(0);
for (int i = 1; i <= n; i++) {
if (prpr[a[i]] != -1) {
b[prpr[a[i]]] = 0;
up(prpr[a[i]] / Q);
}
if (pr[a[i]] != -1) {
b[pr[a[i]]] = -1;
up(pr[a[i]] / Q);
}
prpr[a[i]] = pr[a[i]];
pr[a[i]] = i;
int sum = 1;
dp[i] = 0;
for (int j = (i - 1) / Q; j >= 0; j--) {
dp[i] += get(j, sum);
relax(dp[i]);
sum += ::sum[j];
}
b[i] = 1;
up(i / Q);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
java
|
import java.io.*;
import java.util.*;
public class A {
public static void main (String[] args) {
// int tests = 1000;
// while(tests-->0) {
// new A();
// }
new A();
}
int SIZE;
int[] dp, lastOcc, lastOcc2, actVals, Ls, Rs, sumBlock;
int[][] dpSum;
public A() {
// try { new TestGen(); } catch (Exception e) {}
FastScanner fs = new FastScanner();
// fs = new FastScanner("testdata.out");
// PrintWriter out = new PrintWriter(System.out);
System.err.println("");
int n = fs.nextInt();
int k = fs.nextInt();
SIZE = (int)Math.sqrt(n);
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = fs.nextInt()-1;
}
int numBlocks = n/SIZE + (n%SIZE > 0 ? 1 : 0);
int[] blockID = new int[n];
Ls = new int[numBlocks];
sumBlock = new int[numBlocks];
Rs = new int[numBlocks];
dpSum = new int[numBlocks][2 * SIZE + 1];
actVals = new int[n];
for(int i = 0, curID = 0; i < n; i += SIZE) {
int R = Math.min(n-1, i+SIZE-1);
Ls[curID] = i; Rs[curID] = R;
for(int j = i; j <= R; j++) {
blockID[j] = curID;
}
curID++;
}
int[] lastOcc = new int[n];
int[] lastOcc2 = new int[n];
Arrays.fill(lastOcc, -1);
Arrays.fill(lastOcc2, -1);
dp = new int[n + 1];
dp[0] = 1;
for(int i = 0; i < n; i++) {
if(lastOcc[a[i]] != -1) {
int who = lastOcc[a[i]];
int id = blockID[who];
actVals[who] = -1;
updateBlock(id);
}
if(lastOcc2[a[i]] != -1) {
int who = lastOcc2[a[i]];
int id = blockID[who];
actVals[who] = 0;
updateBlock(id);
}
int myID = blockID[i];
actVals[i] = 1;
int ways = 0;
int numD = 0;
for(int j = i; j >= Ls[myID]; j--) {
numD += actVals[j];
if(numD <= k) {
ways = add(ways, dp[j]);
}
}
for(int bid = myID-1; bid >= 0; bid--) {
int diff = k-numD;
diff = Math.min(diff, SIZE);
if(diff >= -SIZE) {
ways = add(ways, dpSum[bid][diff+SIZE]);
}
numD += sumBlock[bid];
}
dp[i+1] = ways;
updateBlock(myID);
lastOcc2[a[i]] = lastOcc[a[i]];
lastOcc[a[i]] = i;
}
System.out.println(dp[n]);
// int br = naive(a, k);
// System.out.println(br);
// if(br != dp[n]) throw null;
}
void updateBlock(int id) {
Arrays.fill(dpSum[id], 0);
int curSum = 0;
for(int j = Rs[id]; j >= Ls[id]; j--) {
curSum += actVals[j];
int pos = curSum+SIZE;
dpSum[id][pos] = add(dpSum[id][pos], dp[j]);
}
sumBlock[id] = curSum;
for(int j = 1; j < dpSum[id].length; j++) {
dpSum[id][j] = add(dpSum[id][j-1], dpSum[id][j]);
}
}
int naive(int[] a, int k) {
int n = a.length;
int max = 0; for(int i : a) max = Math.max(max, i);
int[] dp = new int[n + 1];
dp[0] = 1;
for(int i = 0; i < n; i++) {
int[] freq = new int[max+1];
int nD = 0;
for(int j = i; j >= 0; j--) {
int v = a[j];
if(freq[v] == 0) {
nD++;
}
else if(freq[v] == 1) {
nD--;
}
freq[v]++;
if(nD <= k) dp[i+1] = add(dp[i+1], dp[j]);
}
}
System.out.println("> " + Arrays.toString(dp));
return dp[n];
}
int MOD = 998244353;
int add(int a, int b) {
a += b;
if(a >= MOD) a -= MOD;
return a;
}
class Block {
int L, R;
int[] pref, dpSum;
Block(int l, int r) {
L = l; R = r;
pref = new int[2 * SIZE + 1];
dpSum = new int[2 * SIZE + 1];
}
void update(int pos) {
}
}
class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for(int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
}
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int B = 300;
const int mod = 998244353;
const int N = 1e5 + 10 + B;
int n, k, arr[N];
int memo[N];
int which(int i) { return i / B; }
void mod_add(int &a, int b) { a = (a + 1LL * b) % mod; }
struct bucket {
int ID;
int offset = 0;
int cnt[B];
int prefix[B];
void rebuild() {
int mn = cnt[0];
for (int i = 0; i < B; ++i) {
mn = min(mn, cnt[i]);
}
offset += mn;
for (int i = 0; i < B; ++i) {
prefix[i] = 0;
}
for (int i = 0; i < B; ++i) {
cnt[i] -= mn;
assert(0 <= cnt[i] && cnt[i] < B);
mod_add(prefix[cnt[i]], memo[i + ID * B]);
}
for (int i = 1; i < B; ++i) {
mod_add(prefix[i], prefix[i - 1]);
}
}
} buckets[N / B + 3];
void add(int L, int R, int diff) {
for (int i = L; i <= R && which(i) == which(L); ++i) {
buckets[which(i)].cnt[i - i / B * B] += diff;
}
buckets[which(L)].rebuild();
if (which(L) == which(R)) return;
for (int i = which(L) + 1; i < which(R); ++i) {
buckets[i].offset += diff;
}
for (int i = R; i >= 0 && which(i) == which(R); --i) {
buckets[which(i)].cnt[i - i / B * B] += diff;
}
buckets[which(R)].rebuild();
}
int query(int R) {
if (R < 0) return 0;
int sum = 0;
for (int i = 0; i <= which(R); ++i) {
int tgt = k - buckets[i].offset;
tgt = min(tgt, B - 1);
if (tgt < 0) continue;
mod_add(sum, buckets[i].prefix[tgt]);
}
assert(0 <= sum && sum < mod);
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
vector<vector<int>> pos(n + 1);
for (int i = 0; i <= which(n); ++i) {
buckets[i].ID = i;
}
for (int i = 0; i <= n; ++i) {
pos[i].push_back(-1);
}
for (int i = 0; i < n; ++i) {
cin >> arr[i];
}
memo[0] = 1;
buckets[which(0)].rebuild();
for (int i = 0; i < n; ++i) {
int currS = pos[arr[i]].size();
if (currS >= 2) {
int L = pos[arr[i]][currS - 2] + 1;
int R = pos[arr[i]].back();
add(L, R, -1);
}
add(pos[arr[i]].back() + 1, i, 1);
memo[i + 1] = query(i);
pos[arr[i]].push_back(i);
buckets[which(i + 1)].rebuild();
}
cout << memo[n];
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
java
|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
DIsolation solver = new DIsolation();
solver.solve(1, in, out);
out.close();
}
}
static class DIsolation {
static Modular mod = new Modular(998244353);
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
int k = in.readInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = in.readInt();
}
int[] registries = new int[n + 1];
int[] last = new int[n + 1];
for (int i = 1; i <= n; i++) {
last[i] = registries[a[i]];
registries[a[i]] = i;
}
int[] dp = new int[n + 1];
BlockManager bm = new BlockManager(n, k);
bm.append(0, new Element(0, 1));
for (int i = 1; i <= n; i++) {
if (last[i] == 0) {
bm.add(0, i - 1, 1);
} else {
int l = last[last[i]] + 1;
int m = last[i];
bm.add(l - 1, m - 1, -1);
bm.add(m, i - 1, 1);
}
dp[i] = mod.valueOf(bm.sumOf());
bm.append(i, new Element(0, dp[i]));
}
out.println(dp[n]);
}
}
static class Element {
int k;
int val;
public Element(int k, int val) {
this.k = k;
this.val = val;
}
}
static interface LongEntryIterator {
boolean hasNext();
void next();
long getEntryKey();
long getEntryValue();
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class Modular {
int m;
public Modular(int m) {
this.m = m;
}
public Modular(long m) {
this.m = (int) m;
if (this.m != m) {
throw new IllegalArgumentException();
}
}
public Modular(double m) {
this.m = (int) m;
if (this.m != m) {
throw new IllegalArgumentException();
}
}
public int valueOf(long x) {
x %= m;
if (x < 0) {
x += m;
}
return (int) x;
}
public String toString() {
return "mod " + m;
}
}
static class DigitUtils {
private DigitUtils() {
}
public static int floorDiv(int a, int b) {
return a < 0 ? -ceilDiv(-a, b) : a / b;
}
public static int ceilDiv(int a, int b) {
if (a < 0) {
return -floorDiv(-a, b);
}
int c = a / b;
if (c * b < a) {
return c + 1;
}
return c;
}
}
static class BlockManager {
Element[] buf;
Block[] blocks;
int bSize;
public BlockManager(int n, int k) {
bSize = (int) Math.ceil(Math.sqrt(n + 1));
buf = new Element[bSize];
int m = DigitUtils.ceilDiv(n + 1, bSize);
blocks = new Block[m];
for (int i = 0; i < m; i++) {
blocks[i] = new Block(bSize, k);
}
}
public void append(int i, Element e) {
blocks[i / bSize].append(e);
}
public void add(int ll, int rr, int x) {
for (int i = 0; i < blocks.length; i++) {
int l = i * bSize;
int r = l + bSize - 1;
if (r < ll || l > rr) {
continue;
}
if (ll <= l && r <= rr) {
blocks[i].add(x);
} else {
blocks[i].add(Math.max(ll, l) - l, Math.min(rr, r) - l, x, buf);
}
}
}
public long sumOf() {
long ans = 0;
for (Block b : blocks) {
ans += b.sum;
}
return ans;
}
}
static class Block {
Element[] elements;
LongHashMap map;
int size;
int add;
long sum;
int k;
public Block(int n, int k) {
elements = new Element[n];
this.k = k;
map = new LongHashMap(n, false);
}
public void add(int x) {
if (x > 0) {
sum -= map.getOrDefault(k - add, 0);
add++;
} else {
add--;
sum += map.getOrDefault(k - add, 0);
}
}
public void add(int l, int r, int x, Element[] buf) {
pushDown();
for (int i = l; i <= r; i++) {
map.put(elements[i].k, map.getOrDefault(elements[i].k, 0) - elements[i].val);
if (x > 0) {
if (elements[i].k == k) {
sum -= elements[i].val;
}
elements[i].k++;
} else {
elements[i].k--;
if (elements[i].k == k) {
sum += elements[i].val;
}
}
map.put(elements[i].k, map.getOrDefault(elements[i].k, 0) + elements[i].val);
}
}
private void pushDown() {
if (add != 0) {
map.clear();
for (int i = 0; i < size; i++) {
elements[i].k += add;
map.put(elements[i].k, map.getOrDefault(elements[i].k, 0L) + elements[i].val);
}
add = 0;
}
}
public void append(Element e) {
pushDown();
elements[size++] = e;
map.put(e.k, map.getOrDefault(e.k, 0) + e.val);
if (e.k <= k) {
sum += e.val;
}
}
}
static class LongHashMap {
private int[] slot;
private int[] next;
private long[] keys;
private long[] values;
private int alloc;
private boolean[] removed;
private int mask;
private int size;
private boolean rehash;
public LongHashMap(int cap, boolean rehash) {
this.mask = (1 << (32 - Integer.numberOfLeadingZeros(cap - 1))) - 1;
slot = new int[mask + 1];
next = new int[cap + 1];
keys = new long[cap + 1];
values = new long[cap + 1];
removed = new boolean[cap + 1];
this.rehash = rehash;
}
private void doubleCapacity() {
int newSize = Math.max(next.length + 10, next.length * 2);
next = Arrays.copyOf(next, newSize);
keys = Arrays.copyOf(keys, newSize);
values = Arrays.copyOf(values, newSize);
removed = Arrays.copyOf(removed, newSize);
}
public void alloc() {
alloc++;
if (alloc >= next.length) {
doubleCapacity();
}
next[alloc] = 0;
removed[alloc] = false;
size++;
}
private void rehash() {
int[] newSlots = new int[Math.max(16, slot.length * 2)];
int newMask = newSlots.length - 1;
for (int i = 0; i < slot.length; i++) {
if (slot[i] == 0) {
continue;
}
int head = slot[i];
while (head != 0) {
int n = next[head];
int s = hash(keys[head]) & newMask;
next[head] = newSlots[s];
newSlots[s] = head;
head = n;
}
}
this.slot = newSlots;
this.mask = newMask;
}
private int hash(long x) {
int h = Long.hashCode(x);
return h ^ (h >>> 16);
}
public void put(long x, long y) {
put(x, y, true);
}
public void put(long x, long y, boolean cover) {
int h = hash(x);
int s = h & mask;
if (slot[s] == 0) {
alloc();
slot[s] = alloc;
keys[alloc] = x;
values[alloc] = y;
} else {
int index = findIndexOrLastEntry(s, x);
if (keys[index] != x) {
alloc();
next[index] = alloc;
keys[alloc] = x;
values[alloc] = y;
} else if (cover) {
values[index] = y;
}
}
if (rehash && size >= slot.length) {
rehash();
}
}
public long getOrDefault(long x, long def) {
int h = hash(x);
int s = h & mask;
if (slot[s] == 0) {
return def;
}
int index = findIndexOrLastEntry(s, x);
return keys[index] == x ? values[index] : def;
}
private int findIndexOrLastEntry(int s, long x) {
int iter = slot[s];
while (keys[iter] != x) {
if (next[iter] != 0) {
iter = next[iter];
} else {
return iter;
}
}
return iter;
}
public void clear() {
alloc = 0;
Arrays.fill(slot, 0);
size = 0;
}
public LongEntryIterator iterator() {
return new LongEntryIterator() {
int index = 1;
int readIndex = -1;
public boolean hasNext() {
while (index <= alloc && removed[index]) {
index++;
}
return index <= alloc;
}
public long getEntryKey() {
return keys[readIndex];
}
public long getEntryValue() {
return values[readIndex];
}
public void next() {
if (!hasNext()) {
throw new IllegalStateException();
}
readIndex = index;
index++;
}
};
}
public String toString() {
LongEntryIterator iterator = iterator();
StringBuilder builder = new StringBuilder("{");
while (iterator.hasNext()) {
iterator.next();
builder.append(iterator.getEntryKey()).append("->").append(iterator.getEntryValue()).append(',');
}
if (builder.charAt(builder.length() - 1) == ',') {
builder.setLength(builder.length() - 1);
}
builder.append('}');
return builder.toString();
}
}
static class FastOutput implements AutoCloseable, Closeable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput println(int c) {
cache.append(c).append('\n');
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, mod = 998244353;
int n, m, B, a[N], pos[N], bel[N], lst[N], f[N], cnt[N], delta[440], ans[440],
sum[440][N];
void update(int u, int v) {
int t = bel[u];
sum[t][cnt[u]] = (sum[t][cnt[u]] - f[u] + mod) % mod;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] - f[u] + mod) % mod;
cnt[u] += v;
sum[t][cnt[u]] = (sum[t][cnt[u]] + f[u]) % mod;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] + f[u]) % mod;
}
void add(int u, int v, int w) {
if (u > v) return;
int p = bel[u], q = bel[v];
if (p + 1 >= q) {
for (int i = u; i <= v; i++) update(i, w);
return;
}
for (int i = u; bel[i] == p; i++) update(i, w);
for (int i = v; bel[i] == q; i--) update(i, w);
for (int i = p + 1; i < q; i++) {
if (w > 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] - sum[i][m - delta[i]] + mod) % mod;
delta[i] += w;
if (w < 0)
if (m - delta[i] >= 0) ans[i] = (ans[i] + sum[i][m - delta[i]]) % mod;
}
}
int main() {
scanf("%d%d", &n, &m);
B = sqrt(n);
bel[0] = 1;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), bel[i] = i / B + 1;
f[0] = sum[1][0] = ans[1] = 1;
for (int i = 1; i <= n; i++) {
lst[i] = pos[a[i]];
add(lst[lst[i]], lst[i] - 1, -1);
add(lst[i], i - 1, 1);
int j = i - 1;
for (int j = i - 1; j >= 0 && bel[i] == bel[j]; j--)
if (cnt[j] + delta[bel[i]] <= m) f[i] = (f[i] + f[j]) % mod;
for (int j = bel[i] - 1; j; j--) f[i] = (f[i] + ans[j]) % mod;
sum[bel[i]][0] = (sum[bel[i]][0] + f[i]) % mod;
if (delta[bel[i]] <= m) ans[bel[i]] = (ans[bel[i]] + f[i]) % mod;
pos[a[i]] = i;
}
printf("%d", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int SIZE = 500;
int n, k;
vector<int> dp;
vector<int> pos;
vector<int> offset;
vector<vector<int>> sum;
const int MOD = 998244353;
int plusM(int lhs, int rhs) {
int res = lhs + rhs;
return res >= MOD ? res - MOD : res;
}
int minusM(int lhs, int rhs) {
return lhs >= rhs ? lhs - rhs : lhs + MOD - rhs;
}
void inc(int bid, int a, int b) {
int l = bid * SIZE, r = (bid + 1) * SIZE;
if (a <= l && r <= b) {
offset[bid]--;
} else {
int lb = max(l, a);
int ub = min(r, b);
for (int i = lb; i < ub; i++) {
sum[bid][pos[i]] = minusM(sum[bid][pos[i]], dp[i]);
pos[i]++;
}
}
}
void dec(int bid, int a, int b) {
int l = bid * SIZE, r = (bid + 1) * SIZE;
if (a <= l && r <= b) {
offset[bid]++;
} else {
int lb = max(l, a);
int ub = min(r, b);
for (int i = lb; i < ub; i++) {
sum[bid][pos[i] - 1] = plusM(sum[bid][pos[i] - 1], dp[i]);
pos[i]--;
}
}
}
int calc(int bid) {
int pos = offset[bid] + k;
return sum[bid][min(pos, (int)sum[bid].size() - 1)];
}
void makeBlock(int bid) {
sum[bid].resize(2 * n + 4);
offset[bid] = n + 1;
int l = bid * SIZE, r = (bid + 1) * SIZE;
for (int i = l; i < r; i++) {
pos[i] += offset[bid];
sum[bid][pos[i]] = plusM(sum[bid][pos[i]], dp[i]);
}
for (int i = 1; i < sum[bid].size(); i++) {
sum[bid][i] = plusM(sum[bid][i], sum[bid][i - 1]);
}
}
int main() {
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i], a[i]--;
dp.resize(n + 1);
pos.resize(n);
int B = n / SIZE + 1;
offset.resize(B);
sum.resize(B);
vector<vector<int>> prev(n, vector<int>(2, -1));
dp[0] = 1;
pos[0] = 0;
for (int i = 1; i <= n; i++) {
int bid = i / SIZE;
int sz = prev[a[i - 1]].size();
int pre2 = prev[a[i - 1]][sz - 2];
int pre1 = prev[a[i - 1]][sz - 1];
for (int j = 0; j < bid; j++) dec(j, pre2 + 1, pre1 + 1);
for (int j = 0; j < bid; j++) inc(j, pre1 + 1, i);
int l = max(bid * SIZE, pre2 + 1);
int r = min(i, pre1 + 1);
for (int j = l; j < r; j++) pos[j]--;
l = max(bid * SIZE, pre1 + 1);
r = i;
for (int j = l; j < r; j++) pos[j]++;
int sum = 0;
for (int j = 0; j < bid; j++) sum = plusM(sum, calc(j));
for (int j = bid * SIZE; j < i; j++)
if (pos[j] <= k) sum = plusM(sum, dp[j]);
dp[i] = sum;
prev[a[i - 1]].push_back(i - 1);
if ((i + 1) % SIZE == 0) {
makeBlock(bid);
}
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, a[100001], pos[100001], pre[100001], b[100001], dp[100001],
c[((100001 / 320) + 1)][2 * 320 + 1], s[((100001 / 320) + 1)];
int GetBlockNumber(int i) { return i / 320 + (i % 320 != 0); }
void UpdateBlock(int k) {
s[k] = 0;
for (int i = 0; i <= 2 * 320; ++i) {
c[k][i] = 0;
}
int l = (k - 1) * 320 + 1, r = min(n, l + 320 - 1), &x = s[k], y = 320;
for (int i = r; i >= l; --i) {
x += b[i];
y += b[i];
c[k][y] = (c[k][y] + dp[i - 1]) % 998244353;
}
for (int i = 1; i <= 2 * 320; ++i) {
c[k][i] = (c[k][i] + c[k][i - 1]) % 998244353;
}
}
void Update(int i, int x, int curBlockNo) {
if (i == 0) {
return;
}
b[i] = x;
int iBlockNo = GetBlockNumber(i);
if (iBlockNo != curBlockNo) {
UpdateBlock(iBlockNo);
}
}
int Query(int i) {
int ans = 0, x = 0, iBlockNo = GetBlockNumber(i);
int l = (iBlockNo - 1) * 320 + 1;
for (; i >= l; --i) {
x += b[i];
if (x <= m) {
ans = (ans + dp[i - 1]) % 998244353;
}
}
for (--iBlockNo; iBlockNo > 0; x += s[iBlockNo], --iBlockNo) {
int y = 320 + m - x;
if (y < 0) {
continue;
}
y = min(2 * 320, y);
ans = (ans + c[iBlockNo][y]) % 998244353;
}
return ans;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
int iBlockNo = GetBlockNumber(i);
pre[i] = pos[a[i]];
Update(pre[pre[i]], 0, iBlockNo);
Update(pre[i], -1, iBlockNo);
b[i] = 1;
dp[i] = Query(i);
pos[a[i]] = i;
if (i % 320 == 0) {
UpdateBlock(iBlockNo);
}
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int t = 399, mod = 998244353;
inline int mo(const register int x) { return x >= mod ? x - mod : x; }
int a[100010], n, k, bl[100010], p[100010], pre[100010], sum[400][100010],
f[400], S[400], dp[100010], lst[100010];
inline void rebuild(const register int x, const register int l,
const register int r, const register int y) {
for (int i = (x - 1) * t + 1; i <= min(n, x * t); i++)
sum[x][p[i]] = 0, p[i] += k - f[x];
for (int i = l; i <= r; i++) p[i] += y;
S[x] = 0;
for (int i = (x - 1) * t + 1; i <= min(n, x * t); i++)
sum[x][p[i]] = mo(sum[x][p[i]] + dp[i]),
S[x] = mo(S[x] + (p[i] <= k) * dp[i]);
f[x] = k;
}
inline void ad(const register int l, const register int r,
const register int x) {
if (bl[l] == bl[r]) {
rebuild(bl[l], l, r, x);
return;
}
rebuild(bl[l], l, bl[l] * t, x);
rebuild(bl[r], (bl[r] - 1) * t + 1, r, x);
for (int i = bl[l] + 1; i < bl[r]; i++) {
f[i] -= x;
if (x > 0) {
if (f[i] + 1 >= 0 && f[i] + 1 < n)
S[i] = mo(S[i] - sum[i][f[i] + 1] + mod);
} else if (f[i] >= 0 && f[i] < n)
S[i] = mo(S[i] + sum[i][f[i]]);
}
}
int main() {
scanf("%d%d", &n, &k);
n++;
for (int i = 1; i <= n; i++) bl[i] = (i - 1) / t + 1;
for (int i = 1; i <= bl[n]; i++) f[i] = k;
for (int i = 2; i <= n; i++) scanf("%d", &a[i]), pre[a[i]] = 1;
dp[1] = 1;
ad(1, 1, 0);
for (int i = 2; i <= n; i++) {
if (pre[a[i]] - 1) ad(lst[pre[a[i]]], pre[a[i]] - 1, -1);
ad(pre[a[i]], i - 1, 1);
lst[i] = pre[a[i]];
pre[a[i]] = i;
for (int j = 1; j <= bl[n]; j++) dp[i] = mo(dp[i] + S[j]);
ad(i, i, 0);
}
cout << dp[n] << endl;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
int n, k;
inline int ad(int& x, int& y) { return (x + y > P ? x + y - P : x + y); }
int A[200006], las[200006], pr[200006];
const int blo = 200;
int bel[200006];
pair<int, int> B[200006];
int S[200006];
int val[200006], gv[200006], lz[200006], cur;
void rebuild(int b) {
int l = b * blo - blo + 1, r = b * blo;
for (int i = (l), iend = (r); i <= iend; ++i) val[i] = val[i] + lz[b];
lz[b] = 0;
for (int i = (l), iend = (r); i <= iend; ++i) B[i] = make_pair(val[i], gv[i]);
sort(B + l, B + r + 1);
S[l] = B[l].second;
for (int i = (l + 1), iend = (r); i <= iend; ++i)
S[i] = ad(S[i - 1], B[i].second);
}
void add(int l, int r, int c) {
++l, ++r;
if (bel[l] == bel[r]) {
int b = bel[l];
for (int i = (l), iend = (r); i <= iend; ++i) val[i] += c;
rebuild(b);
return;
}
for (int i = (l), iend = (bel[l] * blo); i <= iend; ++i) val[i] += c;
rebuild(bel[l]);
for (int i = (r), iend = (bel[r] * blo - blo + 1); i >= iend; --i)
val[i] += c;
rebuild(bel[r]);
for (int i = (bel[l] + 1), iend = (bel[r] - 1); i <= iend; ++i) lz[i] += c;
}
int que() {
int as = 0;
for (int b = (1), bend = (bel[cur]); b <= bend; ++b) {
int l = (b - 1) * blo + 1, r = b * blo;
int ps =
upper_bound(B + l, B + r + 1, make_pair(k - lz[b], 0x3f3f3f3f)) - B - 1;
if (ps < l)
continue;
else
as = ad(as, S[ps]);
}
return as;
}
void gt(int p, int c) {
++p;
gv[p] = c;
rebuild(bel[p]);
}
int dp[200006];
void solve() {
cin >> n >> k;
for (int i = (1), iend = (n); i <= iend; ++i)
scanf("%d", A + i), las[i] = pr[A[i]], pr[A[i]] = i;
for (int i = (1), iend = (n + 1); i <= iend; ++i) bel[i] = (i - 1) / blo + 1;
dp[0] = 1;
for (int i = (1), iend = (n); i <= iend; ++i) {
cur = i + 1;
add(las[i] + 1, i, 1);
if (las[i]) add(las[las[i]] + 1, las[i], -1);
gt(i, dp[i - 1]);
dp[i] = que();
}
printf("%d\n", dp[n]);
}
signed main() { solve(); }
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
namespace ywy {
inline int get() {
int n = 0;
char c;
while ((c = getchar()) || 23333) {
if (c >= '0' && c <= '9') break;
if (c == '-') goto s;
}
n = c - '0';
while ((c = getchar()) || 23333) {
if (c >= '0' && c <= '9')
n = n * 10 + c - '0';
else
return (n);
}
s:
while ((c = getchar()) || 23333) {
if (c >= '0' && c <= '9')
n = n * 10 - c + '0';
else
return (n);
}
}
long long dp[100001];
int val[290][200001], tag[100001], f[100001], pre[100001], ints[100001],
ld[100001];
long long ans = 0;
int k, n;
inline void add(int l, int r, int num) {
int lbd = l / 350, rbd = r / 350;
if (lbd == rbd) {
for (register int i = l; i <= r; i++) {
if (f[i] + tag[lbd] == k + 1 && num == -1)
ans = (ans + dp[i]) % 998244353;
if (f[i] + tag[lbd] == k && num == 1)
ans = (ans + 998244353 - dp[i]) % 998244353;
val[lbd][f[i] + 100000] =
(val[lbd][f[i] + 100000] + 998244353 - dp[i]) % 998244353;
f[i] += num;
val[lbd][f[i] + 100000] = (val[lbd][f[i] + 100000] + dp[i]) % 998244353;
}
return;
}
for (register int i = l; i < (lbd + 1) * 350; i++) {
if (f[i] + tag[lbd] == k + 1 && num == -1) ans = (ans + dp[i]) % 998244353;
if (f[i] + tag[lbd] == k && num == 1)
ans = (ans + 998244353 - dp[i]) % 998244353;
val[lbd][f[i] + 100000] =
(val[lbd][f[i] + 100000] + 998244353 - dp[i]) % 998244353;
f[i] += num;
val[lbd][f[i] + 100000] = (val[lbd][f[i] + 100000] + dp[i]) % 998244353;
}
for (register int i = rbd * 350; i <= r; i++) {
if (f[i] + tag[rbd] == k + 1 && num == -1) ans = (ans + dp[i]) % 998244353;
if (f[i] + tag[rbd] == k && num == 1)
ans = (ans + 998244353 - dp[i]) % 998244353;
val[rbd][f[i] + 100000] =
(val[rbd][f[i] + 100000] + 998244353 - dp[i]) % 998244353;
f[i] += num;
val[rbd][f[i] + 100000] = (val[rbd][f[i] + 100000] + dp[i]) % 998244353;
}
for (register int i = lbd + 1; i < rbd; i++) {
if (num == 1)
ans = (ans + 998244353 - val[i][k - tag[i] + 100000]) % 998244353;
if (num == -1) ans = (ans + val[i][k - tag[i] + 100001]) % 998244353;
tag[i] += num;
}
}
void ywymain() {
n = get();
k = get();
ans = 1;
dp[0] = 1;
val[0][100000] = 1;
for (register int i = 1; i <= n; i++) {
ints[i] = get();
pre[i] = ld[ints[i]];
ld[ints[i]] = i;
}
for (register int i = 1; i <= n; i++) {
add(pre[i], i - 1, 1);
if (pre[i]) add(pre[pre[i]], pre[i] - 1, -1);
dp[i] = ans;
ans = (ans + dp[i]) % 998244353;
val[i / 350][100000] = (val[i / 350][100000] + dp[i]) % 998244353;
}
cout << dp[n] << endl;
}
} // namespace ywy
int main() {
ywy::ywymain();
return (0);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "URDL";
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
assert(y >= 0);
x += y;
if (x >= mod) x -= mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
int a[100035], pre[100035], pos[100035], dp[100035];
int SZ, ps[320][100035], S, add[320];
int pfx[100035];
void bd(int bid, int pos, int val) {
int z = pfx[pos];
addmod(ps[bid][z], 998244353 - dp[pos - 1]);
if (z + add[bid] <= k) addmod(S, 998244353 - dp[pos - 1]);
pfx[pos] += val;
z = pfx[pos];
addmod(ps[bid][z], dp[pos - 1]);
if (z + add[bid] <= k) addmod(S, dp[pos - 1]);
}
int gid(int pos) { return (pos + SZ - 1) / SZ; }
void upt(int l, int r, int val) {
int lid = gid(l), rid = gid(r);
if (lid == rid) {
for (int i = l; i <= r; i++) bd(lid, i, val);
return;
}
for (int i = l; i <= n; i++) {
bd(lid, i, val);
if (i % SZ == 0) break;
}
for (int z = lid + 1; z < rid; z++) {
if (val == 1) {
if (k >= add[z]) addmod(S, 998244353 - ps[z][k - add[z]]);
add[z]++;
} else {
if (k + 1 >= add[z]) addmod(S, ps[z][k + 1 - add[z]]);
add[z]--;
}
}
for (int i = (rid - 1) * SZ + 1; i <= r; i++) {
bd(rid, i, val);
}
}
void fmain(int tid) {
scanf("%d%d", &n, &k);
for (int(i) = 1; (i) <= (int)(n); (i)++) {
scanf("%d", a + i);
pre[i] = pos[a[i]];
pos[a[i]] = i;
}
dp[0] = 1;
S = 1;
SZ = sqrt(n);
ps[1][0] = 1;
for (int(i) = 1; (i) <= (int)(n); (i)++) {
upt(pre[i] + 1, i, 1);
if (pre[i]) upt(pre[pre[i]] + 1, pre[i], -1);
dp[i] = S;
int z = gid(i + 1);
addmod(ps[z][0], S);
addmod(S, S);
}
printf("%d\n", dp[n]);
}
int main() {
int t = 1;
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int f[100010], val[100010], sep[100010], cnt, k, h[100010], last[100010], n;
int read() {
int tmp = 0;
char c = getchar();
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') {
tmp = tmp * 10 + c - '0';
c = getchar();
}
return tmp;
}
struct block {
int l, r, tag, xs[320], val[320], diff[320], pre[320], cur, cnt;
int id[2][320], merge[320];
void change(int tl, int tr, int c) {
int sum[2], p[2], now = 0;
sum[0] = sum[1] = 0;
p[0] = p[1] = 1;
for (int i = 1; i <= r - l + 1; i++)
if (merge[i] >= tl - l + 1 && merge[i] <= tr - l + 1) {
id[1][++sum[1]] = merge[i];
xs[merge[i]] += c;
} else
id[0][++sum[0]] = merge[i];
while (p[0] <= sum[0] || p[1] <= sum[1]) {
int ch;
if (p[0] > sum[0])
ch = 1;
else if (p[1] > sum[1])
ch = 0;
else if (xs[id[0][p[0]]] < xs[id[1][p[1]]])
ch = 0;
else
ch = 1;
merge[++now] = id[ch][p[ch]];
p[ch]++;
}
cnt = 0;
for (int i = 1; i <= r - l + 1; i++)
if (i != 1 && xs[merge[i]] == xs[merge[i - 1]])
pre[cnt] = (pre[cnt] + val[merge[i]]) % 998244353;
else {
cnt++;
diff[cnt] = xs[merge[i]];
pre[cnt] = val[merge[i]];
}
cur = 0;
for (int i = 1; i <= cnt; i++) {
if (diff[i] + tag <= k) cur = i;
pre[i] = (pre[i - 1] + pre[i]) % 998244353;
}
}
void update() {
if (cur && diff[cur] + tag > k) cur--;
if (cur != cnt && diff[cur + 1] + tag <= k) cur++;
}
} B[320];
void modify(int l, int r, int x) {
if (sep[l] == sep[r]) {
B[sep[l]].change(l, r, x);
return;
}
for (int i = sep[l] + 1; i <= sep[r] - 1; i++) {
B[i].tag += x;
B[i].update();
}
B[sep[l]].change(l, B[sep[l]].r, x);
B[sep[r]].change(B[sep[r]].l, r, x);
}
int main() {
n = read();
k = read();
int S = sqrt(n);
for (int i = 1; i <= n; i++) val[i] = read();
for (int cur = 0; cur <= n; cur += S) {
int nxt = min(cur + S - 1, n);
cnt++;
B[cnt].l = cur;
B[cnt].r = nxt;
for (int i = cur; i <= nxt; i++) {
sep[i] = cnt;
B[cnt].merge[i - cur + 1] = i - cur + 1;
}
B[cnt].cnt = B[cnt].cur = 1;
}
f[0] = 1;
B[sep[0]].val[1] = f[0];
B[1].change(1, 0, 0);
for (int i = 1; i <= n; i++) {
last[i] = h[val[i]];
h[val[i]] = i;
int tmp = last[i];
modify(tmp, i - 1, 1);
if (tmp != 0) modify(last[tmp], tmp - 1, -1);
for (int j = 1; j <= sep[i] - 1; j++)
f[i] = (f[i] + B[j].pre[B[j].cur]) % 998244353;
int c = sep[i];
for (int j = B[c].l; j <= i - 1; j++)
if (B[c].tag + B[c].xs[j - B[c].l + 1] <= k)
f[i] = (f[i] + f[j]) % 998244353;
B[c].val[i - B[c].l + 1] = f[i];
B[c].change(i, i, 0);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
using namespace std;
template <class T>
bool umin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool umax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
int a[101001];
vector<int> positions[101010];
int tr[404040];
int b[101010];
int dp[404040];
long long getSum(int* b, int* dp, int n) {
long long res = 0;
for (int j = 0; j < n; ++j) {
res += b[j] > 0 ? 0 : dp[j];
}
return res;
}
const int fftmod = 998244353;
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < (n); ++i) {
b[i] -= k;
}
for (int i = 0; i < (n); ++i) {
cin >> a[i];
--a[i];
positions[i].push_back(-1);
positions[i].push_back(-1);
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
int x = a[i];
{
int to = positions[x].back();
int from = *++positions[x].rbegin() + 1;
for (int j = from; j <= to; ++j) {
b[j]--;
}
}
int from = positions[x].back() + 1;
for (int j = from; j <= i; ++j) {
b[j]++;
}
long long res = 0;
for (int j = 0; j < i + 1; ++j) res += b[j] > 0 ? 0 : dp[j];
dp[i + 1] = res % fftmod;
positions[x].push_back(i);
}
cout << dp[n] << endl;
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MX_N = 1e5 + 5;
const int MX_K = 1e5 + 5;
const int MX_A = MX_N;
const int MOD = 998244353;
const int B = 300;
int N, K, A[MX_N];
int pos[MX_A], prv[MX_N], dp[MX_N];
struct Bucket {
int pre[B], cnt[B], offset;
Bucket() {
for (int i = (0); i <= (B - 1); ++i) {
pre[i] = 0;
cnt[i] = 0;
}
offset = 0;
}
void rebuild(int b) {
int mn = MX_N;
int j = b * B;
for (int i = (0); i <= (B - 1); ++i) {
pre[i] = 0;
mn = min(mn, cnt[i]);
}
offset += mn;
for (int i = (0); i <= (B - 1); ++i) {
cnt[i] -= mn;
pre[cnt[i]] += dp[i + j];
if (pre[cnt[i]] >= MOD) pre[cnt[i]] -= MOD;
}
for (int i = (1); i <= (B - 1); ++i) {
pre[i] += pre[i - 1];
if (pre[i] >= MOD) pre[i] -= MOD;
}
}
int sum() {
if (offset > K) return 0;
return pre[min(B - 1, K - offset)];
}
int get(int b, int i) {
if (offset + cnt[i] > K) return 0;
return dp[b * B + i];
}
} bucket[MX_N / B + 1];
void update(int x, int y, int v) {
if (x > y) return;
int bx = x / B, jx = x % B, by = y / B, jy = y % B;
if (bx == by) {
for (int i = (jx); i <= (jy); ++i) {
bucket[bx].cnt[i] += v;
}
bucket[bx].rebuild(bx);
} else {
for (int i = (jx); i <= (B - 1); ++i) {
bucket[bx].cnt[i] += v;
}
bucket[bx].rebuild(bx);
for (int i = (bx + 1); i <= (by - 1); ++i) {
bucket[i].offset += v;
}
for (int i = (0); i <= (jy); ++i) {
bucket[by].cnt[i] += v;
}
bucket[by].rebuild(by);
}
}
int query(int i) {
int b = i / B, j = i % B;
int sum = 0;
for (int k = (0); k <= (b - 1); ++k) {
sum += bucket[k].sum();
if (sum >= MOD) sum -= MOD;
}
for (int k = (0); k <= (j); ++k) {
sum += bucket[b].get(b, k);
if (sum >= MOD) sum -= MOD;
}
return sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> K;
for (int i = (1); i <= (N); ++i) {
cin >> A[i];
prv[i] = pos[A[i]];
pos[A[i]] = i;
}
dp[0] = 1;
bucket[0].rebuild(0);
for (int i = (1); i <= (N); ++i) {
update(prv[prv[i]], prv[i] - 1, -1);
update(prv[i], i - 1, 1);
dp[i] = query(i - 1);
int b = i / B;
bucket[b].rebuild(b);
}
cout << dp[N];
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void ckmin(T& a, T b) {
if (b < a) a = b;
}
template <class T>
inline void ckmax(T& a, T b) {
if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
return x * x;
}
const int MOD = 998244353;
inline int ADD(int a, int b) {
return (a + b >= MOD) ? (a + b - MOD) : (a + b);
}
inline void ADDTO(int& a, int b) {
a = ((a + b >= MOD) ? (a + b - MOD) : (a + b));
}
inline int SUB(int a, int b) { return (a >= b) ? (a - b) : (a - b + MOD); }
inline void SUBTO(int& a, int b) { a = ((a >= b) ? (a - b) : (a - b + MOD)); }
inline int MUL(int a, int b) { return (long long)a * b % MOD; }
const int maxsize = 200000 + 16;
struct SArray {
int total = 0;
vector<int> s;
void reset() {
total = 0;
s = vector<int>(16);
}
int get(int i) {
if (i <= 0) return 0;
if (i >= ((int)s.size())) return total;
int r = 0;
for (; i; i &= (i - 1)) ADDTO(r, s[i]);
return r;
}
void update(int key, int delta) {
delta = (delta % MOD + MOD) % MOD;
while (key >= ((int)s.size())) {
s.push_back(0);
int at = ((int)s.size()) - 1;
s[at] = SUB(total, get(at & (at - 1)));
}
for (int i = key; i < ((int)s.size()); i = (i | (i - 1)) + 1)
ADDTO(s[i], delta);
ADDTO(total, delta);
}
};
int M;
int m;
SArray sum_f[maxsize];
int block_e[maxsize];
int e[maxsize];
int f[maxsize];
void init(int n) {
M = (int)(sqrt(n + 1) + 1);
m = n / M + 1;
for (int i = 0; i < (n + 1); ++i) e[i] = 1;
for (int i = 0; i < (m); ++i) {
sum_f[i].reset();
block_e[i] = 0;
}
for (int i = 0; i < (n + 1); ++i) f[i] = 0;
}
void update_one(int key, int delta) {
int w = key / M;
sum_f[w].update(e[key], -f[key]);
e[key] += delta;
sum_f[w].update(e[key], f[key]);
}
void update_one_block(int idx, int delta) { block_e[idx] += delta; }
void update_interval(int s, int t, int delta) {
if (s > t) return;
for (; s <= t && s % M != 0; s++) update_one(s, delta);
for (; s <= t && t % M != M - 1; t--) update_one(t, delta);
for (; s <= t; s += M) update_one_block(s / M, delta);
}
int main() {
std::ios::sync_with_stdio(false);
int n, limit;
while (cin >> n >> limit) {
vector<int> a(n);
for (int i = 0; i < (n); ++i) {
cin >> a[i];
--a[i];
}
vector<int> p_val(n, -1), p_idx(n, -1);
init(n);
f[0] = 1;
sum_f[0].update(1, 1);
for (int i = 0; i < (n); ++i) {
p_idx[i] = p_val[a[i]];
p_val[a[i]] = i;
if (p_idx[i] >= 0) update_interval(p_idx[p_idx[i]] + 1, p_idx[i], -1);
update_interval(p_idx[i] + 1, i, 1);
f[i + 1] = 0;
for (int k = 0; k < (m); ++k)
ADDTO(f[i + 1], sum_f[k].get(limit - block_e[k] + 1));
sum_f[(i + 1) / M].update(1, f[i + 1]);
}
printf("%d\n", f[n]);
break;
}
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int N = 100005;
const int B = 620;
int n, k, a[N];
int dp[N];
int cnt[N];
vector<int> occ[N];
int delta[B];
vector<int> vet[B];
vector<int> psum[B];
void add(int &aa, int b) { aa = aa + b >= mod ? aa + b - mod : aa + b; }
void rebuild(int id) {
sort(vet[id].begin(), vet[id].end(),
[=](int q, int w) { return cnt[q] < cnt[w]; });
for (int i = 0; i < (int)vet[id].size(); i++) {
if (!i)
psum[id][i] = 0;
else
psum[id][i] = psum[id][i - 1];
add(psum[id][i], dp[vet[id][i] - 1]);
}
}
void add(int L, int R, int val) {
int ida = L / B, idb = R / B;
if (ida == idb) {
for (int i = L; i <= R; i++) cnt[i] += val;
rebuild(ida);
return;
}
for (int i = L; ida == i / B; i++) cnt[i] += val;
for (int i = idb * B; i <= R; i++) cnt[i] += val;
rebuild(ida);
rebuild(idb);
for (int i = ida + 1; i < idb; i++) delta[i] += val;
}
int get(int id) {
vector<int> &v = vet[id];
int L = 0, R = (int)v.size() - 1, ans = (int)v.size();
while (L <= R) {
int mid = (L + R) / 2;
if (cnt[v[mid]] + delta[id] > k) {
ans = mid;
R = mid - 1;
} else
L = mid + 1;
}
if (!ans) return 0;
return psum[id][ans - 1];
}
int query(int lim) {
int ans = 0;
for (int id = 0; id <= lim / B; id++) {
add(ans, get(id));
}
return ans;
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", a + i);
for (int i = 1; i <= n; i++) {
occ[i].push_back(0);
occ[i].push_back(0);
vet[i / B].push_back(i);
psum[i / B].push_back(0);
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
vector<int> &v = occ[a[i]];
add(v[v.size() - 2] + 1, v.back(), -1);
add(v.back() + 1, i, +1);
occ[a[i]].push_back(i);
dp[i] = query(i);
}
cout << dp[n] << endl;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, Siz, num[100010], pos[100010], bel[100010], lst[100010], f[100010],
cnt[100010], delta[100010], ans[100010], sum[440][100010];
void update(int u, int v) {
int t = bel[u];
sum[t][cnt[u]] = (sum[t][cnt[u]] - f[u] + 998244353) % 998244353;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] - f[u] + 998244353) % 998244353;
cnt[u] += v, sum[t][cnt[u]] = (sum[t][cnt[u]] + f[u]) % 998244353;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] + f[u]) % 998244353;
}
void add(int u, int v, int w) {
if (u > v) return;
int p = bel[u], q = bel[v];
if (p + 1 >= q) {
for (int i = u; i <= v; i++) update(i, w);
return;
}
for (int i = u; bel[i] == p; i++) update(i, w);
for (int i = v; bel[i] == q; i--) update(i, w);
for (int i = p + 1; i < q; i++) {
if (w > 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] - sum[i][m - delta[i]] + 998244353) % 998244353;
delta[i] += w;
if (w < 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] + sum[i][m - delta[i]]) % 998244353;
}
}
int main() {
scanf("%d%d", &n, &m), Siz = sqrt(n);
for (int i = 0; i <= n; i++) bel[i] = i / Siz + 1;
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
f[0] = sum[1][0] = ans[1] = 1;
for (int i = 1; i <= n; i++) {
lst[i] = pos[num[i]], add(lst[lst[i]], lst[i] - 1, -1),
add(lst[i], i - 1, 1);
for (int j = i - 1; (~j) && bel[j] == bel[i]; j--)
if (cnt[j] + delta[bel[i]] <= m) (f[i] += f[j]) %= 998244353;
for (int j = bel[i] - 1; j; j--) (f[i] += ans[j]) %= 998244353;
sum[bel[i]][0] = (sum[bel[i]][0] + f[i]) % 998244353;
if (delta[bel[i]] <= m) ans[bel[i]] = (ans[bel[i]] + f[i]) % 998244353;
pos[num[i]] = i;
}
printf("%d\n", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
namespace zyt {
template <typename T>
inline bool read(T &x) {
char c;
bool f = false;
x = 0;
do c = getchar();
while (c != EOF && c != '-' && !isdigit(c));
if (c == EOF) return false;
if (c == '-') f = true, c = getchar();
do x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f) x = -x;
return true;
}
template <typename T>
inline void write(T x) {
static char buf[20];
char *pos = buf;
if (x < 0) putchar('-'), x = -x;
do *pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf) putchar(*--pos);
}
const int N = 1e5 + 10, S = 320, P = 998244353;
int n, k, _sum[S][S << 1], *sum[S], dp[N], pre[N], last[N];
int block, blnum, begin[S], belong[N], w[N], tot[S];
void add(int &a, const int b) { a = (a + b) % P; }
void change(const int pos, const int x) {
if (!pos) return;
int b = belong[pos];
w[pos] = x;
int tmp = 0;
for (int i = -block; i <= block; i++) sum[b][i] = 0;
for (int i = begin[b + 1] - 1; i >= begin[b]; i--) {
tmp += w[i];
add(sum[b][tmp], dp[i - 1]);
}
tot[b] = tmp;
for (int i = -block + 1; i <= block; i++) add(sum[b][i], sum[b][i - 1]);
}
int work() {
read(n), read(k);
block = sqrt(n), blnum = ceil(double(n) / block);
dp[0] = 1;
for (int i = 1; i <= n; i++) belong[i] = (i - 1) / block + 1;
for (int i = 1; i <= blnum; i++)
begin[i] = (i - 1) * block + 1, sum[i] = _sum[i] + block + 1;
begin[blnum + 1] = n + 1;
for (int i = 1; i <= n; i++) {
int a, tmp = 0;
read(a);
change(pre[last[a]], 0);
change(pre[i] = last[a], -1);
change(last[a] = i, 1);
dp[i] = 0;
for (int j = i; j >= begin[belong[i]]; j--)
if ((tmp += w[j]) <= k) add(dp[i], dp[j - 1]);
for (int j = belong[i] - 1; j > 0; j--) {
if (k - tmp >= -block) add(dp[i], sum[j][min(block, k - tmp)]);
tmp += tot[j];
}
}
write(dp[n]);
return 0;
}
} // namespace zyt
int main() { return zyt::work(); }
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,popcnt")
using namespace std;
int a[100007], bel[100007], pre[100007], pos[100007], tag[293], delt[100007], n,
k, S;
int f[100007], sum[293][100007 * 2 + 1], ans[293];
const int p = 998244353;
template <class T>
void read(T &x) {
char ch = x = 0;
bool fl = false;
while (!isdigit(ch)) fl |= ch == '-', ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x = fl ? -x : x;
}
void add(int &x, const int &y) { x = (x + y) % p; }
void ins(int x, int y, long long val) {
int id = bel[x];
add(sum[id][(delt[x] = y - tag[id]) + 100007], val);
if (y <= k) add(ans[id], val);
}
void build(int id) {
for (int i = (id - 1) * S; i < id * S; i++) {
add(sum[id][delt[i] + 100007], p - f[i]);
delt[i] += tag[id];
add(sum[id][delt[i] + 100007], f[i]);
}
tag[id] = 0;
}
void upd(int l, int r, int val) {
if (bel[l] == bel[r]) {
build(bel[l]);
for (int i = l; i <= r; i++) {
add(sum[bel[l]][delt[i] + 100007], p - f[i]);
if (~val && delt[i] == k) add(ans[bel[l]], p - f[i]);
if (!~val && delt[i] == k + 1) add(ans[bel[l]], f[i]);
delt[i] += val;
add(sum[bel[l]][delt[i] + 100007], f[i]);
}
} else {
build(bel[l]);
for (int i = l; i < bel[l] * S; i++) {
add(sum[bel[l]][delt[i] + 100007], p - f[i]);
if (~val && delt[i] == k) add(ans[bel[l]], p - f[i]);
if (!~val && delt[i] == k + 1) add(ans[bel[l]], f[i]);
delt[i] += val;
add(sum[bel[l]][delt[i] + 100007], f[i]);
}
build(bel[r]);
for (int i = (bel[r] - 1) * S; i <= r; i++) {
add(sum[bel[r]][delt[i] + 100007], p - f[i]);
if (~val && delt[i] == k) add(ans[bel[r]], p - f[i]);
if (!~val && delt[i] == k + 1) add(ans[bel[r]], f[i]);
delt[i] += val;
add(sum[bel[r]][delt[i] + 100007], f[i]);
}
for (int i = bel[l] + 1; i < bel[r]; i++) {
if (~val)
add(ans[i], p - sum[i][k - tag[i] + 100007]);
else
add(ans[i], sum[i][k + 1 - tag[i] + 100007]);
tag[i] += val;
}
}
}
int que(int r) {
int ret = 0;
for (int i = 1; i < bel[r]; i++) {
add(ret, ans[i]);
}
build(bel[r]);
for (int i = (bel[r] - 1) * S; i <= r; i++) {
if (delt[i] <= k) add(ret, f[i]);
}
return ret;
}
int main() {
read(n), read(k), S = max((int)sqrt(n), n / 289);
for (int i = 1; i <= n; i++) {
read(a[i]), pre[i] = pos[a[i]], pos[a[i]] = i;
}
for (int i = 0; i <= n; i++) {
bel[i] = i / S + 1;
}
ins(0, 0, f[0] = 1);
for (int i = 1; i <= n; i++) {
upd(pre[i], i - 1, 1);
if (pre[i]) upd(pre[pre[i]], pre[i] - 1, -1);
f[i] = que(i - 1), ins(i, 0, f[i]);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int __i__, __j__;
class _Debug {
public:
template <typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
int n, k;
int a[100000], p[100000];
int last[100000];
int dp[100001];
int num[100001];
int sum[350][100100], shift[350];
int sumall = 0;
int prop(int b) {
if (shift[b] == 0) return 0;
int i;
for (i = b * 300; i < min((b + 1) * 300, n); i++) {
sum[b][num[i]] += 998244353 - dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
num[i] += shift[b];
sum[b][num[i]] += dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
}
shift[b] = 0;
return 0;
}
int add(int i, int d) {
int b = i / 300;
sum[b][num[i]] += 998244353 - dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += 998244353 - dp[i];
if (sumall >= 998244353) sumall -= 998244353;
num[i] += d;
sum[b][num[i]] += dp[i];
if (sum[b][num[i]] >= 998244353) sum[b][num[i]] -= 998244353;
if (num[i] <= k) sumall += dp[i];
if (sumall >= 998244353) sumall -= 998244353;
return 0;
}
int update(int s, int e, int d) {
int i;
if (s / 300 == e / 300) {
int b = s / 300;
prop(b);
for (i = s; i <= e; i++) add(i, d);
} else {
int b = s / 300;
prop(b);
for (i = s; i < (b + 1) * 300; i++) add(i, d);
for (i = b + 1; i < e / 300; i++) {
if ((d == 1) && (k >= shift[i]))
sumall += 998244353 - sum[i][k - shift[i]];
else if ((d == -1) && (k + 1 >= shift[i]))
sumall += sum[i][k + 1 - shift[i]];
if (sumall >= 998244353) sumall -= 998244353;
shift[i] += d;
}
b = e / 300;
prop(b);
for (i = b * 300; i <= e; i++) add(i, d);
}
return 0;
}
int main() {
int i;
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++) scanf("%d", &a[i]), a[i]--;
for (i = 0; i < n; i++) last[i] = -1;
for (i = 0; i < n; i++) {
p[i] = last[a[i]];
last[a[i]] = i;
}
dp[0] = 1;
sum[0][shift[0]] = 1, sumall = 1;
for (i = 0; i < n; i++) {
update(p[i] + 1, i, 1);
if (p[i] != -1) update(p[p[i]] + 1, p[i], -1);
int b = (i + 1) / 300;
prop(b);
dp[i + 1] = sumall;
sum[b][num[i + 1]] += dp[i + 1];
sumall += dp[i + 1];
if (sum[b][num[i + 1]] >= 998244353) sum[b][num[i + 1]] -= 998244353;
if (sumall >= 998244353) sumall -= 998244353;
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int b = 325;
int dp[100005];
int a[100005], cnt[100005];
int s[100005], e[100005];
vector<int> v[100005];
struct bucket {
int val[2 * b + 5];
int sum = 0;
void rebuild(int s, int e) {
for (int i = 0; i <= 2 * b; i++) val[i] = 0;
sum = 0;
for (int i = e; i >= s; i--) {
sum += cnt[i];
val[b + sum] = (val[b + sum] + dp[i - 1]) % mod;
}
for (int i = 1; i <= 2 * b; i++) val[i] = (val[i] + val[i - 1]) % mod;
}
} bucket[b + 5];
int main() {
int n, k, a;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
int bi = i / b;
e[bi] = i;
if (s[bi] == 0) s[bi] = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
scanf("%d", &a);
if (v[a].size() >= 1) {
cnt[v[a][v[a].size() - 1]] = -1;
int idx = (v[a][v[a].size() - 1]) / b;
bucket[idx].rebuild(s[idx], e[idx]);
if (v[a].size() >= 2) {
cnt[v[a][v[a].size() - 2]] = 0;
int idx = (v[a][v[a].size() - 2]) / b;
bucket[idx].rebuild(s[idx], e[idx]);
}
}
v[a].push_back(i);
cnt[i] = 1;
int idx = i / b;
bucket[idx].rebuild(s[idx], e[idx]);
int l = 1, r = i;
int bl = l / b, br = r / b;
if (bl == br) {
int sum = 0;
for (int j = r; j >= l; j--) {
sum += cnt[j];
if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod;
}
} else {
int sum = 0;
for (int j = r; j >= s[br]; j--) {
sum += cnt[j];
if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod;
}
for (int j = br - 1; j >= bl + 1; j--) {
int d = min(2 * b, k - sum + b);
if (d >= 0) dp[i] = (dp[i] + bucket[j].val[d]) % mod;
sum += bucket[j].sum;
}
for (int j = e[bl]; j >= l; j--) {
sum += cnt[j];
if (sum <= k) dp[i] = (dp[i] + dp[j - 1]) % mod;
}
}
}
printf("%d\n", dp[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int P = 998244353;
int n, m, a[N], pre[N], rec[N], f[N];
inline void add(int &x, int y) {
x += y;
if (x >= P) x -= P;
}
inline void sub(int &x, int y) {
x -= y;
if (x < 0) x += P;
}
struct BL {
int bel[N], tag[450], w[N], size, cnt, sum[450][N], ans[450];
void init() {
sum[1][0] = ans[1] = 1;
size = sqrt(n);
for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / size + 1;
cnt = bel[n];
for (int i = 1; i <= cnt; ++i) tag[i] = m;
}
void update(int pos, int x) {
int px = bel[pos];
sub(sum[px][w[pos]], f[pos - 1]);
if (w[pos] <= tag[px]) sub(ans[px], f[pos - 1]);
w[pos] = x;
add(sum[px][x], f[pos - 1]);
if (x <= tag[px]) add(ans[px], f[pos - 1]);
}
void ins(int x, int y, int d) {
if (x > y) return;
if (bel[x] + 1 >= bel[y]) {
for (int i = x; i <= y; ++i) update(i, w[i] + d);
return;
}
for (int i = x; i <= bel[x] * size; ++i) update(i, w[i] + d);
for (int i = (bel[y] - 1) * size + 1; i <= y; ++i) update(i, w[i] + d);
for (int i = bel[x] + 1; i <= bel[y] - 1; ++i) {
if (d > 0) sub(ans[i], sum[i][tag[i]]);
tag[i] -= d;
if (d < 0) add(ans[i], sum[i][tag[i]]);
}
}
int qry(int x) {
int res = 0;
for (int i = x; bel[i] == bel[x]; --i)
if (w[i] <= tag[bel[x]]) add(res, f[i - 1]);
for (int i = bel[x] - 1; i >= 1; --i) add(res, ans[i]);
return res;
}
} bl;
int main() {
scanf("%d%d", &n, &m);
bl.init();
f[0] = 1;
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
pre[i] = rec[a[i]], rec[a[i]] = i;
bl.ins(pre[pre[i]] + 1, pre[i], -1), bl.ins(pre[i] + 1, i, 1);
f[i] = bl.qry(i);
add(bl.sum[bl.bel[i + 1]][0], f[i]);
add(bl.ans[bl.bel[i + 1]], f[i]);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 1e5 + 79, s = 350, ns = maxn / s + 5;
int n, k;
vector<int> f(maxn, 0), a(maxn), dp(maxn, 0), myval(ns, 0);
vector<vector<int> > oc(maxn), sum(ns, vector<int>(s * 2 + 2, 0));
void add(int& a, const int& b) {
a += b;
if (a >= mod) a -= mod;
}
void recalculate_block(int i, int val) {
f[i] = val;
int b = i / s, l = b * s, r = min(n - 1, (b + 1) * s - 1);
myval[b] = 0;
for (int i = 0; i < sum[b].size(); i++) sum[b][i] = 0;
for (int i = r; i >= l; i--) {
myval[b] += f[i];
add(sum[b][myval[b] + s], dp[i]);
}
for (int i = 1; i < sum[b].size(); i++) add(sum[b][i], sum[b][i - 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
int ans = 0;
dp[0] = 1;
for (int r = 0; r < n; r++) {
if (oc[a[r]].size()) {
recalculate_block(oc[a[r]].back(), -1);
}
if (oc[a[r]].size() > 1) {
recalculate_block(oc[a[r]][oc[a[r]].size() - 2], 0);
}
int c = 1;
for (int l = r; l >= 0; l--) {
c += f[l];
if (c <= k) add(dp[r + 1], dp[l]);
if (l % s == 0) break;
}
for (int b = (r / s) - 1; b >= 0; b--) {
if (k - c >= -s) {
add(dp[r + 1], sum[b][min((int)sum[b].size() - 1, s + k - c)]);
}
add(c, myval[b]);
}
oc[a[r]].push_back(r);
add(ans, dp[r + 1]);
recalculate_block(r, 1);
}
cout << dp[n] << "\n";
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int MAXN = 2e5;
const int B = 315;
int cnt[MAXN], dp[MAXN];
vector<int> occ[MAXN];
void add_self(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
void min_self(int &x, int y) { x = min(x, y); }
struct SQRT {
int id, offset, pref_sum[B];
void rebuild() {
int st = id * B, dr = (id + 1) * B - 1, minn = INT_MAX;
for (int i = st; i <= dr; ++i) min_self(minn, offset + cnt[i]);
for (int i = st; i <= dr; ++i) cnt[i] -= minn - offset;
offset = minn;
for (int i = 0; i < B; ++i) pref_sum[i] = 0;
for (int i = st; i <= dr; ++i) add_self(pref_sum[cnt[i]], dp[i]);
for (int i = 1; i < B; ++i) add_self(pref_sum[i], pref_sum[i - 1]);
}
} a[MAXN / B + 1];
int get_bucket(int index) { return index / B; }
void update(int l, int r, short t) {
int bl = get_bucket(l), br = get_bucket(r);
for (int i = l; i <= r && get_bucket(i) == bl; ++i) cnt[i] += t;
a[bl].rebuild();
if (bl == br) return;
for (int i = bl + 1; i < br; ++i) a[i].offset += t;
for (int i = r; get_bucket(i) == br; --i) cnt[i] += t;
a[br].rebuild();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, k;
cin >> n >> k;
for (int i = 0; i <= get_bucket(n); ++i) a[i].id = i;
for (int i = 1; i <= n; ++i) occ[i].emplace_back(-1);
dp[0] = 1;
a[0].rebuild();
for (int r = 0; r < n; ++r) {
int x;
cin >> x;
vector<int> &vec = occ[x];
if (static_cast<int>(vec.size()) >= 2)
update(vec.end()[-2] + 1, vec.back(), -1);
update(vec.back() + 1, r, 1);
vec.emplace_back(r);
int val = 0;
for (int i = 0; i <= get_bucket(r); ++i) {
int at_most = k - a[i].offset;
if (at_most >= 0) add_self(val, a[i].pref_sum[min(at_most, B - 1)]);
}
dp[r + 1] = val;
a[get_bucket(r + 1)].rebuild();
}
cout << dp[n] << '\n';
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int tzu = 500;
int bs = 350;
int n, k;
int a[100001];
int l[100001];
int p[100001];
int dp[100001];
int v[100001];
int sum[501];
int cnt[501][1001];
int mn[501], mx[501];
int m;
void ref(int bl) {
int curv = tzu;
for (int i = mn[bl]; i <= mx[bl]; i++) cnt[bl][i] = 0;
mn[bl] = mx[bl] = tzu;
for (int i = min(m, bl * bs); i > (bl - 1) * bs; i--) {
curv += v[i];
cnt[bl][curv] += dp[i - 1];
if (cnt[bl][curv] >= mod) cnt[bl][curv] -= mod;
mn[bl] = min(mn[bl], curv);
mx[bl] = max(mx[bl], curv);
}
for (int i = mn[bl] + 1; i <= mx[bl]; i++) {
cnt[bl][i] += cnt[bl][i - 1];
if (cnt[bl][i] >= mod) cnt[bl][i] -= mod;
}
sum[bl] = curv;
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
l[i] = p[a[i]];
p[a[i]] = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
m = i;
set<int> upd;
v[i] = 1;
upd.insert((i - 1) / bs + 1);
if (l[i] != 0) {
v[l[i]] = -1;
upd.insert((l[i] - 1) / bs + 1);
if (l[l[i]] != 0) v[l[l[i]]] = 0;
upd.insert((l[l[i]] - 1) / bs + 1);
}
for (auto cur : upd) ref(cur);
int cb = (i - 1) / bs + 1;
int cs = 0;
for (int j = cb; j >= 1; j--) {
if (tzu + k - cs >= 0) dp[i] += cnt[j][min(mx[j], tzu + k - cs)];
cs += sum[j] - tzu;
if (dp[i] >= mod) dp[i] -= mod;
}
}
cout << dp[n] << endl;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
int bb[1 + 100000], dp[1 + 100000], ss[((100000 + 200 - 1) / 200)],
dq[((100000 + 200 - 1) / 200)][200 + 1 + 200];
void update(int h) {
int *qq = dq[h];
int i, t, c;
t = 0;
memset(qq, 0, (200 + 1 + 200) * sizeof *qq);
for (i = (h + 1) * 200; i > h * 200; i--) {
t += bb[i];
qq[200 + t] = (qq[200 + t] + dp[i - 1]) % 998244353;
}
for (c = 1; c <= 200 + 200; c++) qq[c] = (qq[c] + qq[c - 1]) % 998244353;
}
int main() {
static int pp[1 + 100000], ii[1 + 100000];
int n, m, k, h, i, j;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
pp[i] = ii[a];
ii[a] = i;
}
dp[0] = 1;
for (j = 1; j <= n; j++) {
int p, x, t;
m = (j - 1) / 200;
ss[m] += 1 - bb[j];
bb[j] = 1;
if ((p = pp[j])) {
h = (p - 1) / 200;
ss[h] += -1 - bb[p];
bb[p] = -1;
if (p <= m * 200) update(h);
if ((p = pp[p])) {
h = (p - 1) / 200;
ss[h] += 0 - bb[p];
bb[p] = 0;
if (p <= m * 200) update(h);
}
}
x = 0;
t = 0;
++t;
--t;
for (i = j; i > m * 200; i--) {
t += bb[i];
if (t <= k) x = (x + dp[i - 1]) % 998244353;
}
for (h = m - 1; h >= 0; h--) {
if (k - t >= -200)
x = (x + dq[h][200 + (200 < k - t ? 200 : k - t)]) % 998244353;
t += ss[h];
}
dp[j] = x;
if (j % 200 == 0) update(m);
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int Bs = 317, N = 2e5 + 10, mod = 998244353;
int ri() {
char c = getchar();
int x = 0, f = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') f = -1;
for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c;
return x * f;
}
int pr[N], las[N], a[N], val[Bs][N], sum[N], f[N], tag[Bs], B, ans, k, n;
int belong(int x) { return (x - 1) / B + 1; }
void sub(int &a, int b, int P) {
a -= b;
if (a < 0) a += P;
}
void add(int &a, int b, int P) {
a += b;
if (a >= P) a -= P;
}
inline void insert(int u, int v) {
int bu = belong(u);
sum[u] -= tag[bu];
add(ans, v, mod);
add(val[bu][sum[u] + n], v, mod);
}
void change(int u, int v) {
int bu = belong(u);
if (sum[u] + tag[bu] <= k) sub(ans, f[u - 1], mod);
sub(val[bu][sum[u] + n], f[u - 1], mod);
sum[u] += v;
if (sum[u] + tag[bu] <= k) add(ans, f[u - 1], mod);
add(val[bu][sum[u] + n], f[u - 1], mod);
}
inline void modify(int l, int r, int v) {
if (l > r) return;
int bl = belong(l), br = belong(r);
if (bl + 1 >= br) {
for (int i = l; i <= r; i++) change(i, v);
} else {
for (int i = l; i <= B * bl; i++) change(i, v);
for (int i = B * (br - 1) + 1; i <= r; i++) change(i, v);
for (int i = bl + 1; i < br; i++) {
if (~v)
sub(ans, val[i][k - tag[i] + n], mod);
else
add(ans, val[i][k - tag[i] + 1 + n], mod);
tag[i] += v;
}
}
}
int main() {
n = ri();
k = ri();
for (int i = 1, a; i <= n; ++i) a = ri(), pr[i] = las[a], las[a] = i;
B = sqrt(n);
f[0] = 1;
for (int i = 1; i <= n; ++i) {
insert(i, f[i - 1]);
modify(pr[i] + 1, i, 1);
modify(pr[pr[i]] + 1, pr[i], -1);
f[i] = ans;
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void debug_out(T t) {
cerr << t;
}
template <typename A, typename B>
void debug_out(pair<A, B> u) {
cerr << "(" << u.first << " " << u.second << ")";
}
template <typename T>
void debug_out(vector<T> t) {
int sz = t.size();
for (int i = 0; i < sz; i++) {
debug_out(t[i]);
if (i != sz - 1) cerr << ", ";
}
}
template <typename T>
void debug_out(vector<vector<T>> t) {
int sz = t.size();
for (int i = 0; i < sz; i++) {
debug_out(t[i]);
if (i != sz - 1) cerr << endl;
}
}
const int md = 998244353;
inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}
int n, k;
int main() {
scanf("%d%d", &n, &k);
vector<int> a(n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
a[i]--;
}
vector<int> to(n), foo(n, n);
for (int i = n - 1; i > -1; i--) {
to[i] = foo[a[i]];
foo[a[i]] = i;
}
vector<int> dp(n + 1);
int b = sqrt(n + 1);
int cnt = (n + b) / b;
vector<int> shift(cnt);
vector<unordered_map<int, int>> cur(cnt);
cur.back()[0] = 1;
dp[n] = 1;
vector<int> over(n + 1);
int total = 1;
auto modify = [&](int l, int r, int c) {
while (l <= r && l % b) {
over[l] += c;
if (over[l] + shift[l / b] == k + 1 && c > 0) sub(total, dp[l]);
if (over[l] + shift[l / b] == k && c < 0) add(total, dp[l]);
add(cur[l / b][over[l]], dp[l]);
sub(cur[l / b][over[l] - c], dp[l]);
if (!cur[l / b][over[l] - c]) cur[l / b].erase(over[l] - c);
l++;
}
while (r >= l && (r + 1) % b) {
over[r] += c;
if (over[r] + shift[r / b] == k + 1 && c > 0) sub(total, dp[r]);
if (over[r] + shift[r / b] == k && c < 0) add(total, dp[r]);
add(cur[r / b][over[r]], dp[r]);
sub(cur[r / b][over[r] - c], dp[r]);
if (!cur[r / b][over[r] - c]) cur[r / b].erase(over[r] - c);
r--;
}
if (r < l) return;
for (int block = l; block <= r; block += b) {
int t = block / b;
shift[t] += c;
if (c > 0 && cur[t].count(k + 1 - shift[t]))
sub(total, cur[t][k + 1 - shift[t]]);
if (c < 0 && cur[t].count(k - shift[t])) add(total, cur[t][k - shift[t]]);
}
};
for (int i = n - 1; i > -1; i--) {
modify(i + 1, to[i], 1);
if (to[i] < n) modify(to[i] + 1, to[to[i]], -1);
dp[i] = total;
add(total, dp[i]);
add(cur[i / b][-shift[i / b]], dp[i]);
}
printf("%d", dp[0]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
struct sqrt_decomp {
long long n, b;
vector<long long> values;
vector<long long> prefix;
vector<long long> woff;
vector<long long> w;
sqrt_decomp(long long n) {
b = sqrt(n);
if (b * b != n) {
b++;
}
this->b = b;
this->n = b * b;
values = vector<long long>(this->n, 0);
prefix = vector<long long>(this->n, 0);
w = vector<long long>(this->n, 0);
woff = vector<long long>(this->b, 0);
}
void rebuild_wedge(long long idx) {
long long mn = LONG_LONG_MAX;
for (long long i = 0; i < b; i++) {
mn = min(mn, w[idx * b + i]);
}
woff[idx] += mn;
fill(prefix.begin() + idx * b, prefix.begin() + (idx + 1) * b, 0);
for (long long i = 0; i < b; i++) {
w[idx * b + i] -= mn;
assert(w[idx * b + i] < b);
prefix[idx * b + w[idx * b + i]] += values[idx * b + i];
}
for (long long i = 1; i < b; i++) {
prefix[idx * b + i] += prefix[idx * b + i - 1];
}
}
void update_wedge(long long idx, long long d) { woff[idx] += d; }
void update_point(long long i, long long d) { w[i] += d; }
long long query_wedge(long long idx, long long thresh) {
long long target = thresh - woff[idx];
target = min(b - 1, target);
if (false) cout << "wedge " << idx << " target: " << target << endl;
if (target < 0) return 0;
return prefix[idx * b + target];
}
long long query_point(long long i, long long thresh) {
if ((w[i] + woff[i / b]) <= thresh) return values[i];
return 0;
}
void update(long long l, long long r, long long d) {
long long i = l;
set<int> to_rebuild;
while (i < r) {
if (i % b == 0 && (r - i) >= b) {
update_wedge(i / b, d);
i += b;
} else {
update_point(i, d);
to_rebuild.insert(i / b);
i++;
}
}
for (auto it = to_rebuild.begin(); it != to_rebuild.end(); it++)
rebuild_wedge(*it);
}
long long query(long long r, long long thresh) {
if (false) {
for (long long i = 0; i < b; i++) {
cout << woff[i] << string(2 * b - 1, ' ');
}
cout << endl;
for (long long i = 0; i < n; i++) {
cout << w[i] << " ";
}
cout << endl;
for (long long i = 0; i < n; i++) {
cout << values[i] << " ";
}
cout << endl;
for (long long i = 0; i < n; i++) {
cout << prefix[i] << " ";
}
cout << endl;
}
long long ret = 0;
long long i = 0;
while (i < r) {
if (i % b == 0 && (r - i) >= b) {
ret = (ret + query_wedge(i / b, thresh)) % MOD;
i += b;
} else {
ret = (ret + query_point(i, thresh)) % MOD;
i++;
}
}
return ret;
}
void setv(long long i, long long x) {
values[i] = x;
rebuild_wedge(i / b);
}
long long getv(long long i) { return values[i]; }
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n, k;
cin >> n >> k;
n++;
vector<long long> prev(n, 0);
unordered_map<long long, long long> s;
vector<long long> x(n);
x[0] = -1;
for (long long i = 1; i < n; i++) {
cin >> x[i];
prev[i] = s[x[i]];
s[x[i]] = i;
}
s.clear();
sqrt_decomp a(n);
for (long long i = 0; i < n; i++) {
if (!i) {
a.setv(i, 1);
} else {
if (false) cout << "inc: [" << prev[i] << ", " << i << ")" << endl;
a.update(prev[i], i, 1);
if (prev[i]) {
if (false)
cout << "dec: [" << prev[prev[i]] << ", " << prev[i] << ")" << endl;
a.update(prev[prev[i]], prev[i], -1);
}
long long ret = a.query(i, k);
if (false) cout << i << ": " << ret << endl;
a.setv(i, ret);
}
}
cout << a.getv(n - 1) << endl;
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int N = 1e5 + 5, M = N / 250 + 3;
int bel[N], f[N], g[N], tag[M], s[M][250 + 3 << 1];
inline int read() {
int now = 0;
register char c = getchar();
for (; !isdigit(c); c = getchar())
;
for (; isdigit(c); now = now * 10 + c - 48, c = getchar())
;
return now;
}
void Update(int p, int v) {
int *s = ::s[bel[p]];
for (int i = 250; i <= 250 << 1; ++i)
(s[i] += v) >= 998244353 && (s[i] -= 998244353);
}
void Modify(int p, int v) {
int bel = ::bel[p], *s = ::s[bel];
tag[bel] += v;
for (int i = bel * 250 + 1; i <= p; ++i) {
if (v == 1)
(s[g[i] + 250] += 998244353 - f[i - 1]) >= 998244353 &&
(s[g[i] + 250] -= 998244353);
else
(s[g[i] - 1 + 250] += f[i - 1]) >= 998244353 &&
(s[g[i] - 1 + 250] -= 998244353),
(s[g[i] - 2 + 250] += f[i - 1]) >= 998244353 &&
(s[g[i] - 2 + 250] -= 998244353);
g[i] += v;
}
}
int Query(int p, int K) {
int bel = ::bel[p], sum = tag[bel];
long long res = 0;
for (int i = bel * 250 + 1; i <= p; ++i) g[i] <= K && (res += f[i - 1]);
while (bel--) {
if (std::abs(sum - K) <= 250)
res += s[bel][K - sum + 250];
else if (sum < K)
res += s[bel][250 << 1];
sum += tag[bel];
}
return res % 998244353;
}
int main() {
static int las[N], pre[N];
int n = read(), K = read();
for (int i = 1; i <= n; ++i) bel[i] = (i - 1) / 250;
f[0] = 1;
for (int i = 1; i <= n; ++i) {
int a = read();
las[i] = pre[a], pre[a] = i;
Update(i, f[i - 1]), Modify(i, 1);
if (las[i]) {
Modify(las[i], -2);
if (las[las[i]]) Modify(las[las[i]], 1);
}
f[i] = Query(i, K);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, B, a[100005], pos[100005], bel[100005], lst[100005], f[100005],
cnt[100005];
int delta[450], ans[450], sum[450][100005];
void Update(int u, int v) {
int t = bel[u];
sum[t][cnt[u]] = (sum[t][cnt[u]] - f[u] + 998244353) % 998244353;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] - f[u] + 998244353) % 998244353;
cnt[u] += v;
sum[t][cnt[u]] = (sum[t][cnt[u]] + f[u]) % 998244353;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] + f[u]) % 998244353;
}
void Add(int u, int v, int w) {
if (u > v) return;
int p = bel[u], q = bel[v];
if (p + 1 >= q) {
for (int i = u; i <= v; i++) Update(i, w);
return;
}
for (int i = u; bel[i] == p; i++) Update(i, w);
for (int i = v; bel[i] == q; i--) Update(i, w);
for (int i = p + 1; i < q; i++) {
if (w > 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] - sum[i][m - delta[i]] + 998244353) % 998244353;
delta[i] += w;
if (w < 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] + sum[i][m - delta[i]]) % 998244353;
}
}
int main() {
scanf("%d%d", &n, &m);
B = sqrt(n);
bel[0] = 1;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), bel[i] = i / B + 1;
f[0] = sum[1][0] = ans[1] = 1;
for (int i = 1; i <= n; i++) {
lst[i] = pos[a[i]];
Add(lst[lst[i]], lst[i] - 1, -1);
Add(lst[i], i - 1, 1);
int j = i - 1;
for (int j = i - 1; j >= 0 && bel[i] == bel[j]; j--)
if (cnt[j] + delta[bel[i]] <= m) f[i] = (f[i] + f[j]) % 998244353;
for (int j = bel[i] - 1; j; j--) f[i] = (f[i] + ans[j]) % 998244353;
sum[bel[i]][0] = (sum[bel[i]][0] + f[i]) % 998244353;
if (delta[bel[i]] <= m) ans[bel[i]] = (ans[bel[i]] + f[i]) % 998244353;
pos[a[i]] = i;
}
printf("%d", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353, dt = 320;
int b[100010], a[100010], pre[100010], d[100010];
int f[100010];
int q[320][645];
int n, k, now;
int sum[320];
inline int mo(int x) {
if (x >= MOD) return x - MOD;
return x;
}
void ins(int x, int y) {
b[x] = y;
int z = x / dt;
if (z == now) return;
sum[z] = 0;
for (int i = 0; i <= dt + dt; i++) q[z][i] = 0;
for (int i = z * dt + dt - 1; i >= z * dt; i--)
sum[z] += b[i], q[z][sum[z] + dt] = mo(q[z][sum[z] + dt] + f[i]);
for (int i = 1; i <= dt + dt; i++) q[z][i] = mo(q[z][i] + q[z][i - 1]);
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), pre[i] = d[a[i]], d[a[i]] = i;
f[0] = 1;
for (int i = 1; i <= n; i++) {
now = i / dt;
ins(i - 1, 1);
if (pre[i]) {
ins(pre[i] - 1, -1);
if (pre[pre[i]]) ins(pre[pre[i]] - 1, 0);
}
int tot = 0;
for (int j = i - 1; j / dt == now && j >= 0; j--) {
tot += b[j];
if (tot <= k) f[i] = mo(f[i] + f[j]);
}
for (int j = now - 1; j >= 0; j--) {
if (tot <= k + dt) f[i] = mo(f[i] + q[j][min(dt + dt, k + dt - tot)]);
tot += sum[j];
}
}
printf("%d\n", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T lowbit(T x) {
return x & (-x);
}
template <class T>
T gcd(T a, T b) {
return b ? gcd(b, a % b) : a;
}
template <class T>
inline T Pow(T a, T b, T p) {
T ret = 1;
a %= p;
for (; b; b >>= 1, a = a * a % p)
if (b & 1) (ret *= a) %= p;
return ret;
}
template <class T>
inline void read(T &ret) {
T x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
ret = x * f;
}
const int N = 1e5 + 10, M = 1e3;
const long long Z = 998244353;
struct node {
int prev, w;
} lk[N];
int head[N], sz = 0;
void add(int x, int loc) {
lk[++sz] = {head[x], loc};
head[x] = sz;
}
int n, m, a[N], b[N], Prev[N];
int blo, bl[N];
long long f[N], g[M][M], sum[M];
inline long long __g(int x, int y) {
if (y > blo * 2)
return g[x][blo * 2];
else if (y >= 0)
return g[x][y];
else
return 0;
}
void modify(int x, int val, int up) {
b[x] = val;
int cur = 0;
fill(g[bl[x]], g[bl[x]] + blo * 2 + 1, 0);
for (int i = (min(up, ((bl[x] + 1) * blo) - 1)); i >= (((bl[x]) * blo));
i--) {
cur += b[i];
(g[bl[x]][cur + blo] += f[i - 1]) %= Z;
}
sum[bl[x]] = cur;
for (int i = (1); i <= (blo * 2); i++) (g[bl[x]][i] += g[bl[x]][i - 1]) %= Z;
}
long long query(int x) {
long long ret = 0;
long long cur = 0;
for (int i = (bl[x]); i >= (0); i--) {
(ret += __g(i, m - cur + blo)) %= Z;
cur += sum[i];
}
return ret;
}
int main() {
read(n);
read(m);
for (int i = (1); i <= (n); i++) {
read(a[i]);
add(a[i], i);
}
for (int i = (1); i <= (n); i++)
for (int j = head[i]; j; j = lk[j].prev) Prev[lk[j].w] = lk[lk[j].prev].w;
blo = ceil(sqrt(n));
for (int i = (1); i <= (n); i++) bl[i] = i / blo;
f[0] = 1;
for (int i = (1); i <= (n); i++) {
int p = Prev[i], pp = Prev[p];
if (pp) modify(pp, 0, i);
if (p) modify(p, -1, i);
modify(i, 1, i);
f[i] = query(i);
}
printf("%lld\n", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1, mod = 998244353, b_sz = 500;
int n, k;
int last_occ[maxn], prev_occ[maxn];
int delta[maxn];
int dp[maxn];
int tot_delta[201], psums[201][2 * b_sz + 1];
void update(int i) {
vector<int> blocks = {i / b_sz};
if (i % b_sz == 0 and i) {
blocks.push_back(i / b_sz - 1);
}
delta[i] = 1;
if (prev_occ[i]) {
blocks.push_back(prev_occ[i] / b_sz);
delta[prev_occ[i]] = -1;
}
if (prev_occ[prev_occ[i]]) {
blocks.push_back(prev_occ[prev_occ[i]] / b_sz);
delta[prev_occ[prev_occ[i]]] = 0;
}
sort(blocks.begin(), blocks.end());
blocks.resize(unique(blocks.begin(), blocks.end()) - blocks.begin());
for (auto block : blocks) {
memset(psums[block], 0, sizeof(psums[block]));
int curr_unique = 0;
for (int j = min(i, (block + 1) * b_sz - 1); j >= block * b_sz; j--) {
psums[block][curr_unique + b_sz] =
(psums[block][curr_unique + b_sz] + dp[j]) % mod;
curr_unique += delta[j];
}
for (int j = 1; j <= 2 * b_sz; j++) {
psums[block][j] = (psums[block][j] + psums[block][j - 1]) % mod;
}
tot_delta[block] = curr_unique;
}
}
void solve(int i) {
int curr_unique = 0;
for (int block = i / b_sz; block >= 0; block--) {
if (k - curr_unique >= -b_sz) {
dp[i] = (dp[i] + psums[block][min(b_sz, k - curr_unique) + b_sz]) % mod;
}
curr_unique += tot_delta[block];
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
int a;
cin >> a;
prev_occ[i] = last_occ[a];
last_occ[a] = i;
}
dp[0] = 1;
for (int i = 1; i <= n; i++) {
update(i);
solve(i);
}
cout << dp[n];
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
char ch = getchar();
x = 0;
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + ch - 48, ch = getchar();
}
struct Arr {
int x, y;
} b[100000 + 1], c[320 + 1];
int a[100000 + 1], s[100000 + 1], add[320 + 1], pre[100000 + 1][2],
num[100000 + 1];
int f[100000 + 1], sum[100000 + 1];
int n, m, tot, sq;
bool Cmp(Arr a, Arr b) { return a.x < b.x; }
int Max(int x, int y) { return x > y ? x : y; }
int Min(int x, int y) { return x < y ? x : y; }
void Rebuild(int k, int l, int r, int ad) {
for (int i = c[k].x; i <= c[k].y; i++) s[i] += add[k];
for (int i = l; i <= r; i++) s[i] += ad;
add[k] = 0;
for (int i = c[k].x; i <= c[k].y; i++) b[i] = (Arr){s[i], f[i - 1]};
sort(b + c[k].x, b + 1 + c[k].y, Cmp);
sum[c[k].x] = b[c[k].x].y;
for (int i = c[k].x + 1; i <= c[k].y; i++)
sum[i] = (sum[i - 1] + b[i].y) % 998244353;
}
void Add(int l, int r, int ad) {
for (int i = 1; i <= tot; i++) {
if (c[i].x > r) break;
if (c[i].y >= l && c[i].x <= r) {
if (c[i].x < l)
Rebuild(i, l, Min(c[i].y, r), ad);
else if (c[i].y > r)
Rebuild(i, Max(i, c[i].x), r, ad);
else
(add[i] += ad);
}
}
}
int Get(int l, int r, int g) {
int mid = 0, w = 0;
while (l <= r) {
mid = l + r >> 1;
b[mid].x <= g ? l = (w = mid) + 1 : r = mid - 1;
}
return w;
}
int main() {
read(n), read(m);
for (int i = 1; i <= n; i++) read(a[i]);
sq = sqrt(n);
tot = n / sq + (n % sq > 0);
for (int i = 1; i <= n; i++) num[i] = (i - 1) / sq + 1;
for (int i = 1; i <= n; i++)
if (num[i] > num[i - 1]) c[num[i]].x = i, c[num[i - 1]].y = i - 1;
c[num[n]].y = n;
for (int i = 0; i <= n; i++) f[i] = 0;
f[0] = 1;
for (int i = 1; i <= n; i++) {
int k = num[i];
if (pre[a[i]][0]) {
if (pre[a[i]][1] + 1 >= c[k].x) {
for (int j = pre[a[i]][1] + 1; j <= pre[a[i]][0]; j++) --s[j];
for (int j = pre[a[i]][0] + 1; j <= i; j++) ++s[j];
Rebuild(k, 1, 0, 0);
} else if (pre[a[i]][0] >= c[k].x) {
Add(pre[a[i]][1] + 1, c[k].x - 1, -1);
for (int j = c[k].x; j <= pre[a[i]][0]; j++) --s[j];
for (int j = pre[a[i]][0] + 1; j <= i; j++) ++s[j];
Rebuild(k, 1, 0, 0);
} else {
Add(pre[a[i]][1] + 1, pre[a[i]][0], -1),
Add(pre[a[i]][0] + 1, c[k].x - 1, 1);
for (int j = c[k].x; j <= i; j++) ++s[j];
Rebuild(k, 1, 0, 0);
}
} else {
Add(1, c[k].x - 1, 1);
for (int j = c[k].x; j <= i; j++) ++s[j];
Rebuild(k, 1, 0, 0);
}
pre[a[i]][1] = pre[a[i]][0], pre[a[i]][0] = i;
for (int j = c[k].x; j <= i; j++)
if (s[j] + add[k] <= m) (f[i] += f[j - 1]) %= 998244353;
for (int j = k - 1; j >= 1; j--) {
if (b[c[j].x].x + add[j] <= m)
(f[i] += sum[Get(c[j].x, c[j].y, m - add[j])]) %= 998244353;
}
if (i == c[k].y) Rebuild(k, 1, 0, 0);
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize(3)
const int N = 100005, B = 320, M = 998244353;
using namespace std;
int n, k, ti, a[N], occ[N], pre[N], b[N], f[N], sum[B], q[B][B << 1 | 1], now;
inline void mdf(int x, int y) {
b[x] = y;
int z = x / B;
if (z == now) return;
sum[z] = 0;
for (int i = 0; i <= B << 1; i++) q[z][i] = 0;
for (int i = z * B + B - 1; i >= z * B; i--)
sum[z] += b[i], (q[z][sum[z] + B] += f[i]) %= M;
for (int i = 0; i < B << 1; i++) (q[z][i + 1] += q[z][i]) %= M;
}
int main() {
scanf("%d%d", &n, &k);
memset(occ, -1, N << 2);
for (int i = 0; i < n; i++)
scanf("%d", a + i), pre[i] = occ[a[i]], occ[a[i]] = i;
f[0] = 1;
for (int i = 0; i < n; i++) {
now = (i + 1) / B, mdf(i, 1);
if (~pre[i]) {
mdf(pre[i], -1);
if (~pre[pre[i]]) mdf(pre[pre[i]], 0);
}
ti = 0;
for (int j = i; j / B == now && ~j; j--) {
ti += b[j];
if (ti <= k) (f[i + 1] += f[j]) %= M;
}
for (int j = now - 1; ~j; j--) {
if (ti <= k + B) (f[i + 1] += q[j][min(B << 1, k + B - ti)]) %= M;
ti += sum[j];
}
}
printf("%d", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, m, B, a[100005], pos[100005], bel[100005], lst[100005], f[100005],
cnt[100005];
int delta[450], ans[450], sum[450][100005];
void Update(int u, int v) {
int t = bel[u];
sum[t][cnt[u]] = (sum[t][cnt[u]] - f[u] + 998244353) % 998244353;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] - f[u] + 998244353) % 998244353;
cnt[u] += v;
sum[t][cnt[u]] = (sum[t][cnt[u]] + f[u]) % 998244353;
if (cnt[u] + delta[t] <= m) ans[t] = (ans[t] + f[u]) % 998244353;
}
void Add(int u, int v, int w) {
if (u > v) return;
int p = bel[u], q = bel[v];
if (p + 1 >= q) {
for (int i = u; i <= v; i++) Update(i, w);
return;
}
for (int i = u; bel[i] == p; i++) Update(i, w);
for (int i = v; bel[i] == q; i--) Update(i, w);
for (int i = p + 1; i < q; i++) {
if (w > 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] - sum[i][m - delta[i]] + 998244353) % 998244353;
delta[i] += w;
if (w < 0)
if (m - delta[i] >= 0)
ans[i] = (ans[i] + sum[i][m - delta[i]]) % 998244353;
}
}
int main() {
scanf("%d%d", &n, &m);
B = sqrt(n);
bel[0] = 1;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]), bel[i] = i / B + 1;
f[0] = sum[1][0] = ans[1] = 1;
for (int i = 1; i <= n; i++) {
lst[i] = pos[a[i]];
Add(lst[lst[i]], lst[i] - 1, -1);
Add(lst[i], i - 1, 1);
int j = i - 1;
for (int j = i - 1; j >= 0 && bel[i] == bel[j]; j--)
if (cnt[j] + delta[bel[i]] <= m) f[i] = (f[i] + f[j]) % 998244353;
for (int j = bel[i] - 1; j; j--) f[i] = (f[i] + ans[j]) % 998244353;
sum[bel[i]][0] = (sum[bel[i]][0] + f[i]) % 998244353;
if (delta[bel[i]] <= m) ans[bel[i]] = (ans[bel[i]] + f[i]) % 998244353;
pos[a[i]] = i;
}
printf("%d", f[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int B = 320;
const int N = 100000;
const int P = 998244353;
inline int add(int x, int y) {
x += y;
return x >= P ? x - P : x;
}
inline int sub(int x, int y) {
x -= y;
return x < 0 ? x + P : x;
}
inline int mul(int x, int y) { return (int)(1LL * x * y % P); }
int n, k;
int le[N + 5], ri[N + 5], id[N + 5], bcnt;
void build() {
for (int i = 1; i <= n; i++) {
if ((i - 1) % B == 0) le[++bcnt] = i;
ri[bcnt] = i, id[i] = bcnt;
}
}
int c[N + 5], dp[N + 5];
int s[B + 5][B + 5], tg[B + 5], lb[B + 5];
void pushtag(int x) {
if (!tg[x]) return;
for (int i = le[x]; i <= ri[x]; i++) c[i] += tg[x];
tg[x] = 0;
}
void rebuild(int x) {
lb[x] = 10 * N + 5;
for (int i = 0; i <= B; i++) s[x][i] = 0;
for (int i = le[x]; i <= ri[x]; i++) lb[x] = min(lb[x], c[i]);
for (int i = le[x]; i <= ri[x]; i++)
s[x][c[i] - lb[x]] = add(s[x][c[i] - lb[x]], dp[i - 1]);
for (int i = 1; i <= B; i++) s[x][i] = add(s[x][i], s[x][i - 1]);
}
void add(int l, int r, int d) {
int p = id[l], q = id[r];
if (p == q) {
pushtag(p);
for (int i = l; i <= r; i++) c[i] += d;
rebuild(p);
} else {
pushtag(p);
for (int i = l; i <= ri[p]; i++) c[i] += d;
rebuild(p);
for (int i = p + 1; i <= q - 1; i++) tg[i] += d;
pushtag(q);
for (int i = le[q]; i <= r; i++) c[i] += d;
rebuild(q);
}
}
int query() {
int ret = 0;
for (int i = 1; i <= bcnt; i++) {
if (lb[i] + tg[i] <= k) {
if (lb[i] + tg[i] + B <= k)
ret = add(ret, s[i][B]);
else
ret = add(ret, s[i][k - lb[i] - tg[i]]);
}
}
return ret;
}
int a[N + 5], b[N + 5], lst[N + 5];
int main() {
scanf("%d%d", &n, &k), build();
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]), lst[i] = b[a[i]], b[a[i]] = i;
dp[0] = 1, rebuild(1);
for (int i = 1; i <= n; i++) {
add(lst[i] + 1, i, 1);
if (lst[i]) add(lst[lst[i]] + 1, lst[i], -1);
dp[i] = query(), rebuild(id[i + 1]);
}
printf("%d\n", dp[n]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000;
const int SZ = 320;
const long long MOD = 998244353;
int N, K;
int prv[MAX_N + 1];
int arr[MAX_N + 1];
int pidx[MAX_N + 1];
int num[MAX_N + 1];
long long dp[MAX_N + 1];
long long dp2[SZ + 1][MAX_N + 1];
long long D;
int add[SZ + 1];
void Update(int x, int y, int z) {
for (int i = 0; i < SZ; i++) {
int s = i * SZ, e = min(N, (i + 1) * SZ - 1);
if (s > e) return;
if (x <= s && e <= y) {
if (z == 1) {
add[i]++;
if (K + 1 - add[i] >= 0 && K + 1 - add[i] <= N) {
D = (D - dp2[i][K + 1 - add[i]] + MOD) % MOD;
}
} else {
add[i]--;
if (K - add[i] >= 0 && K - add[i] <= N) {
D = (D + dp2[i][K - add[i]]) % MOD;
}
}
} else if (x > e || y < s)
continue;
else {
for (int j = s; j <= e; j++) {
dp2[i][num[j]] = 0LL;
num[j] += add[i];
}
add[i] = 0LL;
for (int j = max(s, x); j <= min(e, y); j++) {
num[j] += z;
if (z == 1) {
if (num[j] == K + 1) {
D = (D - dp[j] + MOD) % MOD;
}
} else {
if (num[j] == K) {
D = (D + dp[j]) % MOD;
}
}
}
for (int j = s; j <= e; j++) {
dp2[i][num[j]] = (dp2[i][num[j]] + dp[j]) % MOD;
}
}
}
}
void Add(int x) {
int g = x / SZ;
int s = g * SZ;
for (int i = s; i < x; i++) {
dp2[g][num[i]] = 0LL;
num[i] += add[g];
}
add[g] = 0;
for (int i = s; i <= x; i++) {
dp2[g][num[i]] = (dp2[g][num[i]] + dp[i]) % MOD;
}
D = (D + dp[x]) % MOD;
}
int main() {
scanf("%d%d", &N, &K);
for (int i = 1; i <= N; i++) {
scanf("%d", &arr[i]);
prv[i] = pidx[arr[i]];
pidx[arr[i]] = i;
}
dp[0] = 1;
D = 1LL;
for (int i = 1; i <= N; i++) {
Update(prv[i], i - 1, 1);
if (prv[i] != 0) {
Update(prv[prv[i]], prv[i] - 1, -1);
}
dp[i] = D;
Add(i);
}
cout << dp[N];
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c i, c j) {
return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
template <class c>
debug& operator<<(const c&) {
return *this;
}
};
using ll = long long;
const int B = 100;
const int nax = 2e5 + 5;
int offset[nax / B];
int cnt[nax / B][B + 2];
int his[nax];
const int mod = 998244353;
void add_self(int& a, int b) {
a += b;
if (a >= mod) {
a -= mod;
}
}
int main() {
int n, k;
scanf("%d%d", &n, &k);
vector<int> dp(B + n + 2);
dp[1] = 1;
auto ogarnij = [&](int who) {
for (int i = 0; i <= B; ++i) {
cnt[who][i] = 0;
}
int small = INT_MAX;
for (int i = max(1, who * B); i < (who + 1) * B; ++i) {
small = min(small, his[i]);
}
offset[who] += small;
for (int i = max(1, who * B); i < (who + 1) * B; ++i) {
his[i] -= small;
add_self(cnt[who][his[i]], dp[i]);
}
for (int i = 1; i <= B; ++i) {
add_self(cnt[who][i], cnt[who][i - 1]);
}
};
auto add = [&](int L, int R, int x) {
int left = L / B;
int right = R / B;
for (int i = left + 1; i < right; ++i) {
offset[i] += x;
}
for (int i = L; i <= R && i / B == left; ++i) {
his[i] += x;
}
ogarnij(left);
if (left != right) {
for (int i = R; i / B == right; --i) {
his[i] += x;
}
ogarnij(right);
}
};
ogarnij(0);
vector<int> last(n + 1), sec_last(n + 1);
for (int me = 1; me <= n; ++me) {
int x;
scanf("%d", &x);
if (last[x]) {
add(sec_last[x] + 1, last[x], -1);
}
add(last[x] + 1, me, 1);
sec_last[x] = last[x];
last[x] = me;
int Z = 0;
for (int who = 0; who <= me / B; ++who) {
int up_to = min(k - offset[who], B);
if (up_to >= 0) {
add_self(Z, cnt[who][up_to]);
}
}
dp[me + 1] = Z;
ogarnij((me + 1) / B);
}
printf("%d\n", dp[n + 1]);
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto& i : v) is >> i;
return is;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (auto& i : v) os << i << ' ';
return os;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p) {
os << p.first << ' ' << p.second;
return os;
}
template <typename T, typename U>
pair<T, U> operator-(pair<T, U> a, pair<T, U> b) {
return make_pair(a.first - b.first, a.second - b.second);
}
template <typename T, typename U>
pair<T, U> operator+(pair<T, U> a, pair<T, U> b) {
return make_pair(a.first + b.first, a.second + b.second);
}
const int K = 330;
const long long mod = 998244353;
array<long long, K * K> dp;
struct Block {
int ind;
array<int, K> b;
array<long long, 2 * K + 1> sms;
int sm = 0;
Block(int i = -1) : ind(i) {
b.fill(0);
sms.fill(0);
}
void reset() {
sms.fill(0);
sm = 0;
for (int i = K - 1; i >= 0; --i) {
sm += b[i];
if (ind == 0 && i == 0)
sms[K + sm]++;
else
sms[K + sm] += dp[ind * K + i - 1];
}
for (int i = 1; i < sms.size(); ++i) sms[i] = (sms[i] + sms[i - 1]) % mod;
sms[0] %= mod;
}
long long ask(int u) {
if (u + K < 0)
return 0;
else if (u + K >= sms.size())
return sms.back();
else
return sms[u + K];
}
void change(int ind, int k) {
b[ind] = k;
reset();
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
vector<Block> blocks(K);
for (int i = 0; i < K; ++i) {
blocks[i].ind = i;
}
int n, k;
cin >> n >> k;
vector<int> vv(n);
cin >> vv;
vector<int> lst(n + 1, -1);
vector<int> prev(n);
for (int i = 0; i < n; ++i) {
prev[i] = lst[vv[i]];
lst[vv[i]] = i;
}
for (int i = 0; i < n; ++i) {
blocks[i / K].change(i % K, 1);
if (prev[i] != -1) {
if (prev[prev[i]] != -1) {
blocks[prev[prev[i]] / K].change(prev[prev[i]] % K, 0);
}
blocks[prev[i] / K].change(prev[i] % K, -1);
}
42;
;
int s = 0;
for (int j = i; j >= i / K * K; --j) {
s += blocks[i / K].b[j % K];
if (s <= k) dp[i] += (j == 0 ? 1 : dp[j - 1]);
}
for (int j = i / K - 1; j >= 0; --j) {
dp[i] += blocks[j].ask(k - s);
s += blocks[j].sm;
}
dp[i] %= mod;
if (i % K == 0) blocks[i / K].reset();
}
42;
;
42;
;
cout << dp[n - 1] << '\n';
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
const long long mod = 998244353;
const int bsize = 75;
int cnt[maxn];
int dp[maxn];
int n, k;
vector<int> occurences[maxn];
class bucket {
public:
int id;
int first, last;
int offset, smallest;
int pref[bsize + 5];
void build() {
first = id * bsize;
last = (id + 1) * bsize - 1;
smallest = INT_MAX;
for (int i = first; i <= last; i++) {
smallest = min(smallest, offset + cnt[i]);
cnt[i] += offset;
}
offset = smallest;
for (int i = first; i <= last; i++) {
cnt[i] -= offset;
}
for (int i = 0; i <= bsize; i++) {
pref[i] = 0;
}
for (int i = first; i <= last; i++) {
if (cnt[i] < 0 || cnt[i] > bsize) continue;
pref[cnt[i]] += dp[i];
if (pref[cnt[i]] > mod) pref[cnt[i]] -= mod;
}
for (int i = 1; i <= bsize; i++) {
pref[i] += pref[i - 1];
if (pref[i] > mod) pref[i] -= mod;
}
}
} buckets[maxn / bsize + 1];
int bucketid(int i) { return i / bsize; }
void add(int l, int r, int diff) {
for (int i = l; i <= r && bucketid(i) == bucketid(l); i++) {
cnt[i] += diff;
}
buckets[bucketid(l)].build();
if (bucketid(l) == bucketid(r)) return;
for (int i = bucketid(l) + 1; i < bucketid(r); i++) {
buckets[i].offset += diff;
}
for (int i = r; i >= l && bucketid(i) == bucketid(r); i--) {
cnt[i] += diff;
}
buckets[bucketid(r)].build();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
for (int i = 0; i <= bucketid(n - 1); i++) {
buckets[i].id = i;
}
for (int i = 0; i <= n; i++) {
occurences[i].push_back(-1);
}
dp[0] = 1;
buckets[bucketid(0)].build();
int x;
for (int i = 0; i < n; i++) {
cin >> x;
if (occurences[x].size() >= 2) {
add(occurences[x][occurences[x].size() - 2] + 1,
occurences[x][occurences[x].size() - 1], -1);
}
add(occurences[x].back() + 1, i, 1);
occurences[x].push_back(i);
int total = 0;
for (int j = 0; j <= bucketid(i); j++) {
int limit = k - buckets[j].offset;
if (limit >= 0) {
total += buckets[j].pref[min(bsize, limit)];
if (total > mod) total -= mod;
}
}
dp[i + 1] = total;
buckets[bucketid(i)].build();
}
cout << dp[n] << "\n";
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <class c>
struct rge {
c b, e;
};
template <class c>
rge<c> range(c h, c n) {
return {h, n};
}
template <class c>
auto dud(c* r) -> decltype(cerr << *r);
template <class c>
char dud(...);
struct muu {
template <class c>
muu& operator<<(const c&) {
return *this;
}
muu& operator()() { return *this; }
};
const int K = 507;
const int N = 1e5 + 7;
int n, k;
int blo = K;
int a[N];
const int mod = 998244353;
int num[N];
pair<int, int> prz[K];
int off[K];
int sum[K][N];
long long qq[K];
int war[N];
int dp[N];
int ost1[N];
int ost2[N];
void changeOff(int i, int c) {
if (c == 1 && k >= off[i]) qq[i] = (qq[i] - sum[i][k - off[i]] + mod) % mod;
off[i] += c;
if (c == -1 && k >= off[i]) qq[i] = (qq[i] + sum[i][k - off[i]]) % mod;
}
void czysc(int i) {
qq[i] = 0;
for (int j = prz[i].first; j <= prz[i].second; ++j) {
sum[i][war[j]] = 0;
war[j] += off[i];
}
off[i] = 0;
}
void przelicz(int i) {
for (int j = prz[i].first; j <= prz[i].second; ++j) {
sum[i][war[j]] += dp[j - 1];
sum[i][war[j]] %= mod;
if (war[j] <= k) {
qq[i] += dp[j - 1];
qq[i] %= mod;
}
}
}
void add(int i, int j, int c) { war[j] += c; }
void insert(int i, int j, int v) {
dp[j] = v;
czysc(i);
przelicz(i);
}
int query(int i) {
(muu() << __FUNCTION__ << "#" << 87 << ": ") << "["
"i"
": "
<< (i)
<< "] "
"["
"qq[i]"
": "
<< (qq[i]) << "] ";
return (qq[i] % mod + mod) % mod;
}
void wstaw(int a, int b, int c) {
(muu() << __FUNCTION__ << "#" << 92 << ": ") << "["
"a"
": "
<< (a)
<< "] "
"["
"b"
": "
<< (b)
<< "] "
"["
"c"
": "
<< (c) << "] ";
int i = num[a];
czysc(i);
for (int j = a; j <= b && j <= prz[i].second; ++j) {
add(i, j, c);
}
przelicz(i);
i++;
while (i < num[b]) {
changeOff(i, c);
i++;
}
if (i == num[b]) {
czysc(i);
for (int j = prz[i].first; j <= b; ++j) {
add(i, j, c);
}
przelicz(i);
}
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
blo = min(blo, n);
for (int i = 1; i <= n; ++i) {
num[i] = (blo * (i - 1)) / n;
}
for (int i = 1; i <= n; ++i) {
prz[num[i]].second = i;
}
for (int i = n; i > 0; --i) {
prz[num[i]].first = i;
}
int wynik = 0;
dp[0] = 1;
for (int i = 1; i <= n; ++i) {
int x = a[i];
if (ost1[x]) {
wstaw(ost2[x] + 1, ost1[x], -1);
}
wstaw(ost1[x] + 1, i, 1);
ost2[x] = ost1[x];
ost1[x] = i;
long long res = 0;
for (int j = 0; j < blo; ++j) {
res += query(j);
}
res %= mod;
wynik = res;
(muu() << __FUNCTION__ << "#" << 142 << ": ") << "["
"i"
": "
<< (i)
<< "] "
"["
"wynik"
": "
<< (wynik) << "] ";
insert(num[i], i, wynik);
}
printf("%d\n", wynik);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
const int Bs = 317, N = 2e5 + 10, mod = 998244353;
template <typename T>
void chkmax(T &x, T y) {
x = x > y ? x : y;
}
template <typename T>
void chkmin(T &x, T y) {
x = x > y ? y : x;
}
template <typename T>
void add(T &x, T y, T mod) {
x = x + y > mod ? x + y - mod : x + y;
}
template <typename T>
void sub(T &x, T y, T mod) {
x = x - y < 0 ? x - y + mod : x - y;
}
template <typename T>
void multi(T &x, T y, T mod) {
x = 1ll * x * y % mod;
}
const int INF = (1ll << 30);
template <typename T>
void read(T &x) {
x = 0;
bool f = 1;
char ch;
do {
ch = getchar();
if (ch == '-') f = 0;
} while (ch > '9' || ch < '0');
do {
x = x * 10 + ch - '0';
ch = getchar();
} while (ch >= '0' && ch <= '9');
x = f ? x : -x;
}
template <typename T>
void write(T x) {
if (x < 0) x = ~x + 1, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
int pr[N], las[N], a[N], val[Bs][N], sum[N], f[N], tag[Bs], B, ans, k, n;
int belong(int x) { return (x - 1) / B + 1; }
inline void insert(int u, int v) {
int bu = belong(u);
sum[u] -= tag[bu];
add(ans, v, mod);
add(val[bu][sum[u] + n], v, mod);
}
void change(int u, int v) {
int bu = belong(u);
if (sum[u] + tag[bu] <= k) sub(ans, f[u - 1], mod);
sub(val[bu][sum[u] + n], f[u - 1], mod);
sum[u] += v;
if (sum[u] + tag[bu] <= k) add(ans, f[u - 1], mod);
add(val[bu][sum[u] + n], f[u - 1], mod);
}
inline void modify(int l, int r, int v) {
if (l > r) return;
int bl = belong(l), br = belong(r);
if (bl + 1 >= br) {
for (int i = l; i <= r; i++) change(i, v);
} else {
for (int i = l; i <= B * bl; i++) change(i, v);
for (int i = B * (br - 1) + 1; i <= r; i++) change(i, v);
for (int i = bl + 1; i < br; i++) {
if (~v)
sub(ans, val[i][k - tag[i] + n], mod);
else
add(ans, val[i][k - tag[i] + 1 + n], mod);
tag[i] += v;
}
}
}
int main() {
read(n);
read(k);
for (int i = 1, a; i <= n; ++i) read(a), pr[i] = las[a], las[a] = i;
B = sqrt(n);
f[0] = 1;
for (int i = 1; i <= n; ++i) {
insert(i, f[i - 1]);
modify(pr[i] + 1, i, 1);
modify(pr[pr[i]] + 1, pr[i], -1);
f[i] = ans;
}
printf("%d\n", f[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
int q, sum[320][2 * 320], tot[320];
int dp[100009], last[100009], last2[100009], v[100009];
inline void add(int &a, int b) {
a += b;
if (a >= 998244353) a -= 998244353;
}
inline void update(int t) {
memset(sum[t], 0, sizeof sum[t]);
int cnt = 0;
for (int i = t * q + q; i > t * q; i--) {
cnt += v[i];
add(sum[t][cnt + q], dp[i - 1]);
}
tot[t] = cnt;
for (int i = 1; i <= 2 * q; i++) add(sum[t][i], sum[t][i - 1]);
}
int main() {
int n, k;
fscanf(stdin, "%d%d", &n, &k);
dp[0] = 1;
q = sqrt(n);
for (int i = 1; i <= n; i++) {
int x;
fscanf(stdin, "%d", &x);
v[i] = 1;
if (last2[x]) {
v[last2[x]] = 0;
if ((last2[x] - 1) / q < (i - 1) / q) update((last2[x] - 1) / q);
}
if (last[x]) {
v[last[x]] = -1;
if ((last[x] - 1) / q < (i - 1) / q) update((last[x] - 1) / q);
}
last2[x] = last[x];
last[x] = i;
int cnt = 0, lim = (i - 1) / q * q;
for (int j = i; j > lim; j--) {
cnt += v[j];
if (cnt <= k) add(dp[i], dp[j - 1]);
}
for (int p = (i - 1) / q - 1; p >= 0; p--) {
if (k - cnt >= -q) {
if (k - cnt <= q)
add(dp[i], sum[p][k - cnt + q]);
else
add(dp[i], sum[p][2 * q]);
}
cnt += tot[p];
}
if (i % q == 0) update((i - 1) / q);
}
fprintf(stdout, "%d\n", dp[n]);
fclose(stdin);
fclose(stdout);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 11, mod = 998244353;
int ans, n, k, blo, bel[N], f[N], g[N], a[N], L[N], R[N], las[N], bef[N],
lim[N], sum[411][N], Sum[411];
inline void inc(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
inline void deal(int l, int r, int x) {
int o = bel[l];
for (register int i = l; i <= r; ++i) {
if (f[i] <= lim[o]) inc(Sum[o], mod - g[i - 1]);
inc(sum[o][f[i]], mod - g[i - 1]);
f[i] += x;
inc(sum[o][f[i]], g[i - 1]);
if (f[i] <= lim[o]) inc(Sum[o], g[i - 1]);
}
}
inline void modify(int l, int r, int x) {
int o;
if (bel[l] == bel[r]) {
deal(l, r, x);
return;
}
o = bel[l];
deal(l, R[o], x);
o = bel[r];
deal(L[o], r, x);
for (register int i = bel[l] + 1; i <= bel[r] - 1; ++i) {
if (x == 1) inc(Sum[i], mod - sum[i][lim[i]]);
lim[i] -= x;
if (x == -1) inc(Sum[i], sum[i][lim[i]]);
}
}
int main() {
scanf("%d%d", &n, &k);
blo = sqrt(n);
for (register int i = 1; i <= n; ++i) {
scanf("%d", a + i), bel[i] = (i - 1) / blo + 1;
las[i] = bef[a[i]];
bef[a[i]] = i;
}
for (register int i = 1; i <= n; ++i) {
if (!L[bel[i]]) L[bel[i]] = i;
R[bel[i]] = i;
}
for (register int i = 1; i <= bel[n]; ++i) lim[i] = k;
g[0] = 1;
for (register int i = 1; i <= n; ++i) {
inc(sum[bel[i]][0], g[i - 1]), inc(Sum[bel[i]], g[i - 1]);
modify(las[i] + 1, i, 1);
if (las[i]) modify(las[las[i]] + 1, las[i], -1);
ans = 0;
for (register int j = 1; j <= bel[i]; ++j) inc(ans, Sum[j]);
g[i] = ans;
}
printf("%d\n", g[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
int n, k, nr_b;
int t[500010], dp[500010], cnt[500010];
vector<int> _last[500010];
int f(int x) {
if (x >= 998244353)
return x - 998244353;
else
return x;
}
struct bucket {
int offset;
int bucket_id;
vector<int> pref_sum;
bucket() {
offset = 0;
bucket_id = nr_b;
pref_sum = vector<int>(315 + 1, 0);
}
void rebuild() {
int l = (bucket_id - 1) * 315 + 1;
int r = bucket_id * 315;
int minn = 1e9;
for (int i = l; i <= r; i++) minn = min(minn, cnt[i] + offset);
for (int i = 0; i < 315; i++) pref_sum[i] = 0;
for (int i = l; i <= r; i++) {
cnt[i] = (cnt[i] + offset) - minn;
pref_sum[cnt[i]] = f(pref_sum[cnt[i]] + dp[i - 1]);
}
offset = minn;
for (int i = 1; i < 315; i++)
pref_sum[i] = f(pref_sum[i] + pref_sum[i - 1]);
}
int get_smaller(int k) {
int x = k - offset;
if (x < 0)
return 0;
else
return pref_sum[min(x, 315 - 1)];
}
};
bucket buckets[500];
int get_bucket(int pos) { return (pos - 1) / 315 + 1; }
void add(int start, int end, int val) {
for (int i = start; i <= end && get_bucket(i) == get_bucket(start); i++)
cnt[i] += val;
buckets[get_bucket(start)].rebuild();
for (int i = get_bucket(start) + 1; i < get_bucket(end); i++)
buckets[i].offset += val;
if (get_bucket(start) != get_bucket(end)) {
for (int i = end; i >= start && get_bucket(i) == get_bucket(end); i--)
cnt[i] += val;
buckets[get_bucket(end)].rebuild();
}
}
int main() {
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &t[i]);
for (int i = 1; i <= n; i++) _last[i].push_back(0);
dp[0] = 1;
for (int i = 1; i <= n; i++) {
if (get_bucket(i) > nr_b) {
nr_b++;
buckets[nr_b] = bucket();
}
add(_last[t[i]].back() + 1, i, 1);
if (((int)(_last[t[i]].size())) >= 2) {
add(_last[t[i]].end()[-2] + 1, _last[t[i]].back(), -1);
}
_last[t[i]].push_back(i);
int total = 0;
for (int j = 1; j <= nr_b; j++)
total = f(total + buckets[j].get_smaller(k));
dp[i] = total;
}
printf("%d", dp[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
using namespace std;
int mod = 998244353;
int A[100077];
int B[100077];
int N[100077];
int dp[100077];
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, k, ai;
cin >> n >> k;
dp[0] = 1;
signed long long sum = 1;
for (int i = 1; i <= n; i++) {
cin >> ai;
for (int j = B[ai]; j < A[ai]; j++)
if (--N[j] == k) sum += dp[j];
for (int j = A[ai]; j < i; j++)
if (++N[j] == k + 1) sum -= dp[j];
sum %= mod;
dp[i] = sum;
B[ai] = A[ai];
A[ai] = i;
sum = (sum + dp[i]) % mod;
}
cout << (dp[n] % mod + mod) % mod << endl;
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
const long long MOD = 998244353;
const long long PIERW = 100;
int ciag[MAX];
int ostatnie[MAX];
int poprzedni[MAX];
long long pomocnicza[MAX];
long long DP[MAX];
struct Blok {
int lewa, prawa;
long long suma;
long long sumaDP[PIERW * 2 + 1];
void Aktualizuj() {
for (int i = 0; i <= 2 * PIERW; ++i) sumaDP[i] = 0;
suma = 0;
for (int i = prawa; i >= lewa; --i) {
suma += pomocnicza[i];
sumaDP[suma + PIERW] = (sumaDP[suma + PIERW] + DP[i - 1]) % MOD;
}
for (int i = 1; i <= 2 * PIERW; i++)
sumaDP[i] = (sumaDP[i] + sumaDP[i - 1]) % MOD;
}
};
Blok bloki[MAX / PIERW + 7];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
DP[0] = 1;
for (int i = 0; i * PIERW <= n; ++i) {
bloki[i].lewa = i * PIERW + 1;
bloki[i].prawa = (i + 1) * PIERW;
bloki[i].Aktualizuj();
}
for (int i = 1; i <= n; ++i) {
cin >> ciag[i];
poprzedni[i] = ostatnie[ciag[i]];
ostatnie[ciag[i]] = i;
pomocnicza[i] = 1;
pomocnicza[poprzedni[i]] = -1;
int t = poprzedni[poprzedni[i]];
pomocnicza[t] = 0;
bloki[(i - 1) / PIERW].Aktualizuj();
bloki[(poprzedni[i] - 1) / PIERW].Aktualizuj();
bloki[(t - 1) / PIERW].Aktualizuj();
int blok = (i - 1) / PIERW;
long long suma = 0;
for (int j = blok; j >= 0; --j) {
int x = min(k - suma, PIERW);
if (-PIERW <= x && x <= PIERW)
DP[i] = (DP[i] + bloki[j].sumaDP[x + PIERW]) % MOD;
suma += bloki[j].suma;
}
bloki[i / PIERW].Aktualizuj();
}
cout << DP[n] << "\n";
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
int bb[1 + 100000], dp[1 + 100000], ss[((100000 + 200 - 1) / 200)],
dq[((100000 + 200 - 1) / 200)][200 + 1 + 200];
void update(int h) {
int *qq = dq[h];
int i, t, c;
t = 0;
memset(qq, 0, (200 + 1 + 200) * sizeof *qq);
for (i = (h + 1) * 200; i > h * 200; i--) {
t += bb[i];
qq[200 + t] = (qq[200 + t] + dp[i - 1]) % 998244353;
}
for (c = 1; c <= 200 + 200; c++) qq[c] = (qq[c] + qq[c - 1]) % 998244353;
}
int main() {
static int pp[1 + 100000], ii[1 + 100000];
int n, m, k, h, i, j;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) {
int a;
scanf("%d", &a);
pp[i] = ii[a];
ii[a] = i;
}
dp[0] = 1;
for (j = 1; j <= n; j++) {
int p, x, t;
m = (j - 1) / 200;
ss[m] += 1 - bb[j];
bb[j] = 1;
if ((p = pp[j])) {
h = (p - 1) / 200;
ss[h] += -1 - bb[p];
bb[p] = -1;
if (p <= m * 200) update(h);
if ((p = pp[p])) {
h = (p - 1) / 200;
ss[h] += 0 - bb[p];
bb[p] = 0;
if (p <= m * 200) update(h);
}
}
x = 0;
t = 0;
for (i = j; i > m * 200; i--) {
t += bb[i];
if (t <= k) x = (x + dp[i - 1]) % 998244353;
}
for (h = m - 1; h >= 0; h--) {
if (k - t >= -200)
x = (x + dq[h][200 + (200 < k - t ? 200 : k - t)]) % 998244353;
t += ss[h];
}
dp[j] = x;
if (j % 200 == 0) update(m);
}
printf("%d\n", dp[n]);
return 0;
}
|
1129_D. Isolation
|
Find the number of ways to divide an array a of n integers into any number of disjoint non-empty segments so that, in each segment, there exist at most k distinct integers that appear exactly once.
Since the answer can be large, find it modulo 998 244 353.
Input
The first line contains two space-separated integers n and k (1 β€ k β€ n β€ 10^5) β the number of elements in the array a and the restriction from the statement.
The following line contains n space-separated integers a_1, a_2, β¦, a_n (1 β€ a_i β€ n) β elements of the array a.
Output
The first and only line contains the number of ways to divide an array a modulo 998 244 353.
Examples
Input
3 1
1 1 2
Output
3
Input
5 2
1 1 2 1 3
Output
14
Input
5 5
1 2 3 4 5
Output
16
Note
In the first sample, the three possible divisions are as follows.
* [[1], [1], [2]]
* [[1, 1], [2]]
* [[1, 1, 2]]
Division [[1], [1, 2]] is not possible because two distinct integers appear exactly once in the second segment [1, 2].
|
{
"input": [
"5 5\n1 2 3 4 5\n",
"3 1\n1 1 2\n",
"5 2\n1 1 2 1 3\n"
],
"output": [
"16",
"3",
"14"
]
}
|
{
"input": [
"50 1\n50 8 46 9 12 38 41 18 49 10 23 15 16 3 13 17 48 8 31 32 6 31 31 49 9 40 9 21 23 41 17 31 45 47 17 1 12 15 50 40 38 4 20 1 9 37 4 47 4 24\n",
"100 30\n31 57 26 94 41 92 88 4 46 51 64 45 89 59 91 49 3 28 17 63 9 74 77 60 83 30 73 64 90 47 34 80 94 89 66 31 19 84 86 83 62 59 96 67 93 58 7 86 11 34 35 15 53 59 71 16 98 4 73 70 23 53 33 95 40 45 90 51 2 20 7 73 38 67 89 90 39 8 66 76 4 57 50 80 81 96 10 46 16 45 84 3 3 3 97 9 94 61 86 63\n",
"10 1\n4 2 9 2 1 4 4 1 4 10\n",
"100 10\n59 95 5 20 91 78 30 76 32 82 3 84 38 92 19 65 79 39 7 81 49 98 29 4 28 71 67 55 99 65 53 58 31 61 4 59 5 55 33 41 81 55 58 23 95 98 60 62 54 94 47 33 20 67 31 67 34 26 47 96 96 64 31 21 49 58 82 15 73 15 42 94 100 8 50 31 77 37 68 65 83 54 28 92 68 24 16 12 55 34 28 16 68 76 1 96 52 70 91 90\n",
"1 1\n1\n",
"100 1\n7 75 3 62 10 31 43 96 31 62 13 76 93 85 38 96 72 69 67 44 88 53 35 21 67 18 34 2 60 14 23 56 95 69 86 51 37 82 30 48 64 10 61 8 60 74 26 100 48 56 98 85 76 5 25 98 73 78 55 4 44 53 10 8 12 10 78 25 58 47 19 45 67 86 72 90 93 78 77 36 20 69 8 73 88 4 66 30 47 92 88 51 20 33 25 50 95 78 10 69\n",
"2 2\n2 1\n",
"2 1\n2 2\n"
],
"output": [
"16",
"726975503",
"24",
"244208148",
"1",
"1",
"2",
"2"
]
}
|
CORRECT
|
cpp
|
#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
int n, k;
inline int ad(int& x, int& y) { return (x + y > P ? x + y - P : x + y); }
int A[200006], las[200006], pr[200006];
const int blo = 233;
int bel[200006];
pair<int, int> B[200006];
int S[200006];
int val[200006], gv[200006], lz[200006], cur;
void rebuild(int b) {
int l = b * blo - blo + 1, r = b * blo;
for (int i = (l), iend = (r); i <= iend; ++i) val[i] = val[i] + lz[b];
lz[b] = 0;
for (int i = (l), iend = (r); i <= iend; ++i) B[i] = make_pair(val[i], gv[i]);
sort(B + l, B + r + 1);
S[l] = B[l].second;
for (int i = (l + 1), iend = (r); i <= iend; ++i)
S[i] = ad(S[i - 1], B[i].second);
}
void add(int l, int r, int c) {
++l, ++r;
if (bel[l] == bel[r]) {
int b = bel[l];
for (int i = (l), iend = (r); i <= iend; ++i) val[i] += c;
rebuild(b);
return;
}
for (int i = (l), iend = (bel[l] * blo); i <= iend; ++i) val[i] += c;
rebuild(bel[l]);
for (int i = (r), iend = (bel[r] * blo - blo + 1); i >= iend; --i)
val[i] += c;
rebuild(bel[r]);
for (int i = (bel[l] + 1), iend = (bel[r] - 1); i <= iend; ++i) lz[i] += c;
}
int que() {
int as = 0;
for (int b = (1), bend = (bel[cur]); b <= bend; ++b) {
int l = (b - 1) * blo + 1, r = b * blo;
int ps =
upper_bound(B + l, B + r + 1, make_pair(k - lz[b], 0x3f3f3f3f)) - B - 1;
if (ps < l)
continue;
else
as = ad(as, S[ps]);
}
return as;
}
void gt(int p, int c) {
++p;
gv[p] = c;
rebuild(bel[p]);
}
int dp[200006];
void solve() {
cin >> n >> k;
for (int i = (1), iend = (n); i <= iend; ++i)
scanf("%d", A + i), las[i] = pr[A[i]], pr[A[i]] = i;
for (int i = (1), iend = (n + 1); i <= iend; ++i) bel[i] = (i - 1) / blo + 1;
dp[0] = 1;
for (int i = (1), iend = (n); i <= iend; ++i) {
cur = i + 1;
add(las[i] + 1, i, 1);
if (las[i]) add(las[las[i]] + 1, las[i], -1);
gt(i, dp[i - 1]);
dp[i] = que();
}
printf("%d\n", dp[n]);
}
signed main() { solve(); }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.