【BZOJ1257】余数之和
k/i的商只有sqrt(k)个,并且如果k div i == k div (i+1) == k div (i+2) == ... == k div j,那么k mod i , k mod (i+1) , k mod (i+2) , ... , k mod j是一个等差数列。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL n,k,ans; int main() { scanf("%lld%lld",&n,&k); for (LL l=1,r=1;l<=n;l++) { LL c=k/l,p=k-c*l,wl,wr; if (c==0) r=n,wl=p,wr=p; else r=min(n,l+p/c),wl=p,wr=p-c*(r-l); ans+=(wl+wr)*(r-l+1)/2; l=r; } printf("%lld\n",ans); return 0; }