---
id: 748
title: "BZOJ1257:「CQOI2007」余数之和题解"
slug: bzoj1257-cqoi2007-sum-of-remainders
lang: zh
date: 2019-02-18T16:12:00
date_gmt: 2019-02-18T08:12:00
modified: 2023-11-11T14:50:42
status: publish
password_protected: false
category: { name: Olympiad in Informatics, slug: oi }
tags: [OI, 数学, 神仙, 数论, 分段, 取模]
original_url: https://kalorona.com/oi/bzoj1257cqoi2007余数之和题解/
archived: 2026-11-27
editor: classic
media: []
archive_notes:
  - "Body headings were <h1> in the original; demoted to level 2 so the page title is the only h1."
comments: []
---

## 神仙思路与证明

这道题可以将式子化简为：

$$
nk-\sum_{i=1}^{n} \lfloor \frac{k}{i} \rfloor * i
$$

第一部分乘积即可，第二部分直觉告诉我们$\lfloor \frac{k}{i} \rfloor$在一段连续的$i$上会相等，也就是说，是分段的。每一段的和可以直接用等差数列来求就行。我们来探究一下段区间的左右端点。我们假设目标区间是$[i,?]$，那么这一段区间的值肯定就是$\lfloor \frac{k}{i} \rfloor$，反求一下那么右端点就是$\lfloor \frac{k}{\lfloor \frac{k}{i} \rfloor} \rfloor$。可以证明右端点大于等于左端点。等差数列求和即可。

## 代码

```cpp
// BZ1257.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, k;
int main()
{
    scanf("%lld%lld", &n, &k);
    ll ans = n * k, gx;
    for (int x = 1; x <= n; x = gx + 1)
    {
        gx = (k / x) ? min(k / (k / x), n) : n;
        // x ~ gx same;
        ans -= (k / x) * (gx - x + 1) * (gx + x) / 2;
    }
    printf("%lld", ans);
    return 0;
}
```
