bzoj 1257 余数之和 —— 数论分块

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1257

\( \sum\limits_{i=1}^{n}k\%i = \sum\limits_{i=1}^{n}k-\left \lfloor k/i \right \rfloor *i \)

然后数论分块做即可,注意 \( n>k \) 时右边界的取值。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,k;
ll ans;
ll cal(int l,int r){return (ll)(l+r)*(r-l+1)/2;}
int main()
{
  scanf("%d%d",&n,&k); ans=(ll)n*k;
  for(int i=1,j;i<=n/*&&i<=k*/;i=j+1)
    {
      if(k/i)j=min(n,k/(k/i));//min
      else j=n;
      ans-=(ll)(k/i)*cal(i,j);
    }
  printf("%lld\n",ans);
  return 0;
}

 

posted @ 2018-12-12 21:51  Zinn  阅读(136)  评论(0编辑  收藏  举报