Problem:
G - Proportional Representation
Problem Statement
An election selecting the members of the parliament in JAG Kingdom was held. The only system adopted in this country is the party-list proportional representation. In this system, each citizen votes for a political party, and the number of seats a party wins will be proportional to the number of votes it receives. Since the total number of seats in the parliament is an integer, of course, it is often impossible to allocate seats exactly proportionaly. In JAG Kingdom, the following method, known as the D'Hondt method, is used to determine the number of seats for each party.
Assume that every party has an unlimited supply of candidates and the candidates of each party are ordered in some way. To the $y$-th candidate of a party which received $x$ votes, assign the value $\dfrac{x}{y}$. Then all the candidates are sorted in the decreasing order of their assigned values. The first $T$ candidates are considered to win, where $T$ is the total number of seats, and the number of seats a party win is the number of its winning candidates.
The table below shows an example with three parties. The first party received $40$ votes, the second $60$ votes, and the third $30$ votes. If the total number of seats is $T = 9$, the first party will win $3$ seats, the second $4$ seats, and the third $2$ seats.
When selecting winning candidates, ties are broken by lottery; any tied candidates will have a chance to win. For instance, in the example above, if $T = 5$ then two candidates tie for the value $20$ and there are two possible outcomes:
The first party wins $2$ seats, the second $2$ seats, and the third $1$ seat.
The first party wins $1$ seat, the second $3$ seats, and the third $1$ seat.
You have just heard the results of the election on TV. Knowing the total number of valid votes and the number of seats each party won, you wonder how many votes each party received.
Given $N$, $M$, and $S_i$ ($1 \le i \le M$), denoting the total number of valid votes, the number of parties, and the number of seats the $i$-th party won, respectively, your task is to determine for each party the minimum and the maximum possible number of votes it received. Note that for some cases there might be no such situation with the given $N$, $M$, and $S_i$.
Input
The first line of the input contains two integers $N$ ($1 \le N \le 10^9$) and $M$ ($1 \le M \le 30{,}000$), where $N$ is the total number of valid votes and $M$ is the number of parties. $M$ lines follow, the $i$-th of which contains a single integer $S_i$ ($0 \le S_i \le 30{,}000$), representing the number of seats the $i$-th party won. You can assume that there exists $i$ with $S_i \ne 0$.
Output
If there is no situation with the given $N$, $M$, and $S_i$, display the word "impossible". Otherwise, output $M$ lines, each containing two integers. The first integer and the second integer in the $i$-th line should be the minimum and the maximum possible number of votes the $i$-th part
...(truncated)

Submitted solutions:

code_snippet: #include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int TEN(int n) {return (n==0)?1:10*TEN(n-1);}

using R = long double;
const R EPS = 1e-9;
const int MS = 30030;
using P = array<ll, 2>;

namespace StopWatch {
    clock_t st;
    bool f = false;
    void start() {
        f = true;
        st = clock();
    }
    int msecs() {
        assert(f);
        return (clock()-st)*1000 / CLOCKS_PER_SEC;
    }
}


vector<P> solve(int N, vector<ll> s) {
    int M = (int)s.size();
    vector<P> rng(M);
    fill_n(rng.begin(), M, P{0, N});
    ll ssm = 0;
    for (int i = 0; i < M; i++) {
        ssm += s[i];
    }
    int deg[MS];
    fill_n(deg, MS, 0);
    for (int i = 0; i < M; i++) {
        deg[s[i]]++;
    }
    map<int, ll> mp;
    StopWatch::start();
    for (int i = 0; i < M; i++) {
        if (s[i] == 0) continue;
        if (mp.count(s[i])) {
            rng[i][0] = mp[s[i]];
            continue;
        }
        if (i % 1000 == 0) {
//            printf("end %d %d\n", i, StopWatch::msecs());
        }
        ll l = (N*s[i]-1) / (ssm+M-1);

        while (l < N) {
            l++;
            ll sm = (ssm-s[i]+M-1)*l-(M-1)*(s[i]-1) - s[i]*(N-l);
/*            for (int j = 0; j < M; j++) {
                if (i == j) continue;
                sm += s[i]-1-(s[j]+1)*l%s[i];
                if (0 <= sm) {
                    break;
                }
            }*/
            sm -= s[i]-1-(s[i]+1)*l%s[i];
            int l2 = l%s[i];
            int now = 0;
            for (int j = 0; j < MS; j++) {
                now += l2; if (now >= s[i]) now -= s[i];
                sm += (s[i]-1-now)*deg[j];
                if (0 <= sm) break;
            }
            if (0 <= sm) break;
        }
        mp[s[i]] = rng[i][0] = l;
    }
    mp.clear();
    StopWatch::start();
    for (int i = 0; i < M; i++) {
        if (mp.count(s[i])) {
            rng[i][1] = mp[s[i]];
            continue;
        }
        if (i % 1000 == 0) {
//            printf("end %d %d\n", i, StopWatch::msecs());
        }
        ll l = (N*(s[i]+1)-(M-1)*s[i]) / (ssm+1);
        while (l <= N) {
            l++;
            ll sm = (ssm-s[i])*l - (s[i]+1)*(N-l);
            sm -= s[i] - (s[i]*l+s[i])%(s[i]+1);
            int l2 = l%(s[i]+1);
            int now = s[i];
/*            for (int j = 0; j < M; j++) {
                if (i == j) continue;
                sm += s[i] - (s[j]*l+s[i])%(s[i]+1);
                if (0 < sm) {
                    break;
                }
            }*/
            for (int j = 0; j < MS; j++) {
                sm += (s[i]-now)*deg[j];
                now += l2; if (now >= s[i]+1) now -= s[i]+1;
                if (0 < sm) break;
            }
            if (0 < sm) break;
        }
        mp[s[i]] = rng[i][1] = l-1;
    }
    return rng;
}


int main2() {
    int N, M;
    cin >> N >> M;
    assert(M <= 30000);
    vector<ll> s;
    for (int i = 0; i < M; i++) {
        int x;
        cin >> x;
        s.push_back(x);
    }
    int idx[M], ridx[M];
    iota(idx, idx+M, 0);
    sort(idx, idx+M, [&](const int &l, const int &r){
        return s[l] < s[r];
    });
    for (int i = 0; i < M; i++) {
        ridx[idx[i]] = i;
    }
    vector<ll> s2(M);
    for (int i = 0; i < M; i++) {
        s2[i] = s[idx[i]];
    }
    auto rng = solve(N, s2);
    for (int i = 0; i < M; i++) {
        if (rng[i][0] > rng[i][1]) {
            printf("impossible\n");
            return 0;
        }
    }
    for (int i = 0; i < M; i++) {
        printf("%lld %lld\n", rng[ridx[i]][0], rng[ridx[i]][1]);
    }
    return 0;
}

/*stack size???????????????*/
int main() {
    static ll eord, enew;
    const int sz = 32*1024*1024;
    static unsigned char *p = (unsigned char*)malloc(sz);
    enew = ((ll)(p+sz-1) & ~0xff);
    __asm__ volatile("mov %%rsp, %0" : "=r"(eord));
    __asm__ volatile("mov %0, %%rsp" : : "r"(enew));
    main2();
    __asm__ volatile("mov %0, %%rsp" : : "r"(eord));
    return 0;
}
accuracy: 1
time_ms: 1690
memory_kb: 8124
final_rank: 3

code_snippet: // AOJ #2683 Proportional Representation
// 2025.3.23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double EPS = 1e-8;

#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)

int Cin() {  // 整数の入力
	int n= 0; int c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void Cout(int n) { // 整数の表示（出力）
	char b[30];
	if (!n) pc('0');
	else {
		if (n < 0) pc('-'), n = -n;
		int i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
}

void Couts(const string &s) { for (char c : s) pc(c); pc('\n'); }


const int NMAX = 30005;
ll mn[NMAX], mx[NMAX];


ll flr(const double &x) { return (ll)(x + EPS); }

ll cel(const double &x) {
    double f = floor(x);
    return x <= f + EPS ? (ll)(f + EPS) : (ll)(f + EPS + 1);
}

pair<double, double> bs(function<ll(double)> f) {
    double lo = 0, hi = 2e9;
    pair<double, double> rsl;
    bool eq0 = false;
    for (int i = 0; i < 100; i++) {
        double mid = (lo + hi) / 2;
        ll v = f(mid);
        if (v < 0) lo = mid;
        else {
            if (v == 0) { eq0 = true; rsl.first = mid; }
            hi = mid;
        }
    }
    lo = 0, hi = 2e9;
    for (int i = 0; i < 100; i++) {
        double mid = (lo + hi) / 2;
        ll v = f(mid);
        if (v > 0) hi = mid;
        else {
            if (v == 0) rsl.second = mid;
            lo = mid;
        }
    }
    if (!eq0) { rsl.first = lo; rsl.second = hi; }
    return rsl;
}

int main(){
    int N = Cin(), M = Cin();
    vector<int> s(M);
    ll tot = 0;
    for (int i = 0; i < M; i++){
        s[i] = Cin();
        tot += s[i];
        mn[i] = 1000000000;
        mx[i] = 0;
    }

    double lo = 1, hi = 1e18;
    function<ll(double)> f0 = [&](const double &r) {
        ll res = -N;
        for (int x : s) res += cel(r * x);
        return res;
    };
    pair<double, double> p = bs(f0);
    hi = min(hi, f0(p.second) == 0 ? p.second : p.first);

    function<ll(double)> f3 = [&](const double &r) {
        ll res = -N;
        for (int x : s) res += flr(r * (x + 1));
        return res;
    };
    pair<double, double> q = bs(f3);
    lo = max(lo, f3(q.first) == 0 ? q.first : q.second);

    bool inv = true;
    if (tot <= N) {
        inv = false;
        int flg;
        double lo0 = lo, hi0 = hi;
        for (int i = 0; i < 100; i++){
            ll sum = 0;
            double r = (i == 99 ? (flg == 1 ? hi : lo) : (lo + hi) / 2);
            for (int j = 0; j < M; j++){
                sum += flr(r * (s[j] + 1));
            }
            flg = 0;
            for (int j = 0; j < M; j++){
                ll a = cel(r * s[j]);
                ll b = N - (sum - flr(r * (s[j] + 1)));
                mn[j] = min(mn[j], max(a, b));
                if (a < b) flg = 1;
                else if (a > b) flg = -1;
            }
            if (flg == 1) lo = r;
            else if (flg == -1) hi = r;
            else break;
        }
        lo = lo0; hi = hi0;
        for (int i = 0; i < 100; i++){
            ll sum = 0;
            double r = (i == 99 ? (flg == 1 ? hi : lo) : (lo + hi) / 2);
            for (int j = 0; j < M; j++){
                sum += cel(r * s[j]);
            }
            flg = 0;
            for (int j = 0; j < M; j++){
                ll a = flr(r * (s[j] + 1));
                ll b = N - (sum - cel(r * s[j]));
                mx[j] = max(mx[j], min(a, b));
                if (a < b) flg = 1;
                else if (a > b) flg = -1;
            }
            if (flg == 1) lo = r;
            else if (flg == -1) hi = r;
            else break;
        }
    }

    if (f0(p.first) == 0 && p.first < 1) {
        double r = p.second;
        bool ok = true;
        for (int i = 0; i < M; i++){
            if (cel(r * s[i]) != flr(r * (s[i] + 1))) { ok = false; break; }
        }
        if (ok) {
            inv = false;
            for (int i = 0; i < M; i++){
                mn[i] = min(mn[i], cel(r * s[i]));
                mx[i] = max(mx[i], cel(r * s[i]));
            }
        }
    }

    if (inv) Couts("impossible");
    else for (int i = 0; i < M; i++) Cout(mn[i]), pc(' '), Cout(mx[i]), pc('\n');
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4048
final_rank: 1

code_snippet: // AOJ #2683 Proportional Representation
// 2025.3.23

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double EPS = 1e-8;

const int N = 33333;
ll mn[N], mx[N];


ll flr(const double &x) { return (ll)(x + EPS); }

ll cel(const double &x) {
    double f = floor(x);
    return x <= f + EPS ? (ll)(f + EPS) : (ll)(f + EPS + 1);
}

pair<double, double> bs(function<ll(double)> f) {
    double lo = 0, hi = 2e9;
    pair<double, double> rsl;
    bool eq0 = false;
    for (int i = 0; i < 100; i++) {
        double mid = (lo + hi) / 2;
        ll v = f(mid);
        if (v < 0) lo = mid;
        else {
            if (v == 0) { eq0 = true; rsl.first = mid; }
            hi = mid;
        }
    }
    lo = 0, hi = 2e9;
    for (int i = 0; i < 100; i++) {
        double mid = (lo + hi) / 2;
        ll v = f(mid);
        if (v > 0) hi = mid;
        else {
            if (v == 0) rsl.second = mid;
            lo = mid;
        }
    }
    if (!eq0) { rsl.first = lo; rsl.second = hi; }
    return rsl;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;
    vector<int> s(m);
    ll tot = 0;
    for (int i = 0; i < m; i++){
        cin >> s[i];
        tot += s[i];
        mn[i] = 1000000000;
        mx[i] = 0;
    }

    double lo = 1, hi = 1e18;
    function<ll(double)> f0 = [&](const double &r) {
        ll res = -n;
        for (int x : s) res += cel(r * x);
        return res;
    };
    pair<double, double> p = bs(f0);
    hi = min(hi, f0(p.second) == 0 ? p.second : p.first);

    function<ll(double)> f3 = [&](const double &r) {
        ll res = -n;
        for (int x : s) res += flr(r * (x + 1));
        return res;
    };
    pair<double, double> q = bs(f3);
    lo = max(lo, f3(q.first) == 0 ? q.first : q.second);

    bool inv = true;
    if (tot <= n) {
        inv = false;
        int flg;
        double lo0 = lo, hi0 = hi;
        for (int i = 0; i < 100; i++){
            ll sum = 0;
            double r = (i == 99 ? (flg == 1 ? hi : lo) : (lo + hi) / 2);
            for (int j = 0; j < m; j++){
                sum += flr(r * (s[j] + 1));
            }
            flg = 0;
            for (int j = 0; j < m; j++){
                ll a = cel(r * s[j]);
                ll b = n - (sum - flr(r * (s[j] + 1)));
                mn[j] = min(mn[j], max(a, b));
                if (a < b) flg = 1;
                else if (a > b) flg = -1;
            }
            if (flg == 1) lo = r;
            else if (flg == -1) hi = r;
            else break;
        }
        lo = lo0; hi = hi0;
        for (int i = 0; i < 100; i++){
            ll sum = 0;
            double r = (i == 99 ? (flg == 1 ? hi : lo) : (lo + hi) / 2);
            for (int j = 0; j < m; j++){
                sum += cel(r * s[j]);
            }
            flg = 0;
            for (int j = 0; j < m; j++){
                ll a = flr(r * (s[j] + 1));
                ll b = n - (sum - cel(r * s[j]));
                mx[j] = max(mx[j], min(a, b));
                if (a < b) flg = 1;
                else if (a > b) flg = -1;
            }
            if (flg == 1) lo = r;
            else if (flg == -1) hi = r;
            else break;
        }
    }

    if (f0(p.first) == 0 && p.first < 1) {
        double r = p.second;
        bool ok = true;
        for (int i = 0; i < m; i++){
            if (cel(r * s[i]) != flr(r * (s[i] + 1))) { ok = false; break; }
        }
        if (ok) {
            inv = false;
            for (int i = 0; i < m; i++){
                mn[i] = min(mn[i], cel(r * s[i]));
                mx[i] = max(mx[i], cel(r * s[i]));
            }
        }
    }

    if (inv) cout << "impossible" << endl;
    else for (int i = 0; i < m; i++) cout << mn[i] << ' ' << mx[i] << endl;
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 4052
final_rank: 2