exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
sequencelengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 129d326248a90064d051a0a1e90ec874 | train_110.jsonl | 1635604500 | For an array $$$b$$$ of $$$n$$$ integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make $$$b$$$ non-decreasing: Select an index $$$i$$$ such that $$$1 \le i \le |b|$$$, where $$$|b|$$$ is the current length of $$$b$$$. Replace $$$b_i$$$ with two elements $$$x$$$ and $$$y$$$ such that $$$x$$$ and $$$y$$$ both are positive integers and $$$x + y = b_i$$$. This way, the array $$$b$$$ changes and the next operation is performed on this modified array. For example, if $$$b = [2, 4, 3]$$$ and index $$$2$$$ gets selected, then the possible arrays after this operation are $$$[2, \underline{1}, \underline{3}, 3]$$$, $$$[2, \underline{2}, \underline{2}, 3]$$$, or $$$[2, \underline{3}, \underline{1}, 3]$$$. And consequently, for this array, this single operation is enough to make it non-decreasing: $$$[2, 4, 3] \rightarrow [2, \underline{2}, \underline{2}, 3]$$$.It's easy to see that every array of positive integers can be made non-decreasing this way.YouKn0wWho has an array $$$a$$$ of $$$n$$$ integers. Help him find the sum of extreme values of all nonempty subarrays of $$$a$$$ modulo $$$998\,244\,353$$$. If a subarray appears in $$$a$$$ multiple times, its extreme value should be counted the number of times it appears.An array $$$d$$$ is a subarray of an array $$$c$$$ if $$$d$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.StringTokenizer;
public class A1603 {
private static final int P = 998244353;
private static final int M = 100000;
private static final int SQ = 800;
private static final int SQ2 = 400;
private static int[] answer = new int[M + 1];
private static HashMap<Integer, Integer>[] g = new HashMap[M + 1];
private static int countAnswer(int n, int[] a) {
if (answer[n] < 0) {
if (n == 0) {
answer[0] = 0;
} else {
answer[n] = (countAnswer(n - 1, a) + countG(n, a[n], a)) % P;
}
}
return answer[n];
}
private static int countG(int n, int last, int[] a) {
if (!g[n].containsKey(last)) {
if (n == 0) {
g[n].put(last, 0);
} else {
long ans = (getMoves(a[n - 1], last) * 1l * n) % P;
ans = (ans + countG(n - 1, getB1(a[n - 1], last), a)) % P;
g[n].put(last, (int) (ans % P));
}
}
return g[n].get(last);
}
private static int getB1(int a, int b) {
return a / ceil(a, b);
}
private static int getMoves(int a, int b) {
return ceil(a, b) - 1;
}
private static int ceil(int a, int b) {
return (a + b - 1) / b;
}
private static int getIndex(int num, int last) {
if (last * 1l * last >= num) {
return num / last;
}
return SQ2 + last;
}
private static int count(int[] a, int n) {
// answer = new int[n];
// g = new HashMap[n];
// for (int i = 0; i < n; i++) {
// answer[i] = -1;
// g[i] = new HashMap<>();
// }
// return countAnswer(n - 1, a);
int ans = 0;
int[][] g = new int[2][SQ + 1];
int active = 0;
for (int i = 1; i < n; i++) {
// compute g[]
for (int j = 1; j <= SQ; j++) {
g[active][j] = 0;
}
for (int j = 1; j <= SQ2; j++) {
int last = a[i] / j;
if (last == 0) {
g[active][j] = 0;
continue;
}
long res = (getMoves(a[i - 1], last) * 1l * i) % P;
res = (res + g[active ^ 1][getIndex(a[i - 1], getB1(a[i - 1], last))]) % P;
g[active][j] = (int) res;
}
for (int j = SQ2 + 1; j <= SQ; j++) {
// last = (j - sq2), if [a[i] / [a[i] / (j - sq2)]] == j - sq2.
int div = a[i] / (j - SQ2);
if (div <= 0 || a[i] / div != j - SQ2) {
g[active][j] = 0;
continue;
}
int last = j - SQ2;
long res = (getMoves(a[i - 1], last) * 1l * i) % P;
res = (res + g[active ^ 1][getIndex(a[i - 1], getB1(a[i - 1], last))]) % P;
g[active][j] = (int) res;
}
ans = (ans + g[active][1]) % P;
active ^= 1;
}
return ans;
}
public static void main(String[] args) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int numTests = Integer.parseInt(rd.readLine());
for (int t = 0; t < numTests; t++) {
int n = Integer.parseInt(rd.readLine());
StringTokenizer st = new StringTokenizer(rd.readLine());
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
pw.println(count(a, n));
}
pw.flush();
pw.close();
rd.close();
}
}
| Java | ["4\n3\n5 4 3\n4\n3 2 1 4\n1\n69\n8\n7264 40515 28226 92776 35285 21709 75124 48163"] | 4 seconds | ["5\n9\n0\n117"] | NoteLet $$$f(l, r)$$$ denote the extreme value of $$$[a_l, a_{l+1}, \ldots, a_r]$$$.In the first test case, $$$f(1, 3) = 3$$$, because YouKn0wWho can perform the following operations on the subarray $$$[5, 4, 3]$$$ (the newly inserted elements are underlined):$$$[5, 4, 3] \rightarrow [\underline{3}, \underline{2}, 4, 3] \rightarrow [3, 2, \underline{2}, \underline{2}, 3] \rightarrow [\underline{1}, \underline{2}, 2, 2, 2, 3]$$$; $$$f(1, 2) = 1$$$, because $$$[5, 4] \rightarrow [\underline{2}, \underline{3}, 4]$$$; $$$f(2, 3) = 1$$$, because $$$[4, 3] \rightarrow [\underline{1}, \underline{3}, 3]$$$; $$$f(1, 1) = f(2, 2) = f(3, 3) = 0$$$, because they are already non-decreasing. So the total sum of extreme values of all subarrays of $$$a = 3 + 1 + 1 + 0 + 0 + 0 = 5$$$. | Java 11 | standard input | [
"dp",
"greedy",
"math",
"number theory"
] | f760f108c66f695e1e51dc6470d29ce7 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. | 2,300 | For each test case, print a single integer — the sum of extreme values of all subarrays of $$$a$$$ modulo $$$998\,244\,353$$$. | standard output | |
PASSED | 8503b883c5458421fae574d9b1acfe8c | train_110.jsonl | 1635604500 | For an array $$$b$$$ of $$$n$$$ integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make $$$b$$$ non-decreasing: Select an index $$$i$$$ such that $$$1 \le i \le |b|$$$, where $$$|b|$$$ is the current length of $$$b$$$. Replace $$$b_i$$$ with two elements $$$x$$$ and $$$y$$$ such that $$$x$$$ and $$$y$$$ both are positive integers and $$$x + y = b_i$$$. This way, the array $$$b$$$ changes and the next operation is performed on this modified array. For example, if $$$b = [2, 4, 3]$$$ and index $$$2$$$ gets selected, then the possible arrays after this operation are $$$[2, \underline{1}, \underline{3}, 3]$$$, $$$[2, \underline{2}, \underline{2}, 3]$$$, or $$$[2, \underline{3}, \underline{1}, 3]$$$. And consequently, for this array, this single operation is enough to make it non-decreasing: $$$[2, 4, 3] \rightarrow [2, \underline{2}, \underline{2}, 3]$$$.It's easy to see that every array of positive integers can be made non-decreasing this way.YouKn0wWho has an array $$$a$$$ of $$$n$$$ integers. Help him find the sum of extreme values of all nonempty subarrays of $$$a$$$ modulo $$$998\,244\,353$$$. If a subarray appears in $$$a$$$ multiple times, its extreme value should be counted the number of times it appears.An array $$$d$$$ is a subarray of an array $$$c$$$ if $$$d$$$ can be obtained from $$$c$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.io.UncheckedIOException;
import java.io.Closeable;
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 << 29);
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);
CExtremeExtension solver = new CExtremeExtension();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
}
static class CExtremeExtension {
int mod = 998244353;
int L = (int) 1e5;
IntegerArrayList prevList = new IntegerArrayList(L);
IntegerArrayList nextList = new IntegerArrayList(L);
long[] prevWay = new long[L];
long[] nextWay = new long[L];
long[] prevCost = new long[L];
long[] nextCost = new long[L];
Debug debug = new Debug(false);
public void process(int x, IntegerArrayList list) {
list.clear();
for (int i = 1, r; i <= x; i = r + 1) {
int v = x / i;
r = x / v;
assert r >= i;
list.add(v);
}
list.reverse();
}
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.ri();
int[] a = in.ri(n);
prevList.clear();
// prevListMap.clear();
long sumOfCost = 0;
for (int i = n - 1; i >= 0; i--) {
process(a[i], nextList);
debug.debug("i", i);
debug.debug("nextList", nextList);
int m = nextList.size();
for (int j = 0; j < m; j++) {
nextWay[j] = 0;
nextCost[j] = 0;
}
int[] prevListData = prevList.getData();
int prevSize = prevList.size();
int[] nextListData = nextList.getData();
int iter = 0;
for (int j = 0; j < prevSize; j++) {
int back = prevListData[j];
int block = (a[i] + back - 1) / back;
int f = a[i] / block;
while (nextListData[iter] < f) {
iter++;
}
int findex = iter;
nextWay[findex] += prevWay[j];
nextCost[findex] += prevCost[j] + prevWay[j] * (block - 1);
}
//add new end
int findex = nextList.size() - 1;
nextWay[findex]++;
for (int j = 0; j < m; j++) {
nextCost[j] %= mod;
nextWay[j] %= mod;
sumOfCost += nextCost[j];
}
debug.run(() -> {
debug.debug("nextWay", Arrays.copyOf(nextWay, m));
debug.debug("nextCost", Arrays.copyOf(nextCost, m));
});
{
IntegerArrayList tmp = prevList;
prevList = nextList;
nextList = tmp;
}
// {
// IntegerHashMap tmp = prevListMap;
// prevListMap = nextListMap;
// nextListMap = tmp;
// }
{
long[] tmp = prevCost;
prevCost = nextCost;
nextCost = tmp;
}
{
long[] tmp = prevWay;
prevWay = nextWay;
nextWay = tmp;
}
}
sumOfCost = DigitUtils.mod(sumOfCost, mod);
out.println(sumOfCost);
}
}
static class SequenceUtils {
public static void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
public static void reverse(int[] data, int l, int r) {
while (l < r) {
swap(data, l, r);
l++;
r--;
}
}
public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
if ((ar - al) != (br - bl)) {
return false;
}
for (int i = al, j = bl; i <= ar; i++, j++) {
if (a[i] != b[j]) {
return false;
}
}
return true;
}
}
static class IntegerArrayList implements Cloneable {
private int size;
private int cap;
private int[] data;
private static final int[] EMPTY = new int[0];
public int[] getData() {
return data;
}
public IntegerArrayList(int cap) {
this.cap = cap;
if (cap == 0) {
data = EMPTY;
} else {
data = new int[cap];
}
}
public IntegerArrayList(int[] data) {
this(0);
addAll(data);
}
public IntegerArrayList(IntegerArrayList list) {
this.size = list.size;
this.cap = list.cap;
this.data = Arrays.copyOf(list.data, size);
}
public IntegerArrayList() {
this(0);
}
public void reverse(int l, int r) {
SequenceUtils.reverse(data, l, r);
}
public void reverse() {
reverse(0, size - 1);
}
public void ensureSpace(int req) {
if (req > cap) {
while (cap < req) {
cap = Math.max(cap + 10, 2 * cap);
}
data = Arrays.copyOf(data, cap);
}
}
public void add(int x) {
ensureSpace(size + 1);
data[size++] = x;
}
public void addAll(int[] x) {
addAll(x, 0, x.length);
}
public void addAll(int[] x, int offset, int len) {
ensureSpace(size + len);
System.arraycopy(x, offset, data, size, len);
size += len;
}
public void addAll(IntegerArrayList list) {
addAll(list.data, 0, list.size);
}
public int size() {
return size;
}
public int[] toArray() {
return Arrays.copyOf(data, size);
}
public void clear() {
size = 0;
}
public String toString() {
return Arrays.toString(toArray());
}
public boolean equals(Object obj) {
if (!(obj instanceof IntegerArrayList)) {
return false;
}
IntegerArrayList other = (IntegerArrayList) obj;
return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
}
public int hashCode() {
int h = 1;
for (int i = 0; i < size; i++) {
h = h * 31 + Integer.hashCode(data[i]);
}
return h;
}
public IntegerArrayList clone() {
IntegerArrayList ans = new IntegerArrayList();
ans.addAll(this);
return ans;
}
}
static class DigitUtils {
private DigitUtils() {
}
public static int mod(long x, int mod) {
if (x < -mod || x >= mod) {
x %= mod;
}
if (x < 0) {
x += mod;
}
return (int) x;
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private OutputStream writer;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
private static Field stringBuilderValueField;
private char[] charBuf = new char[THRESHOLD * 2];
private byte[] byteBuf = new byte[THRESHOLD * 2];
static {
try {
stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value");
stringBuilderValueField.setAccessible(true);
} catch (Exception e) {
stringBuilderValueField = null;
}
stringBuilderValueField = null;
}
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(OutputStream writer) {
this.writer = writer;
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(long c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println(long c) {
return append(c).println();
}
public FastOutput println() {
return append('\n');
}
public FastOutput flush() {
try {
if (stringBuilderValueField != null) {
try {
byte[] value = (byte[]) stringBuilderValueField.get(cache);
writer.write(value, 0, cache.length());
} catch (Exception e) {
stringBuilderValueField = null;
}
}
if (stringBuilderValueField == null) {
int n = cache.length();
if (n > byteBuf.length) {
//slow
writer.write(cache.toString().getBytes(StandardCharsets.UTF_8));
// writer.append(cache);
} else {
cache.getChars(0, n, charBuf, 0);
for (int i = 0; i < n; i++) {
byteBuf[i] = (byte) charBuf[i];
}
writer.write(byteBuf, 0, n);
}
}
writer.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
writer.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class Debug {
private boolean offline;
private PrintStream out = System.err;
static int[] empty = new int[0];
public Debug(boolean enable) {
offline = enable && System.getSecurityManager() == null;
}
public void run(Runnable task) {
if (offline) {
task.run();
}
}
public Debug debug(String name, int x) {
if (offline) {
debug(name, "" + x);
}
return this;
}
public Debug debug(String name, String x) {
if (offline) {
out.printf("%s=%s", name, x);
out.println();
}
return this;
}
public Debug debug(String name, Object x) {
return debug(name, x, empty);
}
public Debug debug(String name, Object x, int... indexes) {
if (offline) {
if (x == null || !x.getClass().isArray()) {
out.append(name);
for (int i : indexes) {
out.printf("[%d]", i);
}
out.append("=").append("" + x);
out.println();
} else {
indexes = Arrays.copyOf(indexes, indexes.length + 1);
if (x instanceof byte[]) {
byte[] arr = (byte[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof short[]) {
short[] arr = (short[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof boolean[]) {
boolean[] arr = (boolean[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof char[]) {
char[] arr = (char[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof int[]) {
int[] arr = (int[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof float[]) {
float[] arr = (float[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof double[]) {
double[] arr = (double[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else if (x instanceof long[]) {
long[] arr = (long[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
} else {
Object[] arr = (Object[]) x;
for (int i = 0; i < arr.length; i++) {
indexes[indexes.length - 1] = i;
debug(name, arr[i], indexes);
}
}
}
}
return this;
}
}
static class FastInput {
private final InputStream is;
private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
public void populate(int[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = readInt();
}
}
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 String next() {
return readString();
}
public int ri() {
return readInt();
}
public int[] ri(int n) {
int[] ans = new int[n];
populate(ans);
return ans;
}
public int readInt() {
boolean rev = false;
skipBlank();
if (next == '+' || next == '-') {
rev = next == '-';
next = read();
}
int val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
return rev ? val : -val;
}
public String readString(StringBuilder builder) {
skipBlank();
while (next > 32) {
builder.append((char) next);
next = read();
}
return builder.toString();
}
public String readString() {
defaultStringBuf.setLength(0);
return readString(defaultStringBuf);
}
}
}
| Java | ["4\n3\n5 4 3\n4\n3 2 1 4\n1\n69\n8\n7264 40515 28226 92776 35285 21709 75124 48163"] | 4 seconds | ["5\n9\n0\n117"] | NoteLet $$$f(l, r)$$$ denote the extreme value of $$$[a_l, a_{l+1}, \ldots, a_r]$$$.In the first test case, $$$f(1, 3) = 3$$$, because YouKn0wWho can perform the following operations on the subarray $$$[5, 4, 3]$$$ (the newly inserted elements are underlined):$$$[5, 4, 3] \rightarrow [\underline{3}, \underline{2}, 4, 3] \rightarrow [3, 2, \underline{2}, \underline{2}, 3] \rightarrow [\underline{1}, \underline{2}, 2, 2, 2, 3]$$$; $$$f(1, 2) = 1$$$, because $$$[5, 4] \rightarrow [\underline{2}, \underline{3}, 4]$$$; $$$f(2, 3) = 1$$$, because $$$[4, 3] \rightarrow [\underline{1}, \underline{3}, 3]$$$; $$$f(1, 1) = f(2, 2) = f(3, 3) = 0$$$, because they are already non-decreasing. So the total sum of extreme values of all subarrays of $$$a = 3 + 1 + 1 + 0 + 0 + 0 = 5$$$. | Java 11 | standard input | [
"dp",
"greedy",
"math",
"number theory"
] | f760f108c66f695e1e51dc6470d29ce7 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^5$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. | 2,300 | For each test case, print a single integer — the sum of extreme values of all subarrays of $$$a$$$ modulo $$$998\,244\,353$$$. | standard output | |
PASSED | 87d3b5e6eca4cc9dd5ea2833c2f35558 | train_110.jsonl | 1635604500 | For two positive integers $$$l$$$ and $$$r$$$ ($$$l \le r$$$) let $$$c(l, r)$$$ denote the number of integer pairs $$$(i, j)$$$ such that $$$l \le i \le j \le r$$$ and $$$\operatorname{gcd}(i, j) \ge l$$$. Here, $$$\operatorname{gcd}(i, j)$$$ is the greatest common divisor (GCD) of integers $$$i$$$ and $$$j$$$.YouKn0wWho has two integers $$$n$$$ and $$$k$$$ where $$$1 \le k \le n$$$. Let $$$f(n, k)$$$ denote the minimum of $$$\sum\limits_{i=1}^{k}{c(x_i+1,x_{i+1})}$$$ over all integer sequences $$$0=x_1 \lt x_2 \lt \ldots \lt x_{k} \lt x_{k+1}=n$$$.Help YouKn0wWho find $$$f(n, k)$$$. | 1024 megabytes | 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.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.io.UncheckedIOException;
import java.io.Closeable;
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 {
new TaskAdapter().run();
}
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);
DArtisticPartition solver = new DArtisticPartition();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
}
static class DArtisticPartition {
int K = 20;
int N = (int) 1e5;
long[][] dp = new long[K][N + 1];
int[][] pts = new int[N + 1][];
long[][] suffix = new long[N + 1][];
int[][] start = new int[N + 1][];
Cost cost;
long inf = (long) 1e18;
long[] p;
public void preprocess() {
IntegerArrayList ptBuf = new IntegerArrayList(N + 1);
IntegerArrayList startBuf = new IntegerArrayList(N + 1);
for (int i = 1; i <= N; i++) {
ptBuf.clear();
startBuf.clear();
for (int j = 1, r; j <= i; j = r + 1) {
int v = i / j;
r = i / v;
ptBuf.add(v);
startBuf.add(j);
}
pts[i] = ptBuf.toArray();
start[i] = startBuf.toArray();
int m = pts[i].length;
suffix[i] = new long[m];
long last = i + 1;
long lastSum = 0;
for (int j = m - 1; j >= 0; j--) {
suffix[i][j] = lastSum + (last - start[i][j]) * p[pts[i][j]];
last = start[i][j];
lastSum = suffix[i][j];
}
}
}
public void dac(int level, int l, int r, int L, int R) {
if (l > r) {
return;
}
int m = (l + r) >>> 1;
int start = Math.min(m, R);
cost.reset(start + 1, m);
int bestChoice = -1;
long bestCost = inf;
for (int i = start; i >= L; i--) {
cost.move(i + 1);
long cand = cost.cost + dp[level - 1][i];
if (cand < bestCost) {
bestCost = cand;
bestChoice = i;
}
}
dp[level][m] = bestCost;
dac(level, l, m - 1, L, bestChoice);
dac(level, m + 1, r, bestChoice, R);
}
public DArtisticPartition() {
MultiplicativeFunctionSieve sieve = new MultiplicativeFunctionSieve(N);
int[] euler = sieve.getEuler();
p = new long[N + 1];
p[0] = euler[0];
for (int i = 1; i <= N; i++) {
p[i] = p[i - 1] + euler[i];
}
preprocess();
cost = new Cost();
SequenceUtils.deepFill(dp, inf);
dp[0][0] = 0;
for (int i = 1; i < K; i++) {
dac(i, 1, N, 0, N);
}
}
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.ri();
int k = in.ri();
if (k >= K) {
out.println(n);
return;
}
long ans = dp[k][n];
out.println(ans);
}
class Cost {
int r;
int l;
long cost;
public void reset(int L, int R) {
r = R;
l = L;
if (l > r) {
cost = 0;
return;
}
int high = BinarySearch.upperBound(start[r], 0, start[r].length - 1, l);
assert high > 0;
cost = 0;
int last = r + 1;
if (high < suffix[r].length) {
cost += suffix[r][high];
last = start[r][high];
}
cost += (last - l) * p[pts[r][high - 1]];
}
public void move(int L) {
while (l < L) {
cost -= p[r / l];
l++;
}
while (l > L) {
l--;
cost += p[r / l];
}
}
}
}
static class SequenceUtils {
public static void deepFill(Object array, long val) {
if (array == null) {
return;
}
if (!array.getClass().isArray()) {
throw new IllegalArgumentException();
}
if (array instanceof long[]) {
long[] longArray = (long[]) array;
Arrays.fill(longArray, val);
} else {
Object[] objArray = (Object[]) array;
for (Object obj : objArray) {
deepFill(obj, val);
}
}
}
public static boolean equal(int[] a, int al, int ar, int[] b, int bl, int br) {
if ((ar - al) != (br - bl)) {
return false;
}
for (int i = al, j = bl; i <= ar; i++, j++) {
if (a[i] != b[j]) {
return false;
}
}
return true;
}
}
static class BinarySearch {
private BinarySearch() {
}
public static int lowerBound(int[] arr, int l, int r, int target) {
while (l < r) {
int mid = DigitUtils.floorAverage(l, r);
if (arr[mid] >= target) {
r = mid;
} else {
l = mid + 1;
}
}
if (arr[l] < target) {
l++;
}
return l;
}
public static int upperBound(int[] arr, int l, int r, int target) {
return lowerBound(arr, l, r, target + 1);
}
}
static class DigitUtils {
private DigitUtils() {
}
public static int floorAverage(int x, int y) {
return (x & y) + ((x ^ y) >> 1);
}
}
static class FastOutput implements AutoCloseable, Closeable, Appendable {
private static final int THRESHOLD = 32 << 10;
private OutputStream writer;
private StringBuilder cache = new StringBuilder(THRESHOLD * 2);
private static Field stringBuilderValueField;
private char[] charBuf = new char[THRESHOLD * 2];
private byte[] byteBuf = new byte[THRESHOLD * 2];
static {
try {
stringBuilderValueField = StringBuilder.class.getSuperclass().getDeclaredField("value");
stringBuilderValueField.setAccessible(true);
} catch (Exception e) {
stringBuilderValueField = null;
}
stringBuilderValueField = null;
}
public FastOutput append(CharSequence csq) {
cache.append(csq);
return this;
}
public FastOutput append(CharSequence csq, int start, int end) {
cache.append(csq, start, end);
return this;
}
private void afterWrite() {
if (cache.length() < THRESHOLD) {
return;
}
flush();
}
public FastOutput(OutputStream writer) {
this.writer = writer;
}
public FastOutput append(char c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(int c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput append(long c) {
cache.append(c);
afterWrite();
return this;
}
public FastOutput println(int c) {
return append(c).println();
}
public FastOutput println(long c) {
return append(c).println();
}
public FastOutput println() {
return append('\n');
}
public FastOutput flush() {
try {
if (stringBuilderValueField != null) {
try {
byte[] value = (byte[]) stringBuilderValueField.get(cache);
writer.write(value, 0, cache.length());
} catch (Exception e) {
stringBuilderValueField = null;
}
}
if (stringBuilderValueField == null) {
int n = cache.length();
if (n > byteBuf.length) {
//slow
writer.write(cache.toString().getBytes(StandardCharsets.UTF_8));
// writer.append(cache);
} else {
cache.getChars(0, n, charBuf, 0);
for (int i = 0; i < n; i++) {
byteBuf[i] = (byte) charBuf[i];
}
writer.write(byteBuf, 0, n);
}
}
writer.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
writer.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class MultiplicativeFunctionSieve {
public int[] primes;
public boolean[] isComp;
public int primeLength;
public int[] smallestPrimeFactor;
public int[] expOfSmallestPrimeFactor;
int limit;
public int[] getEuler() {
int[] euler = new int[limit + 1];
euler[1] = 1;
for (int i = 2; i <= limit; i++) {
if (!isComp[i]) {
euler[i] = i - 1;
} else {
if (expOfSmallestPrimeFactor[i] == i) {
euler[i] = i - i / smallestPrimeFactor[i];
} else {
euler[i] = euler[expOfSmallestPrimeFactor[i]] * euler[i / expOfSmallestPrimeFactor[i]];
}
}
}
return euler;
}
public MultiplicativeFunctionSieve(int limit) {
this.limit = limit;
isComp = new boolean[limit + 1];
primes = new int[limit + 1];
expOfSmallestPrimeFactor = new int[limit + 1];
smallestPrimeFactor = new int[limit + 1];
primeLength = 0;
for (int i = 2; i <= limit; i++) {
if (!isComp[i]) {
primes[primeLength++] = i;
expOfSmallestPrimeFactor[i] = smallestPrimeFactor[i] = i;
}
for (int j = 0, until = limit / i; j < primeLength && primes[j] <= until; j++) {
int pi = primes[j] * i;
smallestPrimeFactor[pi] = primes[j];
expOfSmallestPrimeFactor[pi] = smallestPrimeFactor[i] == primes[j]
? (expOfSmallestPrimeFactor[i] * expOfSmallestPrimeFactor[primes[j]])
: expOfSmallestPrimeFactor[primes[j]];
isComp[pi] = true;
if (i % primes[j] == 0) {
break;
}
}
}
}
}
static class FastInput {
private final InputStream is;
private StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
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 String next() {
return readString();
}
public int ri() {
return readInt();
}
public int readInt() {
boolean rev = false;
skipBlank();
if (next == '+' || next == '-') {
rev = next == '-';
next = read();
}
int val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
return rev ? val : -val;
}
public String readString(StringBuilder builder) {
skipBlank();
while (next > 32) {
builder.append((char) next);
next = read();
}
return builder.toString();
}
public String readString() {
defaultStringBuf.setLength(0);
return readString(defaultStringBuf);
}
}
static class IntegerArrayList implements Cloneable {
private int size;
private int cap;
private int[] data;
private static final int[] EMPTY = new int[0];
public IntegerArrayList(int cap) {
this.cap = cap;
if (cap == 0) {
data = EMPTY;
} else {
data = new int[cap];
}
}
public IntegerArrayList(int[] data) {
this(0);
addAll(data);
}
public IntegerArrayList(IntegerArrayList list) {
this.size = list.size;
this.cap = list.cap;
this.data = Arrays.copyOf(list.data, size);
}
public IntegerArrayList() {
this(0);
}
public void ensureSpace(int req) {
if (req > cap) {
while (cap < req) {
cap = Math.max(cap + 10, 2 * cap);
}
data = Arrays.copyOf(data, cap);
}
}
public void add(int x) {
ensureSpace(size + 1);
data[size++] = x;
}
public void addAll(int[] x) {
addAll(x, 0, x.length);
}
public void addAll(int[] x, int offset, int len) {
ensureSpace(size + len);
System.arraycopy(x, offset, data, size, len);
size += len;
}
public void addAll(IntegerArrayList list) {
addAll(list.data, 0, list.size);
}
public int[] toArray() {
return Arrays.copyOf(data, size);
}
public void clear() {
size = 0;
}
public String toString() {
return Arrays.toString(toArray());
}
public boolean equals(Object obj) {
if (!(obj instanceof IntegerArrayList)) {
return false;
}
IntegerArrayList other = (IntegerArrayList) obj;
return SequenceUtils.equal(data, 0, size - 1, other.data, 0, other.size - 1);
}
public int hashCode() {
int h = 1;
for (int i = 0; i < size; i++) {
h = h * 31 + Integer.hashCode(data[i]);
}
return h;
}
public IntegerArrayList clone() {
IntegerArrayList ans = new IntegerArrayList();
ans.addAll(this);
return ans;
}
}
}
| Java | ["4\n6 2\n4 4\n3 1\n10 3"] | 3 seconds | ["8\n4\n6\n11"] | NoteIn the first test case, YouKn0wWho can select the sequence $$$[0, 2, 6]$$$. So $$$f(6, 2) = c(1, 2) + c(3, 6) = 3 + 5 = 8$$$ which is the minimum possible. | Java 11 | standard input | [
"divide and conquer",
"dp",
"number theory"
] | 2e7d4deeeb700d7ad008875779d6f969 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 3 \cdot 10^5$$$) — the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 10^5$$$). | 3,000 | For each test case, print a single integer — $$$f(n, k)$$$. | standard output |
Subsets and Splits