Problem for Nazar

Problem for Nazar

Created by LXC on Thu Mar 28 16:51:52 2024

https://codeforces.com/problemset/problem/1151/C

ranting: 1800

tag: constructive algorithms, math

problem

设正奇数集合为$\mathrm{A}$,正偶数集合为$\mathrm{B}$,这两个集合是无限集。

在黑板上写了无数轮数,第$i$轮写下了$2^{(i-1)}$个数.

当$i$为奇数时,从集合$\mathrm{A}$中向后取数,当$i$为偶数时,从集合$\mathrm{B}$中向后取数。

求黑板上第$l$个数到第$r$个数的和,模$\mathrm{1000000007}$($10^9+7$)。

$1 \le l,r \le 10^{18}$

solution

倍增模拟,求出前缀和,再作差。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

#include <bits/stdc++.h>
#define SINGLE_INPUT
#define ll long long
#define ull unsigned long long
#define N 500005
// #define MOD 998244353
// const ll MOD = 1e9+7;
using namespace std;

template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p) {
return os<<'['<<p.first<<", "<<p.second<<']';
}
template<class t> ostream& operator<<(ostream& os,const vector<t>& v) {
os<<'['; int s = 1;
for(auto e:v) { if (s) s = 0; else os << ", "; os << e; }
return os<<']';
}
template<class t,class u> ostream& operator<<(ostream& os,const map<t,u>& mp){
os<<'{'; int s = 1;
for(auto [x,y]:mp) { if (s) s = 0; else os << ", "; os<<x<<": "<<y; }
return os<<'}';
}

#define MOD 1000000007
// #define MOD 998244353
#define mint Modint<MOD>
template <const int _MOD>
struct Modint {
int v;
Modint() { v = 0; }
Modint(long long o) { v = o % _MOD; }
int val() { return v; }
int pow(long long o) {
int ret = 1, tmp = v;
while (o) {
if (o & 1)
ret = ((long long)ret * tmp) % _MOD;
o >>= 1;
tmp = ((long long)tmp * tmp) % _MOD;
}
return ret;
}
void operator=(long long o) { v = o % _MOD; }
bool operator==(long long o) const { return v == o; }
bool operator==(Modint o) const { return v == o.v; }
bool operator!=(long long o) const { return v != o; }
bool operator!=(Modint o) const { return v != o.v; }
bool operator<(long long o) const { return v < o; }
bool operator<(Modint o) const { return v < o.v; }
bool operator>(long long o) const { return v > o; }
bool operator>(Modint o) const { return v > o.v; }
bool operator<=(long long o) const { return v <= o; }
bool operator<=(Modint o) const { return v <= o.v; }
bool operator>=(long long o) const { return v >= o; }
bool operator>=(Modint o) const { return v >= o.v; }

Modint operator+(long long o) const { return *this + Modint(o); }
Modint operator+(Modint o) const { return ((long long)v + o.v) % _MOD; }
Modint operator*(long long o) const { return *this * Modint(o); }
Modint operator*(Modint o) const { return (long long)v * o.v % _MOD; }

Modint operator-(long long o) const { return *this - Modint(o); }
Modint operator-(Modint o) const {
return ((long long)v - o.v + _MOD) % _MOD;
}

Modint operator/(long long o) const { return *this / Modint(o); }
Modint operator/(Modint o) const {
return ((long long)v * o.pow(_MOD - 2)) % _MOD;
}
void operator+=(long long o) { *this = *this + o; }
void operator+=(Modint o) { *this = *this + o; }
void operator*=(long long o) { *this = *this * o; }
void operator*=(Modint o) { *this = *this * o; }
void operator-=(long long o) { *this = *this - o; }
void operator-=(Modint o) { *this = *this - o; }
void operator/=(long long o) { *this = *this / o; }
void operator/=(Modint o) { *this = *this / o; }

Modint operator^(long long o) { return Modint(pow(o)); }
Modint operator^(Modint o) { return Modint(pow(o.v)); }

template <class T>
friend bool operator==(T o, Modint u) {
return u == o;
}
template <class T>
friend Modint operator+(T o, Modint u) {
return u + o;
}
template <class T>
friend Modint operator*(T o, Modint u) {
return u * o;
}
template <class T>
friend Modint operator-(T o, Modint u) {
return Modint(o) - u;
}
template <class T>
friend Modint operator/(T o, Modint u) {
return Modint(o) / u;
}

void operator++() { *this = *this + 1; }
void operator--() { *this = *this - 1; }
void operator++(int k) { *this = *this + 1; }
void operator--(int k) { *this = *this - 1; }

template <const int T>
friend std::istream& operator>>(std::istream& in, Modint<T>& modint) {
ll x;
in >> x;
modint = Modint<T>(x);
return in;
}

template <const int T>
friend std::ostream& operator<<(std::ostream& os, const Modint<T>& modint) {
os << modint.v;
return os;
}
};

// 计算 x, x+2, x+4, ..., x+2(c-1)
mint cpt(mint x, mint c) {
return c*(x+c-1);
}

mint ps(ll x) {
ll c = 1, i = 1, a = 1, b = 2;
mint ans = 0;
while (x>=c) {
if (i%2) {
ans += cpt(a, c);
a += 2*c;
} else {
ans += cpt(b, c);
b += 2*c;
}
x -= c;
c <<= 1;
i++;
}
if (i%2) ans += cpt(a, x);
else ans += cpt(b, x);
return ans;
}

void sol() {
ll a, b;
cin >> a >> b;
cout << ps(b) - ps(a-1) << '\n';
}

int main() {
cout << setprecision(15) << fixed;
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef SINGLE_INPUT
int t;
cin >> t;
while (t--) {
sol();
}
#else
sol();
#endif
return 0;
}