Source Code
#include "bits/stdc++.h"

using namespace std;
typedef long long ll;

const int MOD = 1e9 + 7;
inline int add(int a, int b) { return a + b >= MOD ? a + b - MOD : a + b; }
inline int mul(int a, int b) { return (ll)a * b % MOD; }

inline ll ssum(ll x) {
    ll ret = (x % MOD) * ((x + 1) % MOD) / 2;
    return ret % MOD;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll l, r, x, y, k;
    cin >> l >> r >> x >> y >> k;

    ll dif = r - l + 1;
    ll sum = (l * dif) % MOD + (dif * (dif - 1) / 2) % MOD;

    ll breaks = 0;
    ll first = l + k - (l % k == 0 ? k : l % k);
    ll last = r - r % k;
    ll numOfSets = (last - first) / k;
    
    ll fromLeftToFirst = ((first - l) * (l / k)) % MOD;
    if (first == l) fromLeftToFirst = fromLeftToFirst + (l/k-1);
    ll fromlastToRight = (r - last) * (last / k) %MOD;
    // cout << fromLeftToFirst << endl;
    // cout << first << "  " << last << endl;
    if (r >= k) {
        // TODO: first last in the same set
        if (first > last){
          breaks = dif*(r/k)%MOD;
          fromLeftToFirst = 0;
          fromlastToRight = 0;
        }else{
          ll base = (first / k) * (numOfSets * k) % MOD;
          breaks = (base + ((numOfSets * (numOfSets - 1) / 2) % MOD) * k) % MOD;
          
        }
    }

    ll solvingTime = sum * x % MOD;

    ll realBreak = (fromLeftToFirst + fromlastToRight + breaks)%MOD;
    cout << (solvingTime + realBreak * y % MOD) % MOD;
}
Copy
Practice Practice iyaad
GNU G++17
0 ms
556 KB
Wrong Answer