Luogu2261 [CQOI2007]余数求和

https://www.luogu.com.cn/problem/P2261

数论分块

将原式变形,很容易得到以下式子:

\[G(n,k)=n*k-\sum_{i=1}^n \lfloor \frac{k}{i} \rfloor \times i \]

对于\(\lfloor\frac{k}{i}\rfloor=x\),可得出\(\max i=\lfloor \frac{k}{x} \rfloor\)

计算每一块的\(l,r\),高斯求和即可

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,k,r;
long long ans;
int main()
{
    scanf("%d%d",&n,&k);
    ans=(long long)n*(long long)k;
    for (int l=1;l<=n;l=r+1)
    {
        if (k/l==0)
            break;
        r=k/(k/l);
        r=min(r,n);
        ans-=(long long)(k/l)*(long long)(l+r)*(long long)(r-l+1)/2LL;
    }
    cout << ans << endl;
    return 0;
}
posted @ 2020-07-20 19:17  GK0328  阅读(86)  评论(0编辑  收藏  举报