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 . 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 would be required, which Akito, by happy coincidence, had recently learned. represents the following procedure:
Write the number in base , let this representation be , where is the length of the base representation of the number .
Reverse the base representation, let this be .
Convert the number back to decimal and return it as the result.
The merchant's quest was to calculate the sum . Since this number can be quite large, only the remainder of when divided by 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 () — the number of test cases.
In the only line of each test case, two numbers and are given () — the magical number and the upper limit for summation.
Note that the sum of across all test cases is not bounded.
Output
For each test case, you need to output a single number — the remainder of when divided by .
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, . The number one in any numeral system is represented by a single digit, which means for any . Thus, .
In the fourth test case, . Let's calculate each term:
Thus, .
In the seventh test case, . Let's calculate each term:
Thus, .
解题思路
首先容易知道当 时, 的 进制就是 ,此时 就等于 。所以当 时, 这部分的答案就是 。
因此接下来我们只需要考虑 的情况。一个很关键的性质,当 时 在 进制下恰好只有两位,即 。这是因为此时 ,因此 在 进制下不超过 位。又因为 因此 在 进制下至少有 位。所以 在 进制下恰好只有两位。因此有
这部分可以用数论分块来处理,即分别找到使得 相同的一段连续区间 ,这样就有
我们只需把这样的 都找出来即可,这样区间的数量级为 。
剩下的就是当 的情况,直接暴力模拟即可。
AC 代码如下,时间复杂度为 :
#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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18745007
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球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++中构造函数的常见疑问