bzoj 1257 [CQOI2007]余数之和——数论分块

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

\( n\%i = n - \left \lfloor n/i \right \rfloor * i \)

注意 n<k 时当前块的右端点可能超过 n !

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

 

posted on 2018-12-13 08:40  Narh  阅读(157)  评论(0编辑  收藏  举报

导航