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;
- using unt = unsigned int;
- void cv(vector <unt> v){
- for (auto x: v) cout<<x<<' ';
- cout<<'\n';
- }
- vector <unt> level(65);
- //степень - массив
- unt N=1,M=1,A=1,B=1;
- vector <vector <unt> > wrk(15, vector <unt> (25000));
- vector <unt> ar(25000);
- void make(unt &mx_lvl){
- //cout<<"making wrk\n";
- //for (int i=0;i<N;++i) cout<<wrk[0][i]<<' ';
- //cout<<"\n";
- for (unt i = 1; i <= mx_lvl; ++i){
- unt till = N - level[i];
- //cout<<"N= "<<N<<"\n";
- //cout<<"i= "<<i<<" level[i]= "<<level[i]<<" till= "<<till<<"\n";
- for (unt j = 0; j <= till ; ++j){
- //cout<<"j= "<<j<<"\n";
- unt l = j;
- unt r = j + level[i-1];
- unt L = wrk[i-1][l];
- unt R = wrk[i-1][r];
- //cout<<"lr= "<<l<<" "<<r<<"\n";
- //cout<<"LR= "<<L<<" "<<R<<"\n";
- wrk[i][j] = min(L, R);
- //cout<<"wrk[i][j]= "<<wrk[i][j]<<"\n";
- }
- //for (int h = 0; h <= till;++h) cout<<wrk[i][h]<<' ';
- //cout<<"\n";
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- for (int i = 0;i<65;++i){
- level[i] = pow(2,i);
- }
- while (true){
- cin>>N>>M>>A>>B;
- if (N==0 && M==0 && A==0 && B==0) break;
- unt d;
- unt a,b;
- //cout<<"make sequence\n";
- for (int i = 1;i<=N;++i){
- d = A * i + B;
- //ar.push_back(d);
- ar[i-1] = d;
- wrk[0][i-1]=d;
- }
- unt mx_lvl = log2(N);
- //cout<<"let's make wrk\n";
- make(mx_lvl);
- //cout<<"wrk MADE\n";
- ll ans;
- ll tot = 0;
- /*cout<<"qr:\n";
- for (auto x: qr) cout<<x.first<<' '<<x.second<<'\n';
- cout<<'\n';*/
- //в степень - массив
- /*cout<<"ar\n";
- for (int i=0;i<N;++i) cout<<ar[i]<<' ';
- cout<<"\n";*/
- for (unt i = N+1;i<=N+M*2;++i){
- a = (A * i + B);
- a %= N;
- //a++;
- i++;
- b = (A * i + B);
- b %= N;
- if (a > b) swap(a, b);
- //b++;
- //cout<<"ab= "<<a<<" "<<b<<"\n";
- unt LVL = log2(b-a+1);
- ll lft = wrk[LVL][a];
- ll rgt = wrk[LVL][b - level[LVL] + 1];
- ans = min(lft, rgt);
- //cout<<"ans= "<<ans<<'\n';
- tot += ans;
- }
- cout<<tot<<"\n";
- }
- //cout<<"p= "<<p<<'\n';
- }
- /*
- 25000 20000000 1000000000 1000000000
- 0 0 0 0
- */
Add Comment
Please, Sign In to add comment