题解:CF1866L Lihmuf Balling

· · 题解

因为这道题的 n 很大但 m 很小,我们考虑枚举每个 k 并求出相应答案。

先将所有盒子编号减一,Pak Chanek 是按顺序取的,Lihmuf 要得分就需要保证其取的 j \times k \bmod N > j。

然后就是硬推,Lihmuf 拿球会以 \gcd(n,k) 次为一次循环,所以只需求解一下前 \frac{n}{\gcd(n,k)} 次即可。

那这个式子就很好列了。

\sum_{j}^{\frac{n}{\gcd(n,k)}} (j + 1) \times [j \times k \bmod N > j]

模运算不好搞,换成除法。

\sum_{j}^{\frac{n}{\gcd(n,k)}} (j + 1) \times [j \times k - \lfloor \frac{j \times k}{n} \rfloor \times n > j]

这个式子现在看起来还是 O(n) 的,但是 \lfloor \frac{j \times k}{n} \rfloor 的取值实际上只有 O(k) 个,可以直接枚举计算,总复杂度仅有 O(m^2)。

/******************************
 - @GavinCQTD / 2026-03-12 08:28:55
 - "L. Lihmuf Balling" From Codeforces - COMPFEST 15 - Preliminary Online Mirror (Unrated, ICPC Rules, Teams Preferred)
 - # https://codeforces.com/problemset/problem/1866/L
 - 2000 ms / 256 MB
******************************/

#include <algorithm>
#include <iostream>
#include <cassert>
#include <numeric>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

ll n,m,ans=1,mx;

int main(){
    cin >> n >> m;

    for(int k=2;k<=m;k++){
        ll cur=0;

        ll xval=n/gcd(n,k);

        for(int i=0;i<k;i++){
            ll x=max((i*n+k)/k,(i*n+(k-1))/(k-1)),y=min((i+1)*n/k,xval);
            if(x<=y){
                cur += (y-x+1)*((x+y)*k/2-i*n);
            }
        }

        // cerr << k << " " << cur << "\n";

        if(cur>mx){
            mx = cur;
            ans = k;
        }
    }

    cout << ans << "\n";

    return 0;
}