【題解】Codeforces 1301C. Ayoub’s function

【題目敘述】https://codeforces.com/contest/1301/problem/C

#include <iostream>
using namespace std;
 
long long t, n, m, sz, lft;
long long ans;
 
int main() {
    cin >> t;
    while (t--){
        cin >> n >> m;
        if (n % 2 == 0) ans = (1+n) * (n/2);
        else ans = ((1+n)/2) * n;
        if (m == 0){
            cout << 0 << "\n";
            continue;
        }
        sz = (n-m) / (m+1);
        lft = (n-m) % (m+1);
        for (int i = 0; i < lft; i++){
            ans -= ((sz+2) * (sz+1)) / 2;
        }
        for (int i = m+1-lft; i > 0; i--){
            ans -= ((sz+1) * sz) / 2;
        }
        cout << ans << "\n";
    }
}
分享本文 Share with friends