# Submit Info #2101

Problem Lang User Status Time Memory
Sum of Floor of Linear cpp beet AC 97 ms 1.86 MiB

ケース詳細
Name Status Time Memory
example_00 AC 0 ms 0.68 MiB
random_00 AC 25 ms 1.03 MiB
random_01 AC 97 ms 1.86 MiB
random_02 AC 74 ms 1.55 MiB
random_03 AC 52 ms 1.30 MiB
random_04 AC 24 ms 0.98 MiB
small_00 AC 16 ms 0.73 MiB
small_01 AC 47 ms 1.03 MiB
small_02 AC 32 ms 1.01 MiB
small_03 AC 23 ms 0.85 MiB
small_04 AC 9 ms 0.74 MiB

#define PROBLEM "https://judge.yosupo.jp/problem/sum_of_floor_of_linear" #include<bits/stdc++.h> using namespace std; #define call_from_test #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include<bits/stdc++.h> 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 #undef call_from_test signed main(){ 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; }