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