ABOUT ME

Today
Yesterday
Total
  • 1301-C, *1700
    코드포스 2021. 12. 24. 02:34
     

    Problem - C - Codeforces

     

    codeforces.com

     

    풀이

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    #include <bits/stdc++.h>
    #define endl "\n"
    #define ooop(i, n) for(int i = 0; i < n; i++)
    #define loop(i, n) for(int i = 1; i <= n; i++)
    #define all(v) (v).begin(), (v).end()
     
    using namespace std;
    typedef long long ll;
    typedef pair<intint> pi;
    typedef pair<ll, ll> pl;
     
    map<int, ll> dp;
     
    ll H(ll n)
    {
        if(dp[n] == 0) {
            ll ret = (n+1)*n/2;
            dp[n] = ret;
        }
        return dp[n];
    }
     
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0), cout.tie(0);
     
        int t;
        cin >> t;
        while(t--){
            int n, m;
            cin >> n >> m;
            if((n+1)/2 >= n-m) cout << H(n) - (n-m) << endl;
            else{
                ll res = H(n);
                int s = m+1;
                int small = (n-m)/s;
                int big = small + 1;
                int big_cnt = n-m-small*s;
                int small_cnt = s - big_cnt;
                res -= big_cnt*H(big);
                res -= small_cnt*H(small);
                cout << res << endl;
            }
     
        }
     
        return 0;

     

    '코드포스' 카테고리의 다른 글

    1615-A  (0) 2021.12.25
    1301-B, *1500  (0) 2021.12.24
    1301-D, *2000  (0) 2021.12.24
    1295-D, *1800  (0) 2021.11.10
    1295-C, *1600  (0) 2021.11.10

    댓글

Designed by Tistory.