G. I've Been Flipping Numbers for 300 Years and Calculated the Sum

G. I've Been Flipping Numbers for 300 Years and Calculated the Sum

After three hundred years of slime farming, Akito finally obtained the magical number $n$. Upon reaching the merchant, he wanted to exchange the number for gold, but the merchant gave the hero a quest.

The merchant said that for the quest, the skill $\text{rev}(n, p)$ would be required, which Akito, by happy coincidence, had recently learned. $\text{rev}(n, p)$ represents the following procedure:

Write the number $n$ in base $p$, let this representation be $n = \overline{n_{\ell - 1} \ldots n_1 n_0}$, where $\ell$ is the length of the base $p$ representation of the number $n$.
Reverse the base $p$ representation, let this be $m = \overline{n_0 n_1 \ldots n_{\ell - 1}}$.
Convert the number $m$ back to decimal and return it as the result.
The merchant's quest was to calculate the sum $x = \sum\limits_{p = 2}^{k} \text{rev}(n, p)$. Since this number can be quite large, only the remainder of $x$ when divided by $10^9 + 7$ is required. The merchant also mentioned that the previous traveler had been calculating this sum for three hundred years and had not finished it. But you will help Akito finish it faster, right?

Input

The first line contains the number $t$ ($1 \le t \le 5000$) — the number of test cases.

In the only line of each test case, two numbers $n$ and $k$ are given ($1 \le n \le 3 \cdot 10^5, 2 \le k \le 10^{18}$) — the magical number and the upper limit for summation.

Note that the sum of $n$ across all test cases is not bounded.

Output

For each test case, you need to output a single number — the remainder of $x = \sum\limits_{p = 2}^{k} \text{rev}(n, p)$ when divided by $10^9 + 7$.

Example

Input

12
3 2
42 52
1 10
4 4
16 2
69 69
9 3
19 84
9982 44353
100000 1000000007
17 30
777 1000000000000000000

Output

3
7594
9
6
1
33471
10
2006
120792461
584502117
775
46058362

Note

In the third test case, $n = 1$. The number one in any numeral system is represented by a single digit, which means $\text{rev}(1, p) = 1$ for any $p \ge 2$. Thus, $x = \sum\limits_{p = 2}^{k} 1 = \sum\limits_{p = 2}^{10} 1 = 10 - 2 + 1 = 9$.

In the fourth test case, $x = \text{rev}(4, 2) + \text{rev}(4, 3) + \text{rev}(4, 4)$. Let's calculate each term:

  • $4 = 100_2 \rightarrow \text{rev}(4, 2) = 001_2 = 1$
  • $4 = 11_3 \rightarrow \text{rev}(4, 3) = 11_3 = 4$
  • $4 = 10_4 \rightarrow \text{rev}(4, 4) = 01_4 = 1$

Thus, $x = 1 + 4 + 1 = 6$.

In the seventh test case, $x = \text{rev}(9, 2) + \text{rev}(9, 3)$. Let's calculate each term:

  • $9 = 1001_2 \rightarrow \text{rev}(9, 2) = 1001_2 = 9$
  • $9 = 100_3 \rightarrow \text{rev}(9, 3) = 001_3 = 1$

Thus, $x = 9 + 1 = 10$.

 

解题思路

  首先容易知道当 $p>n$ 时,$n$ 的 $p$ 进制就是 $n$,此时 $\text{rev}(n, p)$ 就等于 $n$。所以当 $k > n$ 时,$n+1 \leq p \leq k$ 这部分的答案就是 $(k-n) \cdot n$。

  因此接下来我们只需要考虑 $k \leq n$ 的情况。一个很关键的性质,当 $\left\lfloor \sqrt{n} \right\rfloor < p \leq k$ 时 $n$ 在 $p$ 进制下恰好只有两位,即 $(\left\lfloor n/p \right\rfloor \;\; n \bmod p)_p$。这是因为此时 $p^2 > n$,因此 $n$ 在 $p$ 进制下不超过 $2$ 位。又因为 $p \leq n$ 因此 $n$ 在 $p$ 进制下至少有 $1$ 位。所以 $n$ 在 $p$ 进制下恰好只有两位。因此有

\begin{align*}
&\sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{\text{rev}(n, p)} \\
=& \sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{(n \bmod p) \cdot p + \left\lfloor n/p \right\rfloor} \\
=& \sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{(n-\left\lfloor n/p \right\rfloor \cdot p) \cdot p} + \sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{\left\lfloor n/p \right\rfloor} \\
=& \sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{np} - \sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{\left\lfloor n/p \right\rfloor \cdot p^2} + \sum\limits_{p=\left\lfloor \sqrt{n} \right\rfloor + 1}^{k}{\left\lfloor n/p \right\rfloor}
\end{align*}

  $\left\lfloor n/p \right\rfloor$ 这部分可以用数论分块来处理,即分别找到使得 $\left\lfloor n/p \right\rfloor$ 相同的一段连续区间 $p \in [l, r]$,这样就有

\begin{align*}
&\sum\limits_{p=l}^{r}{np} - \sum\limits_{p=l}^{r}{\left\lfloor n/p \right\rfloor \cdot p^2} + \sum\limits_{p=l}^{r}{\left\lfloor n/p \right\rfloor} \\
=& n\sum\limits_{p=l}^{r}{p} - \left\lfloor n/l \right\rfloor \sum\limits_{p=l}^{r}{p^2} + \left\lfloor n/l \right\rfloor \sum\limits_{p=l}^{r}{1} \\
=& n \frac{(r-l+1)(l+r)}{2} - \left\lfloor n/l \right\rfloor \frac{r(r+1)(2r+1)-(l-1)l(2l-1)}{6} + \left\lfloor n/l \right\rfloor(r-l+1)
\end{align*}

  我们只需把这样的 $[l,r]$ 都找出来即可,这样区间的数量级为 $O(\sqrt{n})$。

  剩下的就是当 $p \leq \left\lfloor \sqrt{n} \right\rfloor$ 的情况,直接暴力模拟即可。

  AC 代码如下,时间复杂度为 $O(\sqrt{n})$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int mod = 1e9 + 7;

void solve() {
    LL n, m;
    cin >> n >> m;
    LL l = 2, ret = (m - min(n, m)) % mod * n % mod;
    m = min(n, m);
    while (l * l <= n && l <= m) {
        vector<int> p;
        int t = n;
        while (t) {
            p.push_back(t % l);
            t /= l;
        }
        for (int i = p.size() - 1, t = 1; i >= 0; i--) {
            ret = (ret + 1ll * p[i] * t) % mod;
            t = t * l % mod;
        }
        l++;
    }
    while (l <= m) {
        LL r = min(m, n / (n / l));
        ret = (ret + (r - l + 1) * (n / l) + n * (r - l + 1) % mod * (l + r) % mod * 500000004 - (n / l) * (r * (r + 1) % mod * (2 * r + 1) % mod - (l - 1) * l % mod * (2 * l - 1) % mod) % mod * 166666668) % mod;
        l = r + 1;
    }
    cout << (ret + mod) % mod << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 1006 (Div. 3) Editorial:https://codeforces.com/blog/entry/140039

posted @ 2025-03-01 16:50  onlyblues  阅读(43)  评论(0)    收藏  举报
Web Analytics