Crane Machine cancer qwq
That Italy DESCRIPTION: Let \ (F_1 = 2 \) , \ (F_n =. 1 + \ FRAC {. 1} {2} (F _ {\ lfloor \ FRAC {n-} {2} \ rfloor} + F _ {\ lceil \ {2} {n-FRAC} \ rceil}) \) , \ (Q \) times query, each query \ (\ sum_ {i = 1 } ^ {n} (n-i + 1) \ cdot f_ {i } \) . \ (Q \ ^ Leq 10. 5, n-\ Leq 10 ^ {15} \) .
The \ (F_X \) of \ (X \) written in binary form, can be obtained:
\[f_{1}=1\]
\[f_{10}=1+\frac{1+1}{2}=2+\frac{0}{2}\]
\[f_{11}=1+\frac{1+2}{2}=2+\frac{1}{2}\]
\[f_{100}=1+\frac{2+2}{2}=3+\frac{0}{4}\]
\[f_{101}=1+\frac{2+\frac{5}{2}}{2}=3+\frac{1}{4}\]
\[f_{110}=1+\frac{\frac{5}{2}+\frac{5}{2}}{2}=3+\frac{2}{4}\]
\[f_{111}=1+\frac{\frac{5}{2}+ 3}{2}=3+\frac{3}{4}\]
\[\vdots\]
Observed that for the most significant bit \ (1 \) at the \ (n-\) binary digit of \ (S \) , \ (F_S = n-+ \ FRAC {S \ BMOD 2 ^ {n--1}} {2 {}. 1-n-^} \) .
A simple proof: for a \ (n \) bit binary number recursive solution \ (F_n \) . If it is even, then the recursion does not produce the denominator (ie, the same two sub-tasks, divided by the sum \ (2 \) unchanged), or multiply the denominator \ (2 \) . In the recursive solution to \ (1 \) before, there will be at most \ (n-1 \) binary LSB \ (1 \) number. Thus, for the (n-\) \ -bit binary number, \ (F_n \) is the denominator of most \ (. 1-n-2 ^ {} \) . Meanwhile, every time a recursive, will produce \ (1 + \) of the overall contribution integer part of \ (n-\) .
For molecules, apparently \ (x> y \) when there is \ (F_X> f_y \) . The most significant bit \ (1 \) at the \ (n-\) binary bits exactly \ (2 ^ {n-1 } \) a, can obviously only with \ ([0, 2 ^ { n-1} -1] \) molecule one correspondence between.
Can be enumerated \ (S \) the most significant bit \ (1 \) position \ (X \) . It found that for the same \ (X \) , the same integer part and a denominator, the numerator and two coefficients are arithmetic sequence, to set power series summation formula.
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
#define R register
#define ll long long
using namespace std;
const ll mod = 998244353, inv2 = 499122177, inv6 = 166374059;
int t;
ll l, r;
template <class T> inline void read(T &x) {
x = 0;
char ch = getchar(), w = 0;
while (!isdigit(ch)) w = (ch == '-'), ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
x = w ? -x : x;
return;
}
inline ll sum2(ll x) {
x %= mod;
return x * (2 * x + 1) % mod * (x + 1) % mod * inv6 % mod;
}
inline ll sum1(ll x) {
x %= mod;
return x * (x + 1) % mod * inv2 % mod;
}
inline ll solve(ll l) {
ll ret = 0, inv = 2;
for (R ll i = 0, p = 1; p <= l; ++i, p <<= 1) {
ll dn = p - 1, up = min((p << 1) - 1, l);
ret = (ret + ((sum2(dn) - sum2(up) + mod) % mod + (((l + 1 - p * i - p) % mod + mod) % mod * (sum1(up) - sum1(dn) + mod) % mod) % mod + (l + 1) % mod * ((p * i + p) % mod) % mod * ((up - dn) % mod) % mod) % mod * inv) % mod;
inv = inv * inv2 % mod;
}
return ret;
}
int main() {
read(t);
while (t--) {
read(l), read(r);
printf("%lld\n", solve(r - l));
}
return 0;
}