C. Problem for Nazar
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for his outstanding mathematical abilities. Today a math teacher gave him a very difficult task.
Consider two infinite sets of numbers. The first set consists of odd positive numbers (1,3,5,7,…1,3,5,7,…), and the second set consists of even positive numbers (2,4,6,8,…2,4,6,8,…). At the first stage, the teacher writes the first number on the endless blackboard from the first set, in the second stage — the first two numbers from the second set, on the third stage — the next four numbers from the first set, on the fourth — the next eight numbers from the second set and so on. In other words, at each stage, starting from the second, he writes out two times more numbers than at the previous one, and also changes the set from which these numbers are written out to another.
The ten first written numbers: 1,2,4,3,5,7,9,6,8,101,2,4,3,5,7,9,6,8,10. Let's number the numbers written, starting with one.
The task is to find the sum of numbers with numbers from ll to rr for given integers ll and rr. The answer may be big, so you need to find the remainder of the division by 10000000071000000007 (109+7109+7).
Nazar thought about this problem for a long time, but didn't come up with a solution. Help him solve this problem.
Input
The first line contains two integers ll and rr (1≤l≤r≤10181≤l≤r≤1018) — the range in which you need to find the sum.
Output
Print a single integer — the answer modulo 10000000071000000007 (109+7109+7).
Examples
input
Copy
1 3
output
Copy
7
input
Copy
5 14
output
Copy
105
input
Copy
88005553535 99999999999
output
Copy
761141116
Note
In the first example, the answer is the sum of the first three numbers written out (1+2+4=71+2+4=7).
In the second example, the numbers with numbers from 55 to 1414: 5,7,9,6,8,10,12,14,16,185,7,9,6,8,10,12,14,16,18. Their sum is 105105.
分析:直接跟着模拟走就可以了。。算两个等差数列求和,随意乱搞,注意一下取模就可以了。
#include "bits/stdc++.h"
using namespace std;
const int mod = 1e9 + 7;
long long qk(long long a, long long n) {
long long ans = 1;
while (n) {
if (n & 1)ans = ans * a % mod;
n >>= 1;
a = a * a % mod;
}
return ans;
}
long long getnum(long long n) {
long long a = 0, b = 0;
long long i, j;
long long sum = 0;
for (i = 1, j = 1; sum < n; i *= 2, j++) {
sum += i;
if (sum > n)break;
if (j & 1)a += i;
else b += i;
}
if (sum > n) {
sum -= i;
if (j & 1)a += n - sum;
else b += n - sum;
}
long long ans1 = (1LL + (a * 2LL - 1LL) % mod) * (a % mod) % mod * qk(2, mod - 2) % mod;
long long ans2 = (2LL + (b * 2LL) % mod) * (b % mod) % mod * qk(2, mod - 2) % mod;
return (ans1 + ans2) % mod;
}
int main() {
long long l, r;
cin >> l >> r;
long long ans1 = getnum(r), ans2 = getnum(l - 1);
printf("%lld\n", (ans1 - ans2 + mod) % mod);
/*long long x;
while (cin >> x)
printf("%lld\n", getnum(x));*/
}