#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < (int)(n); i++)
const int maxN = 1e7;
int64_t sum[maxN + 1];
int main(int argc, char *argv[]) {
int n, m;
int x, y, a;
int z, t, b;
cin >> n >> x >> y >> a;
assert(1 <= n && n <= maxN);
forn(i, n) {
sum[i + 1] = sum[i] + a;
a = (a * x + y) & ((1 << 16) - 1);
}
int64_t res = 0;
cin >> m >> z >> t >> b;
forn(i, m) {
int l = b % n; b = (b * z + t) & ((1 << 30) - 1);
int r = b % n; b = (b * z + t) & ((1 << 30) - 1);
if (l > r)
swap(l, r);
res += sum[r + 1] - sum[l];
}
cout << res << endl;
}
|