#include #include using namespace std; long long floor_sum(long long N,long long M,long long A,long long B) //Sum[floor((A*i+B)/M),{i,0,N-1}] { long long ans=0; if(B>=M) { long long BM=B/M; ans+=N*BM; B-=BM*M; } while(true) { long long AM=A/M; ans+=N*(N-1)/2*AM; A-=AM*M; if(A*N+B