[Editorial] Doctor Doom's Timeline CrisisWCPC 2K26 Preliminary Round (Problems A – G)
Разница между en1 и en2, 45 символ(ов) изменены
↵
↵
<spoiler summary="Problem A: Hardest Matrix Leap Year">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> Given an operational year Y and a calibration interval k, find the number of years until the next strict future multiple of k.</li>↵
    <li><b>Key Intuition:</b> The next strict multiple is given by k &mdash; (Y % k).</li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Formula:</b> Writing Y = q * k + r with 0 &lt;= r &lt; k, the distance is k &mdash; r.</li>↵
    <li><b>Time Complexity:</b> O(1) per testcase.</li>↵
    <li><b>Space Complexity:</b> O(1) extra space.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
↵
using namespace std;↵
↵
void solve() {↵
    long long year, interval;↵
    cin &gt;&gt; year &gt;&gt; interval;↵
    long long remainder = year % interval;↵
    long long answer = interval &mdash; remainder;↵
    cout &lt;&lt; answer &lt;&lt; "\n";↵
}↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int test_cases;↵
    cin &gt;&gt; test_cases;↵
    while (test_cases--) {↵
        solve();↵
    }↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵
<spoiler summary="Problem B: Doctor Doom's Energy Stones">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> Given N stone weights and a container weight capacity K, maximize the total count of stones selected such that their total weight does not exceed K.</li>↵
    <li><b>Key Intuition:</b> To maximize the item count under a fixed capacity limit, always pick the lightest available items first (classic greedy strategy).</li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Detailed Explanation:</b> Sort the array of weights in ascending order w1 &lt;= w2 &lt;= ... &lt;= wN. Greedily accumulate weights starting from the smallest until adding the next stone exceeds capacity K.</li>↵
    <li><b>Time Complexity:</b> O(N log N) per testcase due to sorting.</li>↵
    <li><b>Space Complexity:</b> O(N) space.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
 #include &lt;vector&gt;↵
 #include &lt;algorithm&gt;↵
↵
using namespace std;↵
↵
void solve() {↵
    int n;↵
    long long capacity;↵
    cin &gt;&gt; n &gt;&gt; capacity;↵
    ↵
    vector&lt;long long&gt; weights(n);↵
    for (int i = 0; i &lt; n; i++) {↵
        cin &gt;&gt; weights[i];↵
    }↵
    ↵
    sort(weights.begin(), weights.end());↵
    ↵
    long long current_sum = 0;↵
    int count = 0;↵
    ↵
    for (int i = 0; i &lt; n; i++) {↵
        if (current_sum + weights[i] &gt; capacity) {↵
            break;↵
        }↵
        current_sum += weights[i];↵
        count++;↵
    }↵
    ↵
    cout &lt;&lt; count &lt;&lt; "\n";↵
}↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int test_cases;↵
    cin &gt;&gt; test_cases;↵
    while (test_cases--) {↵
        solve();↵
    }↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵
<spoiler summary="Problem C: The Doom Protocol">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> We may flip signs of a prefix of length p and a disjoint suffix of length q (p + q &lt;= n) of an array. Flipping position i costs 2|a_i| if i is even and |a_i| if i is odd. Find the minimum total cost so that the array sums to exactly S, or output -1.</li>↵
    <li><b>Key Intuition:</b> Flipping a_i changes the total sum by -2a_i. If total sum is T, the sum of flipped elements must equal K = (T &mdash; S) / 2. If T &mdash; S is odd, no solution exists.</li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Detailed Explanation:</b> Precompute prefix sums/costs and suffix sums/costs. As prefix length p decreases from n down to 0, maintain a hash map storing the minimum cost to achieve each suffix sum seen so far. For each prefix p, query the map for the required remaining sum K &mdash; prefSum[p].</li>↵
    <li><b>Time Complexity:</b> O(N) amortized per test case.</li>↵
    <li><b>Space Complexity:</b> O(N) extra space.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
 #include &lt;vector&gt;↵
 #include &lt;cmath&gt;↵
 #include &lt;unordered_map&gt;↵
↵
using namespace std;↵
↵
const long long INF = 1e18;↵
↵
void solve() {↵
    int n;↵
    long long target_sum;↵
    cin &gt;&gt; n &gt;&gt; target_sum;↵
    ↵
    vector&lt;long long&gt; a(n + 1);↵
    vector&lt;long long&gt; flip_cost(n + 1);↵
    long long total_sum = 0;↵
    ↵
    for (int i = 1; i &lt;= n; i++) {↵
        cin &gt;&gt; a[i];↵
        total_sum += a[i];↵
        long long val = abs(a[i]);↵
        flip_cost[i] = (i % 2 == 0) ? 2 * val : val;↵
    }↵
    ↵
    long long diff = total_sum &mdash; target_sum;↵
    if (diff % 2 != 0) {↵
        cout &lt;&lt; -1 &lt;&lt; "\n";↵
        return;↵
    }↵
    ↵
    long long required_sum = diff / 2;↵
    ↵
    vector&lt;long long&gt; pref_sum(n + 1, 0), pref_cost(n + 1, 0);↵
    for (int i = 1; i &lt;= n; i++) {↵
        pref_sum[i] = pref_sum[i &mdash; 1] + a[i];↵
        pref_cost[i] = pref_cost[i &mdash; 1] + flip_cost[i];↵
    }↵
    ↵
    vector&lt;long long&gt; suff_sum(n + 2, 0), suff_cost(n + 2, 0);↵
    for (int i = n; i &gt;= 1; i--) {↵
        suff_sum[i] = suff_sum[i + 1] + a[i];↵
        suff_cost[i] = suff_cost[i + 1] + flip_cost[i];↵
    }↵
    ↵
    unordered_map&lt;long long, long long&gt; best_suffix_cost;↵
    long long min_total_cost = INF;↵
    ↵
    for (int p = n; p &gt;= 0; p--) {↵
        int idx = p + 1;↵
        if (idx &lt;= n + 1) {↵
            long long s_val = suff_sum[idx];↵
            long long c_val = suff_cost[idx];↵
            if (best_suffix_cost.find(s_val) == best_suffix_cost.end() || c_val &lt; best_suffix_cost[s_val]) {↵
                best_suffix_cost[s_val] = c_val;↵
            }↵
        }↵
        ↵
        long long need = required_sum &mdash; pref_sum[p];↵
        if (best_suffix_cost.find(need) != best_suffix_cost.end()) {↵
            long long candidate = pref_cost[p] + best_suffix_cost[need];↵
            min_total_cost = min(min_total_cost, candidate);↵
        }↵
    }↵
    ↵
    if (min_total_cost &gt;= INF) {↵
        cout &lt;&lt; -1 &lt;&lt; "\n";↵
    } else {↵
        cout &lt;&lt; min_total_cost &lt;&lt; "\n";↵
    }↵
}↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int test_cases;↵
    cin &gt;&gt; test_cases;↵
    while (test_cases--) {↵
        solve();↵
    }↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵
<spoiler summary="Problem D: Saturn's Moon Alignment">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> For each energy value A, find the smallest k &gt;= 0 such that adding k^2 yields a perfect square. If impossible, set A = 1 with k = 0. Output max k used and modified array.</li>↵
    <li><b>Key Intuition:</b> We need M^2 &mdash; k^2 = A &lt;=&gt; (M &mdash; k)(M + k) = A. Setting u = M &mdash; k and v = M + k, u * v = A with u and v having the same parity. Minimizing k = (v &mdash; u) / 2 requires choosing divisor u &lt;= sqrt(A) as close to sqrt(A) as possible. If A % 4 == 2, no matching-parity factors exist, forcing A -&gt; 1.</li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Detailed Explanation:</b> Scan u downwards from floor(sqrt(A)) to 1. The first divisor found where A / u has matching parity gives the minimum gap v &mdash; u and thus minimal k.</li>↵
    <li><b>Time Complexity:</b> O(N sqrt(max A)) time.</li>↵
    <li><b>Space Complexity:</b> O(N) space.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
 #include &lt;vector&gt;↵
 #include &lt;cmath&gt;↵
 #include &lt;algorithm&gt;↵
↵
using namespace std;↵
↵
pair&lt;long long, long long&gt; solve_single(long long a) {↵
    long long sq = round(sqrt((double)a));↵
    if (sq * sq == a) {↵
        return {0, a};↵
    }↵
    if (a % 2 != 0 &amp;&amp; (a % 4 == 2)) {↵
        return {0, 1};↵
    }↵
    if (a % 4 == 2) {↵
        return {0, 1};↵
    }↵
    ↵
    long long best_u = -1;↵
    for (long long u = sq; u &gt;= 1; u--) {↵
        if (a % u == 0) {↵
            long long v = a / u;↵
            if ((v &mdash; u) % 2 == 0) {↵
                best_u = u;↵
                break;↵
            }↵
        }↵
    }↵
    ↵
    if (best_u == -1) {↵
        return {0, 1};↵
    }↵
    ↵
    long long v = a / best_u;↵
    long long k = (v &mdash; best_u) / 2;↵
    long long m = (v + best_u) / 2;↵
    return {k, m * m};↵
}↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int n;↵
    if (!(cin &gt;&gt; n)) return 0;↵
    ↵
    vector&lt;long long&gt; final_energies(n);↵
    long long max_k = 0;↵
    ↵
    for (int i = 0; i &lt; n; i++) {↵
        long long a;↵
        cin &gt;&gt; a;↵
        auto [k, energy] = solve_single(a);↵
        max_k = max(max_k, k);↵
        final_energies[i] = energy;↵
    }↵
    ↵
    cout &lt;&lt; max_k &lt;&lt; "\n";↵
    for (int i = 0; i &lt; n; i++) {↵
        cout &lt;&lt; final_energies[i] &lt;&lt; (i == n &mdash; 1 ? "" : " ");↵
    }↵
    cout &lt;&lt; "\n";↵
    ↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵
<spoiler summary="Problem E: Doctor Doom's Binary Dominion">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> Split binary string S of length N into K non-empty contiguous parts (each length &lt;= 31) to maximize sum(i * value(P_i)).</li>↵
    <li><b>Key Intuition:</b> Because each part length is bounded by 31 bits, we can use interval dynamic programming with memoization dp(pos, parts_left) where each state transitions into at most 31 choices.</li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Detailed Explanation:</b> Base case dp(N, 0) = 0. For state (pos, parts_left), iterate ending index end from pos up to min(N-1, pos + 30). Compute binary integer value iteratively and transition to dp(end + 1, parts_left &mdash; 1).</li>↵
    <li><b>Time Complexity:</b> O(31 * N * K) total operations.</li>↵
    <li><b>Space Complexity:</b> O(N * K) space for memo table.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
 #include &lt;vector&gt;↵
 #include &lt;string&gt;↵
 #include &lt;algorithm&gt;↵
 #include &lt;climits&gt;↵
↵
using namespace std;↵
↵
const long long INF = 1e18;↵
↵
long long solve_dp(const string &amp;s, int pos, int parts_left, int total_k, vector&lt;vector&lt;long long&gt;&gt; &amp;dp) {↵
    int n = s.length();↵
    if (pos == n) {↵
        return parts_left == 0 ? 0 : -INF;↵
    }↵
    if (n &mdash; pos &lt; parts_left || parts_left &lt;= 0) {↵
        return -INF;↵
    }↵
    if (dp[pos][parts_left] != -1) {↵
        return dp[pos][parts_left];↵
    }↵
    ↵
    int part_number = total_k &mdash; parts_left + 1;↵
    long long best = -INF;↵
    long long val = 0;↵
    ↵
    for (int end = pos; end &lt; min(n, pos + 31); end++) {↵
        val = (val &lt;&lt; 1) + (s[end] &mdash; '0');↵
        long long rest = solve_dp(s, end + 1, parts_left &mdash; 1, total_k, dp);↵
        if (rest != -INF) {↵
            best = max(best, part_number * val + rest);↵
        }↵
    }↵
    ↵
    return dp[pos][parts_left] = best;↵
}↵
↵
void solve() {↵
    int n, k;↵
    cin &gt;&gt; n &gt;&gt; k;↵
    string s;↵
    cin &gt;&gt; s;↵
    ↵
    vector&lt;vector&lt;long long&gt;&gt; dp(n + 1, vector&lt;long long&gt;(k + 1, -1));↵
    long long ans = solve_dp(s, 0, k, k, dp);↵
    cout &lt;&lt; ans &lt;&lt; "\n";↵
}↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int test_cases;↵
    cin &gt;&gt; test_cases;↵
    while (test_cases--) {↵
        solve();↵
    }↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵
<spoiler summary="Problem F: Quantum Chain">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> Grid pathfinding from top-left to bottom-right. Pick one light frequency from sorted base array B shifted by cell energy G_{r,c}. Chosen frequencies must strictly increase along path. Minimize total cost sum(10^6 &mdash; F) or print DOOMED.</li>↵
    <li><b>Key Intuition:</b> Maintain running prefix minimum arrays for candidate light frequencies. Monotonic two-pointer updates allow finding the best strictly smaller frequency predecessor from the top or left cell in amortized O(1) time.</li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Detailed Explanation:</b> Sort base frequencies B ascending. Iterating through cells (r, c), maintain idx_top and idx_left pointers advancing monotonically to query pref[c][idx_top &mdash; 1] and pref[c &mdash; 1][idx_left &mdash; 1]. Use rolling 1D DP arrays over grid rows to optimize memory.</li>↵
    <li><b>Time Complexity:</b> O(N * M * K) total time.</li>↵
    <li><b>Space Complexity:</b> O(M * K) memory using rolling arrays.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
 #include &lt;vector&gt;↵
 #include &lt;algorithm&gt;↵
↵
using namespace std;↵
↵
const long long INF = 1e18;↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int n, m, k;↵
    if (!(cin &gt;&gt; n &gt;&gt; m &gt;&gt; k)) return 0;↵
    ↵
    vector&lt;long long&gt; b(k);↵
    for (int i = 0; i &lt; k; i++) {↵
        cin &gt;&gt; b[i];↵
    }↵
    sort(b.begin(), b.end());↵
    ↵
    vector&lt;long long&gt; prev_grid(m), curr_grid(m);↵
    vector&lt;vector&lt;long long&gt;&gt; prev_pref(m, vector&lt;long long&gt;(k, INF));↵
    vector&lt;vector&lt;long long&gt;&gt; curr_pref(m, vector&lt;long long&gt;(k, INF));↵
    ↵
    for (int r = 0; r &lt; n; r++) {↵
        for (int c = 0; c &lt; m; c++) {↵
            cin &gt;&gt; curr_grid[c];↵
            long long g_curr = curr_grid[c];↵
            long long g_top = (r &gt; 0) ? prev_grid[c] : 0;↵
            long long g_left = (c &gt; 0) ? curr_grid[c &mdash; 1] : 0;↵
            ↵
            long long running_min = INF;↵
            int idx_top = 0, idx_left = 0;↵
            ↵
            for (int i = 0; i &lt; k; i++) {↵
                long long f = g_curr + b[i];↵
                ↵
                if (r &gt; 0) {↵
                    while (idx_top &lt; k &amp;&amp; g_top + b[idx_top] &lt; f) {↵
                        idx_top++;↵
                    }↵
                }↵
                if (c &gt; 0) {↵
                    while (idx_left &lt; k &amp;&amp; g_left + b[idx_left] &lt; f) {↵
                        idx_left++;↵
                    }↵
                }↵
                ↵
                long long best_prev = INF;↵
                if (r == 0 &amp;&amp; c == 0) {↵
                    best_prev = 0;↵
                } else {↵
                    if (r &gt; 0 &amp;&amp; idx_top &gt; 0) {↵
                        best_prev = min(best_prev, prev_pref[c][idx_top &mdash; 1]);↵
                    }↵
                    if (c &gt; 0 &amp;&amp; idx_left &gt; 0) {↵
                        best_prev = min(best_prev, curr_pref[c &mdash; 1][idx_left &mdash; 1]);↵
                    }↵
                }↵
                ↵
                if (best_prev != INF) {↵
                    running_min = min(running_min, best_prev + (1000000LL &mdash; f));↵
                }↵
                curr_pref[c][i] = running_min;↵
            }↵
        }↵
        ↵
        for (int c = 0; c &lt; m; c++) {↵
            prev_grid[c] = curr_grid[c];↵
            for (int i = 0; i &lt; k; i++) {↵
                prev_pref[c][i] = curr_pref[c][i];↵
            }↵
        }↵
    }↵
    ↵
    long long ans = prev_pref[m &mdash; 1][k &mdash; 1];↵
    if (ans &gt;= INF) {↵
        cout &lt;&lt; "DOOMED\n";↵
    } else {↵
        cout &lt;&lt; ans &lt;&lt; "\n";↵
    }↵
    ↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵
<spoiler summary="Problem G: Doom's Palindromic Matrix">↵
↵
<p><b>Problem Summary &amp; Key Intuition:</b></p>↵
<ul>↵
    <li><b>Summary:</b> Calculate range sums of distinct palindromic subsequence counts f_x(i) over Pascal matrix rows i in [L, R] truncated at column x modulo 998244353.</li>↵
    <li><b>Key Intuition:</b> The structural behavior of row i splits i into three main mathematical regions relative to limit x:↵
        <ol>↵
            <li><b>Region 1 (i &lt;= x):</b> Full symmetric row, geometric sums of powers of 2 based on even/odd parity.</li>↵
            <li><b>Region 2 (x &lt; i &lt;= 2x+1):</b> Partially folded row, capped symmetric pair counts.</li>↵
            <li><b>Region 3 (i &gt;= 2x+2):</b> Strictly increasing window, constant x + 1 distinct elements per row.</li>↵
        </ol>↵
    </li>↵
</ul>↵
↵
<p><b>Detailed Explanation &amp; Complexity:</b></p>↵
<ul>↵
    <li><b>Detailed Explanation:</b> Evaluate contributions from each region using logarithmic power function power_two and range exponent sum formula sum_pow.</li>↵
    <li><b>Time Complexity:</b> O(Q log MOD) time.</li>↵
    <li><b>Space Complexity:</b> O(1) space.</li>↵
</ul>↵
↵
<p><b>C++ Implementation:</b></p>↵
↵
<pre class="prettyprint lang-cpp">↵
 #include &lt;iostream&gt;↵
 #include &lt;algorithm&gt;↵
↵
using namespace std;↵
↵
const long long MOD = 998244353;↵
↵
long long power_two(long long exp) {↵
    long long res = 1;↵
    long long base = 2;↵
    while (exp &gt; 0) {↵
        if (exp % 2 == 1) res = (res * base) % MOD;↵
        base = (base * base) % MOD;↵
        exp /= 2;↵
    }↵
    return res;↵
}↵
↵
long long sum_pow(long long low, long long high) {↵
    if (low &gt; high) return 0;↵
    long long val_high = power_two(high + 1);↵
    long long val_low = power_two(low);↵
    return (val_high &mdash; val_low + MOD) % MOD;↵
}↵
↵
void solve_query() {↵
    long long l, r, x;↵
    if (!(cin &gt;&gt; l &gt;&gt; r &gt;&gt; x)) return;↵
    ↵
    long long total = 0;↵
    ↵
    long long r1_start = l, r1_end = min(r, x);↵
    if (r1_start &lt;= r1_end) {↵
        long long res = 0;↵
        ↵
        long long even_start = (r1_start % 2 == 0) ? r1_start : r1_start + 1;↵
        long long even_end = (r1_end % 2 == 0) ? r1_end : r1_end &mdash; 1;↵
        if (even_start &lt;= even_end) {↵
            long long count = (even_end &mdash; even_start) / 2 + 1;↵
            long long sp = sum_pow(even_start / 2, even_end / 2);↵
            res = (res + 3 * sp % MOD * (count % MOD)) % MOD;↵
        }↵
        ↵
        long long odd_start = (r1_start % 2 != 0) ? r1_start : r1_start + 1;↵
        long long odd_end = (r1_end % 2 != 0) ? r1_end : r1_end &mdash; 1;↵
        if (odd_start &lt;= odd_end) {↵
            long long count = (odd_end &mdash; odd_start) / 2 + 1;↵
            long long sp = sum_pow((odd_start + 1) / 2, (odd_end + 1) / 2);↵
            res = (res + 2 * sp % MOD * (count % MOD)) % MOD;↵
        }↵
        ↵
        total = (total + res) % MOD;↵
    }↵
    ↵
    long long r2_start = max(l, x + 1), r2_end = min(r, 2 * x + 1);↵
    if (r2_start &lt;= r2_end) {↵
        long long res = 0;↵
        ↵
        long long even_start = (r2_start % 2 == 0) ? r2_start : r2_start + 1;↵
        long long even_end = (r2_end % 2 == 0) ? r2_end : r2_end &mdash; 1;↵
        if (even_start &lt;= even_end) {↵
            long long sp = sum_pow(x &mdash; even_end / 2, x &mdash; even_start / 2);↵
            res = (res + 3 * sp) % MOD;↵
        }↵
        ↵
        long long odd_start = (r2_start % 2 != 0) ? r2_start : r2_start + 1;↵
        long long odd_end = (r2_end % 2 != 0) ? r2_end : r2_end &mdash; 1;↵
        if (odd_start &lt;= odd_end) {↵
            long long sp = sum_pow(x &mdash; (odd_end + 1) / 2, x &mdash; (odd_start + 1) / 2);↵
            res = (res + 2 * sp) % MOD;↵
        }↵
        ↵
        total = (total + res) % MOD;↵
    }↵
    ↵
    long long r3_start = max(l, 2 * x + 2), r3_end = r;↵
    if (r3_start &lt;= r3_end) {↵
        long long count = r3_end &mdash; r3_start + 1;↵
        total = (total + (x + 1) % MOD * (count % MOD)) % MOD;↵
    }↵
    ↵
    cout &lt;&lt; total &lt;&lt; "\n";↵
}↵
↵
int main() {↵
    ios_base::sync_with_stdio(false);↵
    cin.tie(nullptr);↵
    ↵
    int q;↵
    if (cin &gt;&gt; q) {↵
        while (q--) {↵
            solve_query();↵
        }↵
    }↵
    return 0;↵
}↵
</pre>↵
↵
</spoiler>↵
↵

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Ritesh369369 2026-09-24 13:05:49 45 Tiny change: '\n\n<spoiler' -> '\n<spoiler'
en1 Английский Ritesh369369 2026-09-24 12:59:36 20611 Initial revision (published)