Submission #44304584


Source Code Expand

#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <string>

#include <cmath>
#include <queue>
#include <stack>

#include <unordered_set>
#include <unordered_map>
#include <array>
#include <iomanip>

#include <atcoder/all>

using namespace std;

#define LL long long

inline int read() {
    int x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

inline void write(int x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
}

inline void writeln(int x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
	putchar('\n');
}

inline LL readll() {
    LL x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') {
            f = -1;
        }
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

inline void writell(LL x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static LL sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
}

inline void writellln(LL x) {
	if (x < 0) {
		putchar('-'); x = -x;
	}
    static LL sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48); 
	putchar('\n');
}
template<class DataType1, class DataType2>
void write(pair<DataType1, DataType2> a, char endSplit = ' ') {
    cout << "(" << a.first << ", " << a.second << ")" << endSplit;
}
template<class DataType>
void write(vector<DataType> &a, char split = ' ', char endSplit = '\n') {
    for (int i = 0; i < a.size(); ++i) {
        cout << a[i] << split;
    }
    cout << endSplit;
}

using mint = atcoder::modint998244353;

LL getS(LL a, LL b, LL c, LL n) {
    if (n <= 0) {
        return 0;
    }
    if (n == 1) {
        return b / c;
    }
    LL tmp = 0;
    tmp += (a / c) * (n - 1) * n / 2;
    tmp += (b / c) * n;
    a = a % c;
    b = b % c;
    if (a == 0) {
        return tmp;
    } else {
        return tmp + getS(c, (a * n + b) % c, a, (a * n + b) / c);
    }
    // LL res = 0;
    // for (LL i = 0; i < n; ++i) {
    //     res += (b + i * a) / c;
    // }
    // return res;
}
int main() {
	// std::ios::sync_with_stdio(false);
    // std::cin.tie(0);
    int n = read();
    vector<LL> a(n);
    for (int i = 0; i < n; ++i) {
        a[i] = readll();
    }
    sort(a.begin(), a.end());
    LL last = 0;
    LL diff = 0;
    LL sum = 0;
    mint res = a[0] + 1;
    last = a[0];
    sum = a[0] * n;
    for (int i = 1; i < n; ++i) {
        // sum: bag size after lower last times
        // we can lower last + 1 ~ a[i] times
        if (a[i] == last) {
            continue;
        }
        // diff = a[i] - last;
        // lower last + 1 time
        LL diff = a[i] - last;
        LL d = n - i;
        // for (LL lower = last + 1; lower <= a[i]; ++lower) {
        // for (LL lower = 0; lower < diff; ++lower) {

        //     sum += (n - i);
        //     res += (sum / n) + 1;
        // }

        // sum + d, sum + 2d ... sum + diff * d
        // each over n
        // then +1
        res += diff;
        // (sum + d) / n, (sum + 2d) / n ... (sum + diff * d) / n
        res += getS(d, sum + d, n, diff);
        sum += (diff * d);
        last = a[i];
        // cout << res.val() << endl;
    }
    cout << res.val() << endl;
	return 0;
}

Submission Info

Submission Time
Task G - Redistribution of Piles
User AliceNan
Language C++ (GCC 9.2.1)
Score 625
Code Size 4156 Byte
Status AC
Exec Time 74 ms
Memory 4808 KiB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:155:8: warning: unused variable ‘diff’ [-Wunused-variable]
  155 |     LL diff = 0;
      |        ^~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 625 / 625
Status
AC × 4
AC × 30
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_small_00.txt, 01_small_01.txt, 01_small_02.txt, 01_small_03.txt, 01_small_04.txt, 02_n_small_00.txt, 02_n_small_01.txt, 02_n_small_02.txt, 02_n_small_03.txt, 02_n_small_04.txt, 03_random_00.txt, 03_random_01.txt, 03_random_02.txt, 03_random_03.txt, 03_random_04.txt, 03_random_05.txt, 03_random_06.txt, 03_random_07.txt, 03_random_08.txt, 03_random_09.txt, 04_corner_00.txt, 04_corner_01.txt, 04_corner_02.txt, 04_corner_03.txt, 04_corner_04.txt, 04_corner_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 5 ms 3440 KiB
00_sample_01.txt AC 2 ms 3528 KiB
00_sample_02.txt AC 2 ms 3436 KiB
00_sample_03.txt AC 2 ms 3576 KiB
01_small_00.txt AC 2 ms 3412 KiB
01_small_01.txt AC 2 ms 3428 KiB
01_small_02.txt AC 3 ms 3492 KiB
01_small_03.txt AC 2 ms 3612 KiB
01_small_04.txt AC 2 ms 3556 KiB
02_n_small_00.txt AC 2 ms 3532 KiB
02_n_small_01.txt AC 2 ms 3576 KiB
02_n_small_02.txt AC 2 ms 3440 KiB
02_n_small_03.txt AC 2 ms 3464 KiB
02_n_small_04.txt AC 2 ms 3632 KiB
03_random_00.txt AC 67 ms 4476 KiB
03_random_01.txt AC 74 ms 4616 KiB
03_random_02.txt AC 71 ms 4616 KiB
03_random_03.txt AC 71 ms 4752 KiB
03_random_04.txt AC 59 ms 4376 KiB
03_random_05.txt AC 74 ms 4808 KiB
03_random_06.txt AC 55 ms 4148 KiB
03_random_07.txt AC 71 ms 4680 KiB
03_random_08.txt AC 68 ms 4800 KiB
03_random_09.txt AC 73 ms 4796 KiB
04_corner_00.txt AC 10 ms 4696 KiB
04_corner_01.txt AC 17 ms 4600 KiB
04_corner_02.txt AC 3 ms 3436 KiB
04_corner_03.txt AC 22 ms 4760 KiB
04_corner_04.txt AC 16 ms 4164 KiB
04_corner_05.txt AC 20 ms 4616 KiB