Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <string>
- #include <algorithm>
- using namespace std;
- using ll = long long;
- void cv(vector <ll> v){
- for (auto x: v) cout<<x<<' ';
- cout<<'\n';
- }
- ll inf = pow(2, 32);
- vector <ll> ar(1);
- vector <pair<ll,ll>> qr(1);
- struct sct{
- ll l, r, mn, mx;
- };
- void csc(sct f){
- cout<<f.l<<'-'<<f.r<<" ";
- //cout<<f.mn<<'/'<<f.mx<<'\n';
- }
- vector <vector <sct> > wrk;
- vector <sct> power;
- sct sm;
- void make(){
- int lvl = 1, lst_lvl;
- int l,r;
- while (pow(2,lvl) <= ar.size()){
- lst_lvl = lvl / 2;
- int lst_lvl = pow(2, lst_lvl);
- //cout<<"lvl = "<<lvl<<'\n';
- vector <sct> last = wrk[wrk.size() - 1];
- //cv(ar);
- /*cout<<"last:\n";
- for (auto x: last) csc(x);
- cout<<'\n';*/
- int len = pow(2, lvl);
- int hlf = len / 2;
- for (int i = 0;i<=ar.size()-len;++i){
- //cv(ar);
- l = i;
- r = i + len - 1;
- //cout<<ar[l]<<' '<<ar[r]<<'\n';
- //cout<<"l = "<<l<<" r = "<<r<<'\n';
- sct L = last[l];
- sct R = last[l+hlf];
- sm = {l, r, min(L.mn, R.mn), max(L.mx, R.mx)};
- power.push_back(sm);
- /*csc(L);
- csc(R);
- csc(sm);
- cout<<'\n';*/
- }
- /*cout<<"power:\n";
- for (auto x: power){
- csc(x);
- }
- cout<<'\n';*/
- wrk.push_back(power);
- power.clear();
- //cout<<"sz = "<<wrk.size()<<'\n';
- //cout<<"\n\n\n\n";
- lvl++;
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- ll N,M,A,B;
- while (true){
- cin>>N>>M>>A>>B;
- //cin>>N>>M>>A>>B;
- if (N==0 && M==0 && A==0 && B==0) break;
- ar.clear();
- qr.clear();
- ll d;
- ll d1, d2;
- for (int i = 1;i<=N+M*2;++i){
- if (i <= N){
- d = ( ((A % inf) * (i % inf)) % inf + (B % inf) ) % inf;
- ar.push_back(d);
- /* cout<<d<<'\n';
- if (d == rl[i-1]) cout<<"OK\n";
- else cout<<"NO\n";*/
- }
- else{
- d1 = ( ((A % inf) * (i % inf)) % inf + (B % inf) ) % inf % N;
- d1++;
- i++;
- d2 = ( ((A % inf) * (i % inf)) % inf + (B % inf) ) % inf % N;
- d2++;
- qr.push_back({d1, d2});
- /*cout<<d1<<' '<<d2<<'\n';
- if (d1 == qr[id].first && d2 == qr[id].second){
- cout<<"OK\n";
- }else{
- cout<<"NO\n";
- }*/
- //id++;
- }
- }
- //cout<<'\n';
- //cv(ar);
- for (int i = 0; i < ar.size();++i){
- sm = {i, i, ar[i], ar[i]};
- //cout<<ar[i]<<'\n';
- //csc(sm);
- power.push_back(sm);
- }
- wrk.push_back(power);
- power.clear();
- //cout<<'\n'<<'\n';
- make();
- ll ans;
- ll tot = 0;
- /*cout<<"wrk\n";
- for (int q=0;q<wrk.size();++q) {
- cout<<"pow= "<<q<<'\n';
- for (auto y: wrk[q]){
- csc(y);
- }cout<<'\n';
- }cout<<'\n';*/
- for (auto Q: qr){
- int a = min(Q.first, Q.second);
- int b = max(Q.first, Q.second);
- a--;
- b--;
- //cout<<a<<' '<<b<<'\n';
- //cout<<b - a + 1<<'\n';
- int LVL = floor(log(b-a+1) / log(2));
- //cout<<"LVL = "<<LVL<<'\n';
- vector <sct> now = wrk[LVL];
- sct lft = now[a];
- sct rgt = now[b - pow(2,LVL) + 1];
- ans = min(lft.mn, rgt.mn);
- tot += ans;
- //cout<<a<<' '<<b - pow(2,LVL) + 1<<'\n'<<'\n';
- }
- cout<<tot<<'\n';
- wrk.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement