library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/yosupo/sum_of_floor_of_linear.test.cpp

Depends on

Code

// verification-helper: PROBLEM https://judge.yosupo.jp/problem/sum_of_floor_of_linear

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../math/sum_of_floor.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int T;
  cin>>T;
  for(int t=0;t<T;t++){
    long long n,m,a,b;
    cin>>n>>m>>a>>b;
    cout<<sum_of_floor(n,m,a,b)<<"\n";
  }
  cout<<flush;
  return 0;
}
#line 1 "test/yosupo/sum_of_floor_of_linear.test.cpp"
// verification-helper: PROBLEM https://judge.yosupo.jp/problem/sum_of_floor_of_linear

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "math/sum_of_floor.cpp"

#line 3 "math/sum_of_floor.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
// sum_{i=0}^{n-1} (ai + b) // m
// 0 <= a, b
template<typename T>
T sum_of_floor(T n,T m,T a,T b){
  T res=0;
  if(a>=m){
    res+=(n-1)*n*(a/m)/2;
    a%=m;
  }
  if(b>=m){
    res+=n*(b/m);
    b%=m;
  }
  T y=(a*n+b)/m;
  T x=y*m-b;
  if(y==0) return res;
  res+=(n-(x+a-1)/a)*y;
  res+=sum_of_floor(y,a,m,(a-x%a)%a);
  return res;
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 8 "test/yosupo/sum_of_floor_of_linear.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int T;
  cin>>T;
  for(int t=0;t<T;t++){
    long long n,m,a,b;
    cin>>n>>m>>a>>b;
    cout<<sum_of_floor(n,m,a,b)<<"\n";
  }
  cout<<flush;
  return 0;
}
Back to top page