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> ar(1);
- vector <pair<unt,unt>> qr(1);
- unt sm;
- vector <vector <unt> > wrk;
- vector <unt> power;
- void make(){
- int lvl = 1,l,r;
- //cout<<"sz= "<<ar.size()<<'\n';
- while (pow(2,lvl) <= ar.size()){
- //cout<<"lvl = "<<lvl<<'\n';
- vector <unt> last = wrk[wrk.size() - 1];
- int len = pow(2, lvl);
- int hlf = len / 2;
- for (int i = 0; i<=ar.size()-len; ++i){
- // cout<<"i = "<<i<<'\n';
- l = i;
- r = i + len - 1;
- unt L = last[l];
- unt R = last[l+hlf];
- sm = min(L, R);
- power.push_back(sm);
- }
- wrk.push_back(power);
- power.clear();
- lvl++;
- }
- }
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- unt N,M,A,B;
- while (true){
- cin>>N>>M>>A>>B;
- if (N==0 && M==0 && A==0 && B==0) break;
- ar.clear();
- qr.clear();
- unt d;
- unt d1, d2;
- for (int i = 1;i<=N+M*2;++i){
- if (i <= N){
- d = A * i + B;
- ar.push_back(d);
- }
- else{
- d1 = (A * i + B);
- d1 %= N;
- d1++;
- i++;
- d2 = (A * i + B);
- d2 %= N;
- d2++;
- //cout<<"d1d2= "<<d1<<' '<<d2<<'\n';
- qr.push_back({d1, d2});
- }
- }
- for (int i = 0; i < ar.size();++i){
- sm = ar[i];
- power.push_back(sm);
- }
- wrk.push_back(power);
- power.clear();
- make();
- ll ans;
- ll tot = 0;
- /*cout<<"qr:\n";
- for (auto x: qr) cout<<x.first<<' '<<x.second<<'\n';
- cout<<'\n';*/
- for (auto Q: qr){
- unt a = min(Q.first, Q.second);
- unt b = max(Q.first, Q.second);
- a--;
- b--;
- //cv(ar);
- //cout<<"ab= "<<a<<' '<<b<<'\n';
- unt LVL = floor(log(b-a+1) / log(2));
- vector <unt> now = wrk[LVL];
- ll lft = now[a];
- ll rgt = now[b - pow(2,LVL) + 1];
- ans = min(lft, rgt);
- //cout<<"ans= "<<ans<<'\n';
- tot += ans;
- }
- cout<<tot<<"\n";
- wrk.clear();
- }
- }
Add Comment
Please, Sign In to add comment