BZOJ 1257 余数之和

Posted on 2016-04-03 17:05  ziliuziliu  阅读(95)  评论(0编辑  收藏  举报

原式=k*n-∑(k/i)*i(1<=i<=n),典型的分块处理。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long k,n,ans;
int main()
{
    scanf("%lld%lld",&n,&k);
    ans=k*n;
    long long left,right;left=1;
    while ((k/left!=0) && (left<=n))
    {
        right=min(k/(k/left),n);
        ans-=(left+right)*(right-left+1)*(k/right)/2;
        left=right+1;
    }
    printf("%lld\n",ans);
    return 0;
}