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 rev(n,p) would be required, which Akito, by happy coincidence, had recently learned. rev(n,p) represents the following procedure:

Write the number n in base p, let this representation be n=n1n1n0¯, where is the length of the base p representation of the number n.
Reverse the base p representation, let this be m=n0n1n1¯.
Convert the number m back to decimal and return it as the result.
The merchant's quest was to calculate the sum x=p=2krev(n,p). Since this number can be quite large, only the remainder of x when divided by 109+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 (1t5000) — the number of test cases.

In the only line of each test case, two numbers n and k are given (1n3105,2k1018) — 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=p=2krev(n,p) when divided by 109+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 rev(1,p)=1 for any p2. Thus, x=p=2k1=p=2101=102+1=9.

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

  • 4=1002rev(4,2)=0012=1
  • 4=113rev(4,3)=113=4
  • 4=104rev(4,4)=014=1

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

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

  • 9=10012rev(9,2)=10012=9
  • 9=1003rev(9,3)=0013=1

Thus, x=9+1=10.

 

解题思路

  首先容易知道当 p>n 时,np 进制就是 n,此时 rev(n,p) 就等于 n。所以当 k>n 时,n+1pk 这部分的答案就是 (kn)n

  因此接下来我们只需要考虑 kn 的情况。一个很关键的性质,当 n<pknp 进制下恰好只有两位,即 (n/pnmodp)p。这是因为此时 p2>n,因此 np 进制下不超过 2 位。又因为 pn 因此 np 进制下至少有 1 位。所以 np 进制下恰好只有两位。因此有

p=n+1krev(n,p)=p=n+1k(nmodp)p+n/p=p=n+1k(nn/pp)p+p=n+1kn/p=p=n+1knpp=n+1kn/pp2+p=n+1kn/p

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

p=lrnpp=lrn/pp2+p=lrn/p=np=lrpn/lp=lrp2+n/lp=lr1=n(rl+1)(l+r)2n/lr(r+1)(2r+1)(l1)l(2l1)6+n/l(rl+1)

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

  剩下的就是当 pn 的情况,直接暴力模拟即可。

  AC 代码如下,时间复杂度为 O(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 @   onlyblues  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2024-03-01 D. Pinball
2023-03-01 D. Serval and Shift-Shift-Shift
2023-03-01 最大数量
2022-03-01 糖果传递
2022-03-01 均值不等式证明
2021-03-01 关于C++中构造函数的常见疑问
Web Analytics
点击右上角即可分享
微信分享提示