G33 整除分块(数论分块)

视频链接:https://www.bilibili.com/video/BV1pP4y127tC/

Luogu P2261 [CQOI2007]余数求和

#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long LL;

int main(){
  LL n, k, l, r, res;
  scanf("%lld%lld", &n, &k);
  res = n*k;
  for(l=1; l<=n; l=r+1){
    if(k/l == 0) break;
    r = min(k/(k/l),n); 
    res -= (k/l)*(r-l+1)*(l+r)/2;
  }
  printf("%lld", res);
  return 0;
}

 

posted @ 2022-11-05 18:30  董晓  阅读(617)  评论(0)    收藏  举报