Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int getsum(int k, int num, int mx, int res) {
if (mx <= 0) return 0;
mx = min(mx, (1 << k));
long long ans = (1ll * mx * (mx + 1) / 2 + 1ll * mx * res) % mod;
return ans * num % mod;
}
int f(int x, int y, int mx, int res, int k) {
if (mx <= 0 || x <= 0 || y <= 0) return 0;
long long ans = 0;
if (x >= (1 << k) && y >= (1 << k)) {
ans += getsum(k, 1 << k, mx, res);
ans += getsum(k, x - (1 << k), mx - (1 << k), res + (1 << k));
ans += getsum(k, y - (1 << k), mx - (1 << k), res + (1 << k));
ans += f(x - (1 << k), y - (1 << k), mx, res, k - 1);
} else if (x >= (1 << k)) {
ans += getsum(k, y, mx, res);
ans += f(x - (1 << k), y, mx - (1 << k), res + (1 << k), k - 1);
} else if (y >= (1 << k)) {
ans += getsum(k, x, mx, res);
ans += f(x, y - (1 << k), mx - (1 << k), res + (1 << k), k - 1);
} else {
ans += f(x, y, mx, res, k - 1);
}
return ans % mod;
}
int main() {
int n, x1, x2, y1, y2, k;
scanf("%d", &n);
while (n--) {
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
long long ans = f(x2, y2, k, 0, 30);
ans -= f(x2, y1 - 1, k, 0, 30);
ans -= f(x1 - 1, y2, k, 0, 30);
ans += f(x1 - 1, y1 - 1, k, 0, 30);
printf("%I64d\n", (ans % mod + mod) % mod);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = (1e9) + 7;
pair<long long, long long> query(long long x, long long y, long long k,
long long lim) {
if (x <= 0 || y <= 0 || k < 0 || lim <= 0) return {0, 0};
long long pw = 1 << k;
long long cnt = 0, sum = 0;
if (x >= pw || y >= pw) {
long long val_cnt = min(pw, lim), node_cnt = min(pw, min(x, y));
cnt = val_cnt * node_cnt % MOD;
sum = (val_cnt * (val_cnt + 1) / 2 % MOD) * node_cnt % MOD;
} else {
pw >>= 1;
pair<long long, long long> res[2][2] = {
query(x, y, k - 1, lim), query(x - pw, y, k - 1, lim - pw),
query(x, y - pw, k - 1, lim - pw), query(x - pw, y - pw, k - 1, lim)};
cnt = (res[0][0].second + res[0][1].second + res[1][0].second +
res[1][1].second) %
MOD;
sum = (res[0][0].first + res[0][1].first + res[1][0].first +
res[1][1].first) %
MOD;
sum = (sum + (res[0][1].second + res[1][0].second) * pw) % MOD;
}
assert(sum >= 0 && cnt >= 0);
return {sum, cnt};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
long long x1, y1, x2, y2, lim;
cin >> x1 >> y1 >> x2 >> y2 >> lim;
long long k = 0;
while (1ll << k <= max(x2, y2)) ++k;
long long res =
query(x2, y2, k, lim).first - query(x1 - 1, y2, k, lim).first -
query(x2, y1 - 1, k, lim).first + query(x1 - 1, y1 - 1, k, lim).first;
res %= MOD;
cout << (res + MOD) % MOD << '\n';
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e+9 + 7ll;
const long long linf = (long long)1e+18 + 7ll;
const long double eps = (long double)1e-9;
const long double pi = acosl((long double)-1.0);
const int alph = 26;
static char buff[(int)2e6 + 17];
const int maxn = (int)3e5 + 17;
int q;
long long dp[35][2][2][2], sm[35][2][2][2];
void add(long long& x, long long y) { x += y, x %= inf; }
void sub(long long& x, long long y) {
x %= inf, y %= inf, x -= y, x += inf, x %= inf;
}
long long mul(long long x, long long y) {
return x %= inf, y %= inf, x *= y, x %= inf, x;
}
bool read() {
if (scanf("%d", &q) != 1) return false;
return true;
}
vector<int> calc(int x) {
vector<int> res;
for (int i = 0; i < 34; ++i, x /= 2) res.push_back(x % 2);
reverse(res.begin(), res.end());
return res;
}
long long get(int x, int y, int k) {
if (x < 0 || y < 0 || k < 0) return 0;
memset(dp, 0, sizeof(dp));
memset(sm, 0, sizeof(sm));
vector<int> X = calc(x), Y = calc(y), K = calc(k);
dp[0][1][1][1] = 1;
for (int i = 0; i < 34; ++i)
for (int a = 0; a <= 1; ++a)
for (int b = 0; b <= 1; ++b)
for (int c = 0; c <= 1; ++c)
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y) {
long long z = x ^ y;
if (a && x > X[i]) continue;
if (b && y > Y[i]) continue;
if (c && z > K[i]) continue;
add(dp[i + 1][a & (x == X[i])][b & (y == Y[i])][c & (z == K[i])],
dp[i][a][b][c]);
add(sm[i + 1][a & (x == X[i])][b & (y == Y[i])][c & (z == K[i])],
sm[i][a][b][c]);
add(sm[i + 1][a & (x == X[i])][b & (y == Y[i])][c & (z == K[i])],
mul((z << (33 - i)), dp[i][a][b][c]));
}
long long res = 0;
for (int a = 0; a <= 1; ++a)
for (int b = 0; b <= 1; ++b)
for (int c = 0; c <= 1; ++c) {
add(res, dp[34][a][b][c]);
add(res, sm[34][a][b][c]);
}
return res;
}
void solve() {
for (; q > 0; --q) {
int x1, y1, x2, y2, k;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
--x1, --y1, --x2, --y2, --k;
long long ans = 0;
add(ans, get(x2, y2, k));
add(ans, get(x1 - 1, y1 - 1, k));
sub(ans, get(x2, y1 - 1, k));
sub(ans, get(x1 - 1, y2, k));
printf("%lld\n", ans);
}
}
int main() {
while (read()) solve();
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long mem[33][2][2][2];
long long mem2[33][2][2][2];
vector<int> x, y, z;
long long go(int ind, int xs, int ys, int zs) {
if (ind >= 32) {
mem2[ind][xs][ys][zs] = 0;
return mem[ind][xs][ys][zs] = 1;
}
if (mem[ind][xs][ys][zs] != -1) return mem[ind][xs][ys][zs];
long long a = ((xs || x[ind]) && (ys || y[ind]))
? go(ind + 1, xs, ys, zs | (z[ind] > 0))
: 0;
long long b =
go(ind + 1, xs | (x[ind] > 0), ys | (y[ind] > 0), zs | (z[ind] > 0));
long long c = ((zs || z[ind] > 0) && (ys || y[ind] > 0))
? go(ind + 1, xs | (x[ind] > 0), ys, zs)
: 0;
long long d = ((zs || z[ind] > 0) && (xs || x[ind] > 0))
? go(ind + 1, xs, ys | (y[ind] > 0), zs)
: 0;
mem2[ind][xs][ys][zs] = (((1LL << (31 - ind)) % ((long long)(1e9 + 7))) *
((c + d) % ((long long)(1e9 + 7)))) %
((long long)(1e9 + 7));
mem2[ind][xs][ys][zs] += ((xs || x[ind]) && (ys || y[ind]))
? mem2[ind + 1][xs][ys][zs | (z[ind] > 0)]
: 0;
mem2[ind][xs][ys][zs] +=
mem2[ind + 1][xs | (x[ind] > 0)][ys | (y[ind] > 0)][zs | (z[ind] > 0)];
mem2[ind][xs][ys][zs] += ((zs || z[ind] > 0) && (ys || y[ind] > 0))
? mem2[ind + 1][xs | (x[ind] > 0)][ys][zs]
: 0;
mem2[ind][xs][ys][zs] += ((zs || z[ind] > 0) && (xs || x[ind] > 0))
? mem2[ind + 1][xs][ys | (y[ind] > 0)][zs]
: 0;
mem2[ind][xs][ys][zs] %= ((long long)(1e9 + 7));
return mem[ind][xs][ys][zs] = (a + b + c + d) % ((long long)(1e9 + 7));
}
long long get(int xa, int ya, int za) {
if (xa < 0 || ya < 0) return 0;
int i;
x.clear();
y.clear();
z.clear();
for (i = 0; i < 32; i++) {
x.push_back(xa % 2);
y.push_back(ya % 2);
z.push_back(za % 2);
xa /= 2;
ya /= 2;
za /= 2;
}
reverse(x.begin(), x.end());
reverse(y.begin(), y.end());
reverse(z.begin(), z.end());
memset(mem, -1, sizeof mem);
memset(mem2, 0, sizeof mem2);
go(0, 0, 0, 0);
return (mem2[0][0][0][0] + mem[0][0][0][0]) % ((long long)(1e9 + 7));
}
int main() {
int q;
cin >> q;
while (q--) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--;
y1--;
x2--;
y2--;
k--;
cout << (get(x2, y2, k) - get(x2, y1 - 1, k) - get(x1 - 1, y2, k) +
get(x1 - 1, y1 - 1, k) + 2 * ((long long)(1e9 + 7))) %
((long long)(1e9 + 7))
<< endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int DIV_TWO;
int x11, y11, x22, y22, k;
int sum(int a, int b) { return (a + b) % MOD; }
void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
}
int mul(int a, int b) { return a * 1LL * b % MOD; }
int bp(int x, int d) {
if (!d) return 1;
if (d & 1) return mul(x, bp(x, d - 1));
int r = bp(x, d / 2);
return mul(r, r);
}
int asum(int n, int cnt, int add) {
int f = sum(add, 1);
int l = sum(add, n);
int ret = mul(n, sum(f, l));
ret = mul(ret, DIV_TWO);
ret = mul(ret, cnt);
return ret;
}
int max(int a, int b, int c, int d) {
int max1 = max(a, b);
int max2 = max(c, d);
return max(max1, max2);
}
int rec(int s, int w, int h, int k, int addv = 0) {
if (k <= 0) return 0;
if (w <= 0 || h <= 0) return 0;
if (w == s) {
return asum(min(k, s), h, addv);
}
if (h == s) {
return asum(min(k, s), w, addv);
}
int ret = 0;
int ns = s / 2;
add(ret, rec(ns, min(ns, w), min(ns, h), k, addv));
add(ret, rec(ns, w - ns, min(ns, h), k - ns, addv + ns));
add(ret, rec(ns, min(ns, w), h - ns, k - ns, addv + ns));
add(ret, rec(ns, w - ns, h - ns, k, addv));
return ret;
}
int const N = 30;
int ans[N][N];
int get(int col, int row) {
vector<int> v;
v.push_back(0);
for (int i = 1; i < row; ++i) v.push_back(ans[i][col]);
for (int i = 1; i < col; ++i) v.push_back(ans[row][i]);
sort(v.begin(), v.end());
for (int i = 1; i < (int)(v.size()); ++i)
if (v[i] - v[i - 1] != 1) return v[i - 1] + 1;
return v.back() + 1;
}
void brute() {
for (int i = 1; i < N; ++i) {
for (int j = 1; j < N; ++j) {
int mini = get(j, i);
ans[i][j] = mini;
}
}
}
int getBrute(int s, int w, int h, int k) {
int ret = 0;
for (int y = 1; y < h + 1; ++y)
for (int x = 1; x < w + 1; ++x)
if (ans[y][x] <= k) ret += ans[y][x];
return ret;
}
int solve() {
cin >> x11 >> y11 >> x22 >> y22 >> k;
int ans = 0;
int maxi = max(x11, y11, x22, y22);
int s = 1;
while (s < maxi) {
s *= 2;
}
add(ans, rec(s, s, s, k));
add(ans, -rec(s, s, y11 - 1, k));
add(ans, -rec(s, x11 - 1, s, k));
add(ans, -rec(s, s - x22, s, k));
add(ans, -rec(s, s, s - y22, k));
add(ans, rec(s, x11 - 1, y11 - 1, k));
add(ans, rec(s, s, y11 - 1, k));
add(ans, -rec(s, x22, y11 - 1, k));
add(ans, rec(s, x11 - 1, s, k));
add(ans, -rec(s, x11 - 1, y22, k));
add(ans, rec(s, s, s, k));
add(ans, -rec(s, x22, s, k));
add(ans, -rec(s, s, y22, k));
add(ans, rec(s, x22, y22, k));
return ans;
}
int main() {
DIV_TWO = bp(2, MOD - 2);
int test;
cin >> test;
while (test--) {
cout << solve() << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
char ch = 0;
int x = 0, flag = 1;
while (!isdigit(ch)) {
ch = getchar();
if (ch == '-') flag = -1;
}
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * flag;
}
const int mo = 1e9 + 7;
void add(int &x, int k) { x = (x + k) % mo; }
int f[1100], g[1100], h[1100], dp[2][2][2], DP[2][2][2], dp_[2][2][2],
DP_[2][2][2];
int solve() {
memset(dp, 0, sizeof(dp));
memset(DP, 0, sizeof(DP));
memset(dp_, 0, sizeof(dp_));
memset(DP_, 0, sizeof(DP_));
dp[0][0][0] = 1;
DP[0][0][0] = 0;
for (int i = 1; i <= 31; i++) {
int w = (1 << (30 - i + 1)) % mo;
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
if (dp[a][b][c] || DP[a][b][c])
for (int x = 0; x <= (a ? 1 : f[i]); x++)
for (int y = 0; y <= (b ? 1 : g[i]); y++)
if ((x ^ y) <= (c ? 1 : h[i])) {
int o = dp[a][b][c], t = DP[a][b][c];
add(dp_[a | (x < f[i])][b | (y < g[i])][c | ((x ^ y) < h[i])],
o);
add(DP_[a | (x < f[i])][b | (y < g[i])][c | ((x ^ y) < h[i])],
((1ll * (x ^ y) * o * w % mo) + t) % mo);
}
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++) {
dp[a][b][c] = dp_[a][b][c];
dp_[a][b][c] = 0;
DP[a][b][c] = DP_[a][b][c];
DP_[a][b][c] = 0;
}
}
int ans = 0;
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
ans = (ans + ((dp[a][b][c] + DP[a][b][c]) % mo)) % mo;
return (ans % mo + mo) % mo;
}
void work() {
int a = read() - 1, b = read() - 1, c = read() - 1, d = read() - 1,
k = read() - 1, ans = 0;
for (int i = 30; i >= 0; i--) h[30 - i + 1] = ((1 << i) & (k)) ? 1 : 0;
if (c >= 0 && d >= 0) {
for (int i = 30; i >= 0; i--) f[30 - i + 1] = ((1 << i) & (c)) ? 1 : 0;
for (int i = 30; i >= 0; i--) g[30 - i + 1] = ((1 << i) & (d)) ? 1 : 0;
ans = (ans + solve()) % mo;
}
if (a - 1 >= 0 && d >= 0) {
for (int i = 30; i >= 0; i--) f[30 - i + 1] = ((1 << i) & (a - 1)) ? 1 : 0;
for (int i = 30; i >= 0; i--) g[30 - i + 1] = ((1 << i) & (d)) ? 1 : 0;
ans = (ans - solve()) % mo;
}
if (c >= 0 && b - 1 >= 0) {
for (int i = 30; i >= 0; i--) f[30 - i + 1] = ((1 << i) & (c)) ? 1 : 0;
for (int i = 30; i >= 0; i--) g[30 - i + 1] = ((1 << i) & (b - 1)) ? 1 : 0;
ans = (ans - solve()) % mo;
}
if (a - 1 >= 0 && b - 1 >= 0) {
for (int i = 30; i >= 0; i--) f[30 - i + 1] = ((1 << i) & (a - 1)) ? 1 : 0;
for (int i = 30; i >= 0; i--) g[30 - i + 1] = ((1 << i) & (b - 1)) ? 1 : 0;
ans = (ans + solve()) % mo;
}
printf("%d\n", (ans % mo + mo) % mo);
}
int main() {
int t = read();
for (int i = 1; i <= t; i++) work();
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline bool umin(T1& x, const T2& y) {
return x > y ? x = y, true : false;
}
template <typename T1, typename T2>
inline bool umax(T1& x, const T2& y) {
return x < y ? x = y, true : false;
}
const int N = (int)1e6 + 7;
const int mod = (int)1e9 + 7;
const int INF = (int)1e9 + 17;
const long long LLINF = (long long)1e18 + 17;
inline void add(int& x, int y) { x = (0ll + x + y) % mod; }
inline int kbit(int n, int k) { return (n >> k) & 1; }
const int BITS = 31;
int dp[33][2][2][2];
int cnt[33][2][2][2];
int solve(int n, int m, int k) {
if (n < 0 || m < 0 || k < 0) return 0;
memset(dp, 0, sizeof dp);
memset(cnt, 0, sizeof cnt);
dp[BITS][1][1][1] = 0;
cnt[BITS][1][1][1] = 1;
for (int i = (BITS + 1) - 1; i >= (1); --i) {
for (int flag1 = (0); flag1 < (2); ++flag1)
for (int flag2 = (0); flag2 < (2); ++flag2)
for (int flag3 = (0); flag3 < (2); ++flag3) {
if (cnt[i][flag1][flag2][flag3] == 0) continue;
for (int bit1 = (0); bit1 < (2); ++bit1)
for (int bit2 = (0); bit2 < (2); ++bit2) {
int new_flag1 = flag1;
int new_flag2 = flag2;
int new_flag3 = flag3;
if (flag1) {
bool bit = kbit(n, i - 1);
if (bit < bit1) continue;
if (bit > bit1) new_flag1 = false;
}
if (flag2) {
bool bit = kbit(m, i - 1);
if (bit < bit2) continue;
if (bit > bit2) new_flag2 = false;
}
if (flag3) {
bool bit = kbit(k, i - 1);
if (bit < (bit1 ^ bit2)) continue;
if (bit > (bit1 ^ bit2)) new_flag3 = false;
}
add(dp[i - 1][new_flag1][new_flag2][new_flag3],
dp[i][flag1][flag2][flag3]);
add(dp[i - 1][new_flag1][new_flag2][new_flag3],
cnt[i][flag1][flag2][flag3] * (bit1 ^ bit2) *
(1ll << (i - 1)) % mod);
add(cnt[i - 1][new_flag1][new_flag2][new_flag3],
cnt[i][flag1][flag2][flag3]);
}
}
}
int ret = 0;
for (int flag1 = (0); flag1 < (2); ++flag1)
for (int flag2 = (0); flag2 < (2); ++flag2)
for (int flag3 = (0); flag3 < (2); ++flag3) {
add(ret, dp[0][flag1][flag2][flag3]);
add(ret, cnt[0][flag1][flag2][flag3]);
}
return ret;
}
int solve(int x, int y, int x2, int y2, int k) {
--x, --y, --x2, --y2;
long long ans = solve(x2, y2, k - 1);
ans = (ans - solve(x - 1, y2, k - 1) + mod) % mod;
ans = (ans - solve(x2, y - 1, k - 1) + mod) % mod;
ans = (ans + solve(x - 1, y - 1, k - 1)) % mod;
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int q;
cin >> q;
while (q-- > 0) {
int x, y, x2, y2, k;
cin >> x >> y >> x2 >> y2 >> k;
cout << solve(x, y, x2, y2, k) << '\n';
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
static const int INF = 0x3f3f3f3f;
static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
template <typename T, typename U>
static void amin(T &x, U y) {
if (y < x) x = y;
}
template <typename T, typename U>
static void amax(T &x, U y) {
if (x < y) x = y;
}
template <int MOD>
struct ModInt {
static const int Mod = MOD;
unsigned x;
ModInt() : x(0) {}
ModInt(signed sig) {
int sigt = sig % MOD;
if (sigt < 0) sigt += MOD;
x = sigt;
}
ModInt(signed long long sig) {
int sigt = sig % MOD;
if (sigt < 0) sigt += MOD;
x = sigt;
}
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) {
if ((x += that.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator-=(ModInt that) {
if ((x += MOD - that.x) >= MOD) x -= MOD;
return *this;
}
ModInt &operator*=(ModInt that) {
x = (unsigned long long)x * that.x % MOD;
return *this;
}
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
};
pair<ModInt<1000000007>, ModInt<1000000007> > memo[33][2][2][2];
int X, Y;
int K;
pair<ModInt<1000000007>, ModInt<1000000007> > rec(int i, bool xlt, bool ylt,
bool klt) {
pair<ModInt<1000000007>, ModInt<1000000007> > &r = memo[i + 1][xlt][ylt][klt];
if (r.first.x != -1) return r;
if (i == -1) return r = make_pair(xlt && ylt && klt ? 1 : 0, 0);
int rx = X >> i & 1;
int ry = Y >> i & 1;
int rk = K >> i & 1;
r = {0, 0};
for (int(x) = 0; (x) < (int)(2); ++(x))
for (int(y) = 0; (y) < (int)(2); ++(y)) {
int k = x ^ y;
if (!xlt && rx < x) continue;
if (!ylt && ry < y) continue;
if (!klt && rk < k) continue;
auto p = rec(i - 1, xlt || x < rx, ylt || y < ry, klt || k < rk);
r.first += p.first;
r.second += p.second + p.first * ((long long)k << i);
}
return r;
}
int main() {
int Q;
while (~scanf("%d", &Q)) {
for (int(ii) = 0; (ii) < (int)(Q); ++(ii)) {
int xL;
int yL;
int xR;
int yR;
scanf("%d%d%d%d", &xL, &yL, &xR, &yR);
--xL, --yL;
scanf("%d", &K);
ModInt<1000000007> ans;
for (int(x) = 0; (x) < (int)(2); ++(x))
for (int(y) = 0; (y) < (int)(2); ++(y)) {
memset(memo, -1, sizeof(memo));
X = x == 0 ? xR : xL;
Y = y == 0 ? yR : yL;
auto p = rec(31, false, false, false);
ans += (p.second + p.first) * ((x + y) % 2 == 1 ? -1 : 1);
}
printf("%d\n", ans.get());
}
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
inline void add(long long &a, long long b) {
a += b;
if (a >= mod) a -= mod;
}
inline void sub(long long &a, long long b) {
a -= b;
if (a < 0) a += mod;
}
inline long long mul(long long a, long long b) { return (a * b) % mod; }
inline int dig(int a, int i) { return 1 & (a >> (31 - i)); }
long long solve(int x, int y, int k) {
long long ct[32][2][2][2], sum[32][2][2][2];
memset(ct, 0, sizeof ct);
memset(sum, 0, sizeof sum);
if (x < 0 || y < 0 || k < 0) return 0;
ct[0][1][1][1] = 1;
for (int i = 0; i < 31; i++) {
for (int xx = 0; xx < 2; xx++)
for (int yy = 0; yy < 2; yy++) {
int kk = xx ^ yy;
for (int fx = 0; fx < 2; fx++)
for (int fy = 0; fy < 2; fy++)
for (int fk = 0; fk < 2; fk++) {
if (fx && xx > dig(x, i + 1)) continue;
if (fy && yy > dig(y, i + 1)) continue;
if (fk && kk > dig(k, i + 1)) continue;
add(ct[i + 1][fx & (xx == dig(x, i + 1))]
[fy & (yy == dig(y, i + 1))][fk & (kk == dig(k, i + 1))],
ct[i][fx][fy][fk]);
add(sum[i + 1][fx & (xx == dig(x, i + 1))]
[fy & (yy == dig(y, i + 1))][fk & (kk == dig(k, i + 1))],
sum[i][fx][fy][fk]);
add(sum[i + 1][fx & (xx == dig(x, i + 1))]
[fy & (yy == dig(y, i + 1))][fk & (kk == dig(k, i + 1))],
mul(kk << (30 - i), ct[i][fx][fy][fk]));
}
}
}
long long ans = 0;
for (int xx = 0; xx < 2; xx++)
for (int yy = 0; yy < 2; yy++)
for (int kk = 0; kk < 2; kk++) {
add(ans, ct[31][xx][yy][kk]);
add(ans, sum[31][xx][yy][kk]);
}
return ans;
}
int main() {
int q;
cin >> q;
while (q--) {
int x, y, x2, y2, k;
cin >> x >> y >> x2 >> y2 >> k;
x--;
y--;
x2--;
y2--;
k--;
long long res = 0;
add(res, solve(x2, y2, k));
sub(res, solve(x2, y - 1, k));
sub(res, solve(x - 1, y2, k));
add(res, solve(x - 1, y - 1, k));
cout << res << '\n';
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long fact[1000005];
long long power(long long a, long long b) {
if (b == 0) return 1;
long long f = power(a, b / 2);
f = (f * f) % 1000000007;
if (b % 2) return (a * f) % 1000000007;
return f;
}
long long comb(long long n, long long r) {
if (n == r || r == 0) return 1;
long long ans =
(power(fact[r], 1000000007 - 2) * power(fact[n - r], 1000000007 - 2)) %
1000000007;
return (ans * fact[n]) % 1000000007;
}
pair<long long, long long> rec2(long long a, long long k) {
long long ans = 0;
k = min(a, k);
ans = (k * (k + 1)) % 1000000007;
ans = (a * ans) % 1000000007;
if (ans % 2 == 1) {
ans = (ans + 1000000007) / 2;
ans = ans % 1000000007;
} else {
ans = ans / 2;
}
return make_pair(ans % 1000000007, (a * k) % 1000000007);
}
pair<long long, long long> rec4(long long a, long long b, long long k) {
k = min(a, k);
long long ans = (k * (k + 1)) % 1000000007;
ans = (b * ans) % 1000000007;
if (ans % 2 == 1) {
ans = (ans + 1000000007) / 2;
ans = ans % 1000000007;
} else {
ans = ans / 2;
}
return make_pair(ans, (b * k) % 1000000007);
}
pair<long long, long long> rec3(long long a, long long b, long long k) {
if (k <= 0 || a <= 0 || b <= 0) return make_pair(0, 0);
if (b == 1) {
k = min(a, k);
return make_pair(((k * (k + 1)) / 2) % 1000000007, k);
}
long long s = 1;
while (s <= b) {
s *= 2;
}
s /= 2;
pair<long long, long long> ans = rec4(a, s, k);
pair<long long, long long> ans2 = rec3(a, b - s, k);
return make_pair((ans2.first + ans.first) % 1000000007,
(ans2.second + ans.second) % 1000000007);
}
pair<long long, long long> rec(long long a, long long b, long long k) {
if (k <= 0 || a <= 0 || b <= 0) return make_pair(0, 0);
long long s = 2;
long long coun = 0;
if (a == 1 || b == 1) {
long long f = max(a, b);
f = min(f, k);
return make_pair(((f * (f + 1)) / 2) % 1000000007, f);
}
while (s < a || s < b) {
s *= 2;
++coun;
}
if (a > (s / 2) && b > (s / 2)) {
pair<long long, long long> f = rec3((s / 2), a - (s / 2), k - (s / 2));
f.first += ((s / 2) * f.second) % 1000000007;
f.first %= 1000000007;
pair<long long, long long> S = rec3(s / 2, b - (s / 2), k - (s / 2));
pair<long long, long long> as = rec(a - (s / 2), b - (s / 2), k);
S.first += ((s / 2) * S.second) % 1000000007;
S.first %= 1000000007;
return make_pair(
(rec2(s / 2, k).first + f.first + S.first + as.first) % 1000000007,
(f.second + S.second + as.second + rec2(s / 2, k).second) % 1000000007);
} else if (b > (s / 2)) {
pair<long long, long long> f = rec3((s / 2), a, k);
pair<long long, long long> S = rec(a, b - (s / 2), k - (s / 2));
S.first += ((s / 2) * S.second) % 1000000007;
S.first %= 1000000007;
return make_pair((f.first + S.first) % 1000000007,
(f.second + S.second) % 1000000007);
} else {
pair<long long, long long> f = rec3(s / 2, b, k);
pair<long long, long long> S = rec(a - (s / 2), b, k - (s / 2));
S.first += ((s / 2) * S.second) % 1000000007;
S.first %= 1000000007;
return make_pair((f.first + S.first) % 1000000007,
(f.second + S.second) % 1000000007);
}
}
int main() {
int q;
cin >> q;
while (q--) {
int a, b, c, d, k;
scanf("%d %d %d %d %d", &a, &b, &c, &d, &k);
long long ans = (rec(c, d, k).first - rec(a - 1, d, k).first -
rec(c, b - 1, k).first + rec(a - 1, b - 1, k).first) %
1000000007;
ans = (ans + 1000000007) % 1000000007;
printf("%I64d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
inline long long gi() {
long long x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
return x * f;
}
int f[32][2][2][2], g[32][2][2][2];
inline int solve(int n, int m, int K) {
if (n < 0 || m < 0) return 0;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
f[31][0][0][0] = 1;
for (int p = 30; ~p; --p)
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
for (int k = 0; k < 2; ++k) {
if (!f[p + 1][i][j][k]) continue;
for (int a = 0; a < 2; ++a)
for (int b = 0; b < 2; ++b) {
if (a && !i && !(n & (1 << p))) continue;
if (b && !j && !(m & (1 << p))) continue;
if ((a ^ b) && !k && !(K & (1 << p))) continue;
int _i = i, _j = j, _k = k;
if (!a && (n & (1 << p))) _i = 1;
if (!b && (m & (1 << p))) _j = 1;
if (!(a ^ b) && (K & (1 << p))) _k = 1;
f[p][_i][_j][_k] =
(f[p][_i][_j][_k] + f[p + 1][i][j][k]) % 1000000007;
g[p][_i][_j][_k] =
(g[p][_i][_j][_k] + g[p + 1][i][j][k]) % 1000000007;
if (a ^ b)
g[p][_i][_j][_k] =
(g[p][_i][_j][k] + 1ll * (1 << p) * f[p + 1][i][j][k]) %
1000000007;
}
}
int ret = 0;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
for (int k = 0; k < 2; ++k)
ret = (ret + (g[0][i][j][k] + f[0][i][j][k]) % 1000000007) % 1000000007;
return ret;
}
int main() {
int QAQ = gi();
while (QAQ--) {
int x1 = gi() - 1, y1 = gi() - 1, x2 = gi() - 1, y2 = gi() - 1,
k = gi() - 1;
printf("%d\n", (0ll + solve(x2, y2, k) - solve(x2, y1 - 1, k) -
solve(x1 - 1, y2, k) + solve(x1 - 1, y1 - 1, k) +
1000000007 + 1000000007) %
1000000007);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
int addMod(int a, int b, int m = MOD) {
a += b;
if (m <= a) {
a -= m;
}
return a;
}
int mulMod(int a, int b, int m = MOD) { return a * 1ll * b % m; }
int dp[32][2][2][2], dp2[32][2][2][2];
bool bit(int x, int p) { return x & (1 << p); }
int calc(int n, int m, int k) {
memset(dp, 0, sizeof dp);
memset(dp2, 0, sizeof dp2);
dp[31][0][0][0] = 1;
for (int i = 30; i >= 0; --i) {
for (int kl = 0; kl < 2; ++kl) {
for (int nl = 0; nl < 2; ++nl) {
for (int ml = 0; ml < 2; ++ml) {
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
if (nl == 0 && x > bit(n, i)) {
continue;
}
if (ml == 0 && y > bit(m, i)) {
continue;
}
int z = (x ^ y);
if (kl == 0 && z > bit(k, i)) {
continue;
}
int nnl = (nl | (x < bit(n, i)));
int nml = (ml | (y < bit(m, i)));
int nkl = (kl | (z < bit(k, i)));
dp[i][nkl][nnl][nml] =
addMod(dp[i][nkl][nnl][nml], dp[i + 1][kl][nl][ml]);
dp2[i][nkl][nnl][nml] =
addMod(dp2[i][nkl][nnl][nml], dp2[i + 1][kl][nl][ml]);
dp2[i][nkl][nnl][nml] = addMod(
dp2[i][nkl][nnl][nml], mulMod(z << i, dp[i + 1][kl][nl][ml]));
}
}
}
}
}
}
int ret = 0;
for (int i = 0; i < 8; ++i) {
ret = addMod(ret, dp[0][bit(i, 0)][bit(i, 1)][bit(i, 2)]);
ret = addMod(ret, dp2[0][bit(i, 0)][bit(i, 1)][bit(i, 2)]);
}
return ret;
}
void solve() {
int x1, y1, x2, y2, k;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
--x1;
--y1;
--x2;
--y2;
--k;
int ans = calc(x2, y2, k);
if (x1 > 0) {
ans = addMod(ans, MOD - calc(x1 - 1, y2, k));
}
if (y1 > 0) {
ans = addMod(ans, MOD - calc(x2, y1 - 1, k));
}
if (x1 > 0 && y1 > 0) {
ans = addMod(ans, calc(x1 - 1, y1 - 1, k));
}
printf("%d\n", ans);
}
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
solve();
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
string print_iterable(T1 begin_iter, T2 end_iter, int counter) {
bool done_something = false;
stringstream res;
res << "[";
for (; begin_iter != end_iter and counter; ++begin_iter) {
done_something = true;
counter--;
res << *begin_iter << ", ";
}
string str = res.str();
if (done_something) {
str.pop_back();
str.pop_back();
}
str += "]";
return str;
}
vector<int> SortIndex(int size, std::function<bool(int, int)> compare) {
vector<int> ord(size);
for (int i = 0; i < size; i++) ord[i] = i;
sort(ord.begin(), ord.end(), compare);
return ord;
}
template <typename T>
bool MinPlace(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool MaxPlace(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename S, typename T>
ostream& operator<<(ostream& out, const pair<S, T>& p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
out << "[";
for (int i = 0; i < (int)v.size(); i++) {
out << v[i];
if (i != (int)v.size() - 1) out << ", ";
}
out << "]";
return out;
}
const long long mod = 1e9 + 7;
long long p[100];
long long TrivialComputeNum(long long k, int i, int j) {
long long res = 0;
for (int a = 0; a < (1 << i); a++) {
for (int b = 0; b < (1 << j); b++) {
if ((a ^ b) <= k) res++;
}
}
return res;
}
long long TrivialComputeSum(long long k, int i, int j) {
long long res = 0;
for (int a = 0; a < (1 << i); a++) {
for (int b = 0; b < (1 << j); b++) {
if ((a ^ b) <= k) res += a ^ b;
}
}
return res;
}
long long TrivialSimpleSum(long long k, int x, int y) {
long long res = 0;
for (int a = 0; a < x; a++) {
for (int b = 0; b < y; b++) {
if ((a ^ b) <= k) res += 1 + (a ^ b);
}
}
return res;
}
long long ComputeNum(long long k, int i, int j) {
if (i < j) swap(i, j);
if (1 << i <= k) return p[i + j];
long long res = p[j] % mod;
for (int s = 0; 1 << s <= k; s++) {
if (k & 1 << s) {
res += p[s + j];
res %= mod;
}
}
return res;
}
long long ComputeSum(long long k, int i, int j) {
if (i < j) swap(i, j);
if (1 << i <= k) {
long long res1 = p[j];
long long res2 = (p[i] * (p[i] - 1ll)) / 2ll;
res1 %= mod;
res2 %= mod;
return (res1 * res2) % mod;
}
long long res = (p[j] * k) % mod;
for (int s = 0; (1 << s) <= k; s++) {
if (k & (1 << s)) {
long long ris1 = k - (k % p[s + 1]);
long long ris2 = (p[s] * (p[s] - 1ll)) / 2ll;
ris1 %= mod;
ris2 %= mod;
ris1 *= p[j + s];
ris2 *= p[j];
res += (ris1 + ris2) % mod;
res %= mod;
}
}
return res;
}
long long SimpleSum(long long k, int x, int y) {
long long ans = 0;
if (x <= 0 or y <= 0) return 0;
for (int i = 0; 1 << i <= x; i++) {
for (int j = 0; 1 << j <= y; j++) {
if (x & (1 << i) && y & (1 << j)) {
int x1 = x - x % (1 << (i + 1));
int y1 = y - y % (1 << (j + 1));
x1 = x1 - x1 % (1 << max(i, j));
y1 = y1 - y1 % (1 << max(i, j));
int v = x1 ^ y1;
if (k < v) continue;
ans += (long long)(1 + v) * ComputeNum(k - v, i, j) +
ComputeSum(k - v, i, j);
ans %= mod;
}
}
}
return ans;
}
long long GetSum(int x1, int y1, int x2, int y2, long long k) {
long long res = SimpleSum(k - 1, x2, y2) - SimpleSum(k - 1, x2, y1) -
SimpleSum(k - 1, x1, y2) + SimpleSum(k - 1, x1, y1);
res %= mod;
res += mod;
return res % mod;
}
int main() {
p[0] = 1;
for (int i = 1; i < 100; i++) p[i] = (p[i - 1] * 2ll) % mod;
ios::sync_with_stdio(false);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int x1, y1, x2, y2;
long long k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
cout << GetSum(x1 - 1, y1 - 1, x2, y2, k) << "\n";
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.StringTokenizer;
public class _809C {
/**
* Immutable class
*/
static class Key {
final int len;
final int height;
final int cap;
final int hash;
public Key(int len, int height, int cap) {
this.len = len;
this.height = height;
this.cap = cap;
int h = len;
h = h * 31 + height;
h = h * 31 + cap;
this.hash = h;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (len != key.len) return false;
if (height != key.height) return false;
return cap == key.cap;
}
@Override
public int hashCode() {
return hash;
}
@Override
public String toString() {
return "Key{" + "len=" + len + ", height=" + height + ", cap=" + cap + '}';
}
}
static class Value {
long cnt;
long sum;
@Override
public String toString() {
return "Value{" + "cnt=" + cnt + ", sum=" + sum + '}';
}
}
static final int MOD = 1000000007;
static final int INV_2 = 500000004;
static Map<Key, Value> f = new HashMap<>();
public static void main(String[] args) throws Exception {
Reader.init(System.in);
BufferedWriter cout = new BufferedWriter(new OutputStreamWriter(System.out));
final int MAX_LEN = 1 << 30;
StringJoiner joiner = new StringJoiner("\n");
for (int Q = Reader.nextInt(); Q > 0; Q--) {
int x1 = Reader.nextInt();
int y1 = Reader.nextInt();
int x2 = Reader.nextInt();
int y2 = Reader.nextInt();
int cap = Reader.nextInt();
long ans = g(MAX_LEN, x2, y2, cap).sum;
ans -= g(MAX_LEN, x1 - 1, y2, cap).sum;
ans -= g(MAX_LEN, x2, y1 - 1, cap).sum;
ans += g(MAX_LEN, x1 - 1, y1 - 1, cap).sum;
ans += 2L * MOD;
joiner.add("" + (ans % MOD));
f.clear();
}
System.out.println(joiner.toString());
cout.close();
}
static Value f(int len, int height, int cap) {
cap = Math.max(cap, 0);
cap = Math.min(cap, len);
height = Math.max(height, 0);
height = Math.min(height, len);
Key key = new Key(len, height, cap);
if (f.containsKey(key)) return f.get(key);
Value value = new Value();
if (cap < 1 || height < 1)
value.cnt = value.sum = 0;
else if (cap == len) {
value.cnt = (long) len * height % MOD;
value.sum = (1L + len) * len % MOD * INV_2 % MOD * height % MOD;
} else {
int halfLen = len / 2;
add(value, f(halfLen, height, cap), 0);
add(value, f(halfLen, height, cap - halfLen), halfLen);
add(value, f(halfLen, height - halfLen, cap - halfLen), halfLen);
add(value, f(halfLen, height - halfLen, cap), 0);
}
f.put(key, value);
return value;
}
static void add(Value acc, Value v, int extra) {
acc.cnt = (acc.cnt + v.cnt) % MOD;
acc.sum = (acc.sum + v.sum + v.cnt * extra) % MOD;
}
static Value g(int len, int height, int width, int cap) {
height = Math.min(height, len);
width = Math.min(width, len);
if (height > width)
return g(len, width, height, cap);
Value value = new Value();
if (cap < 1 || height < 1)
value.cnt = value.sum = 0;
else if (len == 1)
value.cnt = value.sum = 1;
else {
int halfLen = len / 2;
if (width <= halfLen)
add(value, g(halfLen, height, width, cap), 0);
else if (height <= halfLen) {
add(value, f(halfLen, height, cap), 0);
add(value, g(halfLen, height, width - halfLen, cap - halfLen), halfLen);
} else {
add(value, f(halfLen, halfLen, cap), 0);
add(value, f(halfLen, height - halfLen, cap - halfLen), halfLen);
add(value, f(halfLen, width - halfLen, cap - halfLen), halfLen);
add(value, g(halfLen, height - halfLen, width - halfLen, cap), 0);
}
}
return value;
}
/**
* Class for buffered reading int and double values
*/
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
} | JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T& num) {
bool start = false, neg = false;
char c;
num = 0;
while ((c = getchar()) != EOF) {
if (c == '-')
start = neg = true;
else if (c >= '0' && c <= '9') {
start = true;
num = num * 10 + c - '0';
} else if (start)
break;
}
if (neg) num = -num;
}
const int mo = (int)(1e9) + 7;
int N, M, K;
inline void add(int& x, int v) {
x += v;
if (x >= mo) x -= mo;
}
int vi[32][2][2][2];
int su[32][2][2][2];
int cnt[32][2][2][2];
void G(int c, int lN, int lM, int lK) {
if (vi[c][lN][lM][lK]) return;
vi[c][lN][lM][lK] = 1;
cnt[c][lN][lM][lK] = su[c][lN][lM][lK] = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++) {
int tari = (N >> c) & 1, tarj = (M >> c) & 1, tark = (K >> c) & 1;
int nlN = lN, nlM = lM, nlK = lK;
if (i > tari && nlN) continue;
if (j > tarj && nlM) continue;
if ((i ^ j) > tark && nlK) continue;
if (i < tari) nlN = 0;
if (j < tarj) nlM = 0;
if ((i ^ j) < tark) nlK = 0;
int C = 1, S = 0;
if (c) {
G(c - 1, nlN, nlM, nlK);
C = cnt[c - 1][nlN][nlM][nlK];
S = su[c - 1][nlN][nlM][nlK];
}
add(cnt[c][lN][lM][lK], C);
add(su[c][lN][lM][lK], (S + 1LL * (i ^ j) * (1 << c) * C) % mo);
}
}
inline int calc(int n, int m) {
if (!n || !m) return 0;
N = n;
M = m;
N--;
M--;
memset(vi, 0, sizeof(vi));
G(31, 1, 1, 1);
int res = 0;
add(res, su[31][1][1][1]);
add(res, cnt[31][1][1][1]);
return res;
}
void solve() {
int lX, lY, rX, rY;
read(lX);
read(lY);
read(rX);
read(rY);
read(K);
K--;
int res = 0;
add(res, calc(rX, rY));
add(res, mo - calc(rX, lY - 1));
add(res, mo - calc(lX - 1, rY));
add(res, calc(lX - 1, lY - 1));
printf("%d\n", res);
}
int main() {
int T;
read(T);
while (T--) solve();
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
long long k;
long long solve(int x1, int y1, int x2, int y2, int L, long long f) {
if (k < f) return 0;
if (x1 == x2 && x1 == 1) {
long long l = f + y1 - 1, r = min(f + y2 - 1, k);
if (l <= r) return (l + r) * (r - l + 1) / 2 % mod;
}
if (y1 == y2 && y1 == 1) {
long long l = f + x1 - 1, r = min(f + x2 - 1, k);
if (l <= r) return (l + r) * (r - l + 1) / 2 % mod;
}
if (x1 == 1 && x2 == L) {
long long l = f, r = min(k, f + L - 1);
if (l <= r) return (l + r) * (r - l + 1) / 2 % mod * (y2 - y1 + 1) % mod;
}
if (y1 == 1 && y2 == L) {
long long l = f, r = min(k, f + L - 1);
if (l <= r) return (l + r) * (r - l + 1) / 2 % mod * (x2 - x1 + 1) % mod;
}
int d = L / 2, lx = 1, rx = d, ly = 1, ry = d;
long long ret = 0;
lx = max(1, x1), rx = min(d, x2);
ly = max(1, y1), ry = min(d, y2);
if (lx <= rx && ly <= ry) (ret += solve(lx, ly, rx, ry, d, f)) %= mod;
;
lx = max(d + 1, x1), rx = min(L, x2);
ly = max(1, y1), ry = min(d, y2);
if (lx <= rx && ly <= ry)
(ret += solve(lx - d, ly, rx - d, ry, d, f + d)) %= mod;
;
lx = max(1, x1), rx = min(d, x2);
ly = max(d + 1, y1), ry = min(L, y2);
if (lx <= rx && ly <= ry)
(ret += solve(lx, ly - d, rx, ry - d, d, f + d)) %= mod;
;
lx = max(d + 1, x1), rx = min(L, x2);
ly = max(d + 1, y1), ry = min(L, y2);
if (lx <= rx && ly <= ry)
(ret += solve(lx - d, ly - d, rx - d, ry - d, d, f)) %= mod;
;
return ret;
}
int main() {
int n;
scanf("%d", &n);
while (n--) {
int x1, x2, y1, y2, t = 1;
scanf("%d%d%d%d%I64d", &x1, &y1, &x2, &y2, &k);
while (t < x2 || t < y2) t <<= 1;
printf("%I64d\n", solve(x1, y1, x2, y2, t, 1ll));
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
struct par {
long long cuantos, suma;
} D[35][5][5][5], m;
long long mr, mc, k;
long long R[35], C[35], A[35];
bool Y[35][5][5][5];
long long P[35], modd = 1000000007;
par dp(long long idx, bool yr, bool yc, bool ya) {
if (Y[idx][yr][yc][ya]) return D[idx][yr][yc][ya];
if (idx == 32) {
m.suma = 0;
m.cuantos = 1;
return m;
}
Y[idx][yr][yc][ya] = true;
par ans;
ans.cuantos = ans.suma = 0;
for (int a = 0; a <= 1; a++) {
for (int b = 0; b <= 1; b++) {
if (a == 1 and !yr and R[idx] == 0) {
continue;
}
if (b == 1 and !yc and C[idx] == 0) {
continue;
}
if (a != b and !ya and A[idx] == 0) {
continue;
}
bool newr = yr or (R[idx] == 1 and a == 0);
bool newc = yc or (C[idx] == 1 and b == 0);
bool newa = ya or (A[idx] == 1 and a == b);
ans.cuantos =
(ans.cuantos + dp(idx + 1, newr, newc, newa).cuantos) % modd;
ans.suma = ans.suma + dp(idx + 1, newr, newc, newa).suma;
ans.suma = ans.suma % modd;
if (a != b)
ans.suma = ans.suma + P[idx] * dp(idx + 1, newr, newc, newa).cuantos;
ans.suma = ans.suma % modd;
}
}
D[idx][yr][yc][ya] = ans;
return ans;
}
void ponlo(long long *O, long long que) {
for (long long i = 0; i < 32; i++) {
O[i] = (que & (1 << (31 - i))) > 0;
}
}
long long saca(long long r, long long c, long long k) {
if (r < 0 or c < 0 or k < 0) return 0;
memset(Y, 0, sizeof(Y));
ponlo(R, r);
ponlo(C, c);
ponlo(A, k);
return (dp(0, 0, 0, 0).suma + dp(0, 0, 0, 0).cuantos) % modd;
}
long long sacar(int r1, int c1, int r2, int c2, int k) {
long long ayu = 0;
long long g = (ayu + saca(r2, c2, k) - saca(r1 - 1, c2, k) -
saca(r2, c1 - 1, k) + saca(r1 - 1, c1 - 1, k)) %
modd;
if (g < 0) g = g + modd;
return g;
}
int main() {
P[31] = 1;
for (int i = 30; i >= 0; i--) P[i] = P[i + 1] * 2;
ios::sync_with_stdio(false);
int ctos;
cin >> ctos;
while (ctos--) {
int r1, c1, r2, c2, k;
cin >> r1 >> c1 >> r2 >> c2 >> k;
--k;
--r1;
--r2;
--c1;
--c2;
cout << sacar(r1, c1, r2, c2, k) << "\n";
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
std::pair<long long, long long> req(long long x, long long y, long long k) {
if (k < 1) return make_pair(0, 0);
if (x == 0 || y == 0) return make_pair(0, 0);
if (x == 1 && y == 1) {
if (k >= 1)
return make_pair(1, 1);
else
return make_pair(0, 0);
}
long long deg = 0;
long long d2 = 1;
while (max(x, y) > d2) {
deg++;
d2 *= 2;
}
long long a = d2 / 2;
if (x > y) swap(x, y);
if (x <= a) {
long long m = min(a, k);
long long sm = ((m * (m + 1)) / 2) % mod;
std::pair<long long, long long> res = make_pair(x * m, x * sm);
std::pair<long long, long long> res2 = req(x, y - a, k - a);
res2.second += res2.first * a;
res.first += res2.first;
res.second = (res.second + res2.second) % mod;
res.first = res.first % mod;
return res;
}
long long m = min(a, k);
long long sm = ((m * (m + 1)) / 2) % mod;
std::pair<long long, long long> res = make_pair(a * m, a * sm);
long long m2 = min(a, k - a);
if (m2 < 0) m2 = 0;
long long sm2 = (a * m2 + (m2 * (m2 + 1)) / 2) % mod;
res.first += (x + y - 2 * a) * m2;
res.second += (x + y - 2 * a) * sm2;
std::pair<long long, long long> res2 = req(x - a, y - a, k);
res.first += res2.first;
res.first = res.first % mod;
res.second = (res.second + res2.second) % mod;
return res;
}
int main() {
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int x1, y1, x2, y2, k;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
printf("%lld\n", (req(x2, y2, k).second - req(x1 - 1, y2, k).second -
req(x2, y1 - 1, k).second +
req(x1 - 1, y1 - 1, k).second + 10 * mod) %
mod);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long t, n, m, u, v, q, k;
const int N = 2e5 + 500;
const long long mod = 1e9 + 7;
const long long INF = 1LL << 57;
long long arr[N];
string str, ss;
long long sum[64][2][2][2];
long long cnt[64][2][2][2];
long long solve(int pos, bool eqx, bool eqy, bool eqk) {
if (pos == 31) {
cnt[pos][eqx][eqy][eqk] = 1;
return 0;
}
if (cnt[pos][eqx][eqy][eqk] != -1) {
return sum[pos][eqx][eqy][eqk];
}
long long &ret = cnt[pos][eqx][eqy][eqk];
long long &ss = sum[pos][eqx][eqy][eqk];
ret = 0;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
int xbit = (1 << (30 - pos)) & n;
int ybit = (1 << (30 - pos)) & m;
int kbit = (1 << (30 - pos)) & k;
bool nx = eqx, ny = eqy, nk = eqk;
int K = i ^ j;
if (eqk && K && !kbit) continue;
if (eqx && i && !xbit) continue;
if (eqy && j && !ybit) continue;
if ((!nx) || (xbit && !i)) nx = false;
if ((!ny) || (ybit && !j)) ny = false;
if ((!nk) || (kbit && !K)) nk = false;
ss += solve(pos + 1, nx, ny, nk);
ss %= mod;
ret += cnt[pos + 1][nx][ny][nk];
ret %= mod;
ss += K * (1LL << (30 - pos)) * cnt[pos + 1][nx][ny][nk];
ss %= mod;
}
}
return ss;
}
void clear() {
memset(sum, 0, sizeof(sum));
memset(cnt, -1, sizeof(cnt));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> q;
while (q--) {
long long sol = 0;
long long xa, ya, xb, yb;
cin >> xa >> ya >> xb >> yb >> k;
xa--;
ya--;
xb--;
yb--;
k--;
clear();
n = xb, m = yb;
sol += solve(0, 1, 1, 1) + cnt[0][1][1][1];
clear();
n = xb, m = ya - 1;
if (m >= 0) sol -= solve(0, 1, 1, 1) + cnt[0][1][1][1];
clear();
n = xa - 1, m = yb;
if (n >= 0) sol -= solve(0, 1, 1, 1) + cnt[0][1][1][1];
clear();
n = xa - 1, m = ya - 1;
if (n >= 0 && m >= 0) sol += solve(0, 1, 1, 1) + cnt[0][1][1][1];
sol %= mod;
if (sol < 0) sol += mod;
cout << sol << '\n';
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
using namespace std;
const unsigned _mod = 998244353;
const unsigned mod = 1e9 + 7;
const long long infi = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
long long read() {
long long x = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
long long qpow(long long x, long long y, long long m) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % m;
y >>= 1ll;
x = x * x % m;
}
return ret;
}
long long ksm(long long x, long long y) {
long long ret = 1;
while (y) {
if (y & 1ll) ret = ret * x % mod;
y >>= 1ll;
x = x * x % mod;
}
return ret;
}
long long _gcd(long long x, long long y) { return y ? _gcd(y, x % y) : x; }
long long k, x1, x2, nibuyaozaiyongy1le, y2;
long long work(long long x, long long y, long long p) {
if (!x || !y || p >= k) return 0;
if (x < y) swap(x, y);
long long t = x, jyz;
for (; t; t -= t & -t) jyz = t;
t = jyz;
long long w = min(k, p + t);
if (t >= y)
return ((w - p) * (p + w + 1) / 2 % mod * y % mod + work(x - t, y, p + t)) %
mod;
else
return (((w - p) * (p + w + 1) / 2 % mod * t % mod +
work(x - t, min(y, t), p + t) + work(min(t, x), y - t, p + t) +
work(x - t, y - t, p)) %
mod);
}
int main() {
int T;
cin >> T;
while (T--) {
x1 = read(), nibuyaozaiyongy1le = read(), x2 = read(), y2 = read(),
k = read();
cout << (work(x2, y2, 0) + work(x1 - 1, nibuyaozaiyongy1le - 1, 0) -
work(x1 - 1, y2, 0) - work(x2, nibuyaozaiyongy1le - 1, 0) +
3 * mod) %
mod
<< '\n';
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
const int N = 100;
const int moder = 1e9 + 7;
int power[N];
int test;
int powermod(int a, int exp) {
int ret = 1;
for (; exp; exp >>= 1) {
if (exp & 1) {
ret = 1ll * ret * a % moder;
}
a = 1ll * a * a % moder;
}
return ret;
}
int sum(int x) { return 1ll * x * (x + 1) / 2 % moder; }
int solve(int x, int y, int k) {
int ret = 0;
for (int i = 0; i < 30; ++i) {
for (int j = 0; j < 30; ++j) {
if ((x >> i & 1) && (y >> j & 1)) {
int x1 = (x >> i) - 1, y1 = (y >> j) - 1;
if (i < j) {
x1 >>= j - i;
} else {
y1 >>= i - j;
}
int z1 = x1 ^ y1, z2 = z1, max = std::max(i, j);
for (int u = 0; u < max; ++u) {
z1 = z1 << 1;
z2 = z2 << 1 | 1;
}
if (k < z1) {
continue;
}
int kk = std::min(z2, k);
ret = (ret +
1ll * power[std::min(i, j)] * (sum(kk) - sum(z1 - 1) + moder)) %
moder;
ret =
(ret + 1ll * power[std::min(i, j)] * (kk - z1 + 1 + moder)) % moder;
}
}
}
return ret;
}
int main() {
power[0] = 1;
for (int i = 1; i < N; ++i) {
power[i] = 2 * power[i - 1] % moder;
}
scanf("%d", &test);
while (test--) {
int x1, x2, y1, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
--k;
int ans = (solve(x2, y2, k) + solve(x1 - 1, y1 - 1, k)) % moder;
ans = (ans - solve(x2, y1 - 1, k) + moder) % moder;
ans = (ans - solve(x1 - 1, y2, k) + moder) % moder;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
namespace IO {
const int buffer_size = 1e5 + 5;
char buf[buffer_size], *S, *T;
bool flag_EOF;
inline char read_char() {
if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin);
return S != T ? *(S++) : EOF;
}
inline int read_int() {
int flag = 1;
char c = read_char();
while (c < '0' || c > '9') {
if (c == EOF) {
flag_EOF = true;
return 0;
}
flag = (c == '-' ? -1 : 1);
c = read_char();
}
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c ^ 48);
c = read_char();
}
return x * flag;
}
char st[13];
int _top;
void Write(int x) {
if (!x) {
putchar('0');
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
while (x) {
st[++_top] = x % 10 + '0';
x /= 10;
}
while (_top > 0) putchar(st[_top--]);
}
} // namespace IO
const int P = 1e9 + 7;
inline void add(int &a, int b) { a = a + b - (a + b >= P ? P : 0); }
inline void sub(int &a, int b) { a = a - b + (a - b < 0 ? P : 0); }
inline void mul(int &a, int b) { a = 1ll * a * b % P; }
inline int get_sum(int a, int b) { return a + b - (a + b >= P ? P : 0); }
inline int get_dif(int a, int b) { return a - b + (a - b < 0 ? P : 0); }
inline int get_pro(int a, int b) { return 1ll * a * b % P; }
int A[32], B[32], C[32];
int dp[32][2][2][2], sum[32][2][2][2];
int solve(int x, int y, int k) {
if (x < 0 || y < 0 || k < 0) return 0;
for (int i = 0; i <= 30; ++i) {
A[i] = x & 1, x >>= 1;
B[i] = y & 1, y >>= 1;
C[i] = k & 1, k >>= 1;
}
reverse(A, A + 31);
reverse(B, B + 31);
reverse(C, C + 31);
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
dp[0][1][1][1] = 1;
sum[0][1][1][1] = 0;
for (int i = 0; i <= 30; ++i)
for (int a = 0; a <= 1; ++a)
for (int b = 0; b <= 1; ++b)
for (int c = 0; c <= 1; ++c)
for (int x = 0; x <= 1; ++x)
for (int y = 0; y <= 1; ++y) {
int z = x ^ y;
if (a == 1 && x > A[i]) continue;
if (b == 1 && y > B[i]) continue;
if (c == 1 && z > C[i]) continue;
add(dp[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
dp[i][a][b][c]);
add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
sum[i][a][b][c]);
add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
get_pro(z << (30 - i), dp[i][a][b][c]));
}
int res = 0;
for (int a = 0; a <= 1; ++a)
for (int b = 0; b <= 1; ++b)
for (int c = 0; c <= 1; ++c)
add(res, get_sum(dp[31][a][b][c], sum[31][a][b][c]));
return res;
}
int main() {
int q = IO::read_int();
while (q--) {
int X1 = IO::read_int() - 1, Y1 = IO::read_int() - 1,
X2 = IO::read_int() - 1, Y2 = IO::read_int() - 1,
k = IO::read_int() - 1;
int ans = get_sum(solve(X1 - 1, Y1 - 1, k), solve(X2, Y2, k));
sub(ans, get_sum(solve(X1 - 1, Y2, k), solve(X2, Y1 - 1, k)));
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
const int M = 1000000007;
using namespace std;
int q, n, L1, R1, L2, R2, K, ans, f[8], g[8], F[8], G[8], va;
inline long long cal(int A, int B) {
if (A < 0 || B < 0) return 0;
memset(f, 0, 32), f[7] = 1;
memset(F, 0, 32);
long long res = 0;
for (int i = 30; ~i; i--) {
for (int j = 0; j < 8; j++) g[j] = f[j], G[j] = F[j], f[j] = F[j] = 0;
for (int j = 0, a, b, c, k; j < 8; j++) {
for (a = 0; a < 1 || a < 2 && ((A >> i & 1) || !(j >> 0 & 1)); a++)
for (b = 0; b < 1 || b < 2 && ((B >> i & 1) || !(j >> 1 & 1)); b++) {
c = a ^ b;
if (c && (j >> 2 & 1) && !(K >> i & 1)) continue;
k = j;
if ((A >> i & 1) && (j >> 0 & 1) && !a) k ^= 1;
if ((B >> i & 1) && (j >> 1 & 1) && !b) k ^= 2;
if ((K >> i & 1) && (j >> 2 & 1) && !c) k ^= 4;
F[k] = (F[k] + G[j] + (1ll * c * g[j] << i)) % M;
f[k] = (f[k] + g[j]) % M;
}
}
}
for (int i = 0; i < 8; i++) res += F[i] + f[i];
return res;
}
int main() {
for (scanf("%d", &q); q--;) {
scanf("%d%d%d%d%d", &L1, &L2, &R1, &R2, &K);
L1 -= 2, L2 -= 2, R1--, R2--, K--;
ans = (cal(R1, R2) - cal(L1, R2) - cal(L2, R1) + cal(L1, L2)) % M;
printf("%d\n", (ans + M) % M);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int sc = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') sc = sc * 10 + ch - '0', ch = getchar();
return sc * f;
}
const int mod = 1e9 + 7;
int f[32][2][2][2], g[32][2][2][2];
inline void inc(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
inline void dec(int &x, int y) {
x -= y;
if (x < 0) x += mod;
}
inline int solve(int n, int m, int k) {
if (!n || !m) return 0;
n--;
m--;
k--;
int ret = 0;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
g[31][1][1][1] = 1;
for (int i = 31; i; i--)
for (int fn = 0; fn < 2; fn++)
for (int fm = 0; fm < 2; fm++)
for (int fk = 0; fk < 2; fk++)
if (f[i][fn][fm][fk] || g[i][fn][fm][fk]) {
int gn = n >> i - 1 & 1, gm = m >> i - 1 & 1, gk = k >> i - 1 & 1;
for (int x = 0; x <= (fn ? gn : 1); x++)
for (int y = 0; y <= (fm ? gm : 1); y++) {
int d = x ^ y;
if (fk && d && !gk) continue;
int a = fn & (x == gn), b = fm & (y == gm), c = fk & (d == gk);
inc(g[i - 1][a][b][c], g[i][fn][fm][fk]);
inc(f[i - 1][a][b][c], f[i][fn][fm][fk]);
if (d)
inc(f[i - 1][a][b][c],
(1LL << i - 1) * g[i][fn][fm][fk] % mod);
}
}
for (int fn = 0; fn < 2; fn++)
for (int fm = 0; fm < 2; fm++)
for (int fk = 0; fk < 2; fk++)
inc(ret, f[0][fn][fm][fk]), inc(ret, g[0][fn][fm][fk]);
return ret;
}
int main() {
for (int T = read(); T; T--) {
int x1 = read(), y1 = read(), x2 = read(), y2 = read(), k = read(), ret = 0;
inc(ret, solve(x2, y2, k));
dec(ret, solve(x1 - 1, y2, k));
dec(ret, solve(x2, y1 - 1, k));
inc(ret, solve(x1 - 1, y1 - 1, k));
printf("%d\n", ret);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.*;
import java.util.*;
public class MainC {
static final StdIn in = new StdIn();
static final PrintWriter out = new PrintWriter(System.out);
static final long M=(long)1e9+7;
public static void main(String[] args) {
int q=in.nextInt();
while(q-->0) {
int x1=in.nextInt(), y1=in.nextInt(), x2=in.nextInt(), y2=in.nextInt(), k=in.nextInt();
out.println((solve(x2, y2, k)-solve(x1-1, y2, k)-solve(x2, y1-1, k)+solve(x1-1, y1-1, k)+2*M)%M);
}
out.close();
}
static int x, y, k;
static long[][][] dp = new long[31][8][2];
static long solve(int x1, int y1, int k1) {
if(x1<=0||y1<=0)
return 0;
x=x1;
y=y1;
k=k1;
for(int i=0; i<dp.length; ++i)
for(int j=0; j<dp[i].length; ++j)
dp[i][j][0]=-1;
long[] ans=cdp(30, k1>=1<<30?1:0, 0, 0);
//System.out.println(x1+" "+y1+" "+k1+" "+ans);
return (ans[0]+ans[1])%M;
}
static long[] cdp(int d, int sm, int xf, int yf) {
long tx=xf==0?x%(1<<(d+1)):1<<(d+1), ty=yf==0?y%(1<<(d+1)):1<<(d+1);
if(tx==0||ty==0)
return new long[]{0, 0};
//if(sm==1)
// return new long[]{tx*ty%M, s1(tx, ty)};
if(d==0) {
long[] r = new long[]{sm==1?tx*ty:k%2*(xf+yf==2?2:1), sm==1?tx+ty-2:0};
//System.out.println(d+" "+sm+" "+xf+" "+yf+" "+r[0]+" "+r[1]);
return r;
}
if(dp[d][sm*4+xf*2+yf][0]==-1) {
long[] ans;
long ans1=0, ans2=0;
ans=cdp(d-1, (k>>d)%2|sm, (x>>d)%2|xf, (y>>d)%2|yf);
ans1+=ans[0];
ans2+=ans[1];
if(((x>>d)%2|xf)==1&&((y>>d)%2|yf)==1) {
ans=cdp(d-1, (k>>d)%2|sm, xf, yf);
ans1+=ans[0];
ans2+=ans[1];
}
if(((k>>d)%2|sm)==1) {
if(((x>>d)%2|xf)==1) {
ans=cdp(d-1, sm, xf, (y>>d)%2|yf);
ans1+=ans[0];
ans2+=ans[1]+(ans[0]<<d);
}
if(((y>>d)%2|yf)==1) {
ans=cdp(d-1, sm, (x>>d)%2|xf, yf);
ans1+=ans[0];
ans2+=ans[1]+(ans[0]<<d);
}
}
dp[d][sm*4+xf*2+yf]=new long[]{ans1%M, ans2%M};
}
//System.out.println(d+" "+sm+" "+xf+" "+yf+" "+dp[d][sm*4+xf*2+yf][0]+" "+dp[d][sm*4+xf*2+yf][1]);
return dp[d][sm*4+xf*2+yf];
}
static class StdIn {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public StdIn() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public StdIn(InputStream in) {
try{
din = new DataInputStream(in);
} catch(Exception e) {
throw new RuntimeException();
}
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String next() {
int c;
while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
StringBuilder s = new StringBuilder();
while (c != -1)
{
if (c == ' ' || c == '\n'||c=='\r')
break;
s.append((char)c);
c=read();
}
return s.toString();
}
public String nextLine() {
int c;
while((c=read())!=-1&&(c==' '||c=='\n'||c=='\r'));
StringBuilder s = new StringBuilder();
while (c != -1)
{
if (c == '\n'||c=='\r')
break;
s.append((char)c);
c = read();
}
return s.toString();
}
public int nextInt() {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public int[] readIntArray(int n) {
int[] ar = new int[n];
for(int i=0; i<n; ++i)
ar[i]=nextInt();
return ar;
}
public long nextLong() {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long[] readLongArray(int n) {
long[] ar = new long[n];
for(int i=0; i<n; ++i)
ar[i]=nextLong();
return ar;
}
public double nextDouble() {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
ret = ret * 10 + c - '0';
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() {
try{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
} catch(IOException e) {
throw new RuntimeException();
}
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
} | JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mo = 1e9 + 7;
long long sum[100];
long long solve(int x, int y, int k, long long delta) {
if (x <= 0 || y <= 0) return 0;
if (x > y) swap(x, y);
if (k < 0) return 0;
int m = 0, len = 1;
for (; len * 2 <= y; m++, len *= 2)
;
if (len == y) {
if (k >= len)
return (1ll * (len + 1) * len / 2 % mo * x % mo +
1ll * delta * len % mo * x % mo) %
mo;
else
return (1ll * (k + 1) * k / 2 % mo * x % mo +
1ll * delta * k % mo * x % mo) %
mo;
}
long long ans = 0;
if (x <= len) {
ans = (ans + solve(x, len, k, delta)) % mo;
ans = (ans + solve(x, y - len, k - len, delta + len)) % mo;
} else {
ans = (ans + solve(len, len, k, delta)) % mo;
ans = (ans + solve(len, y - len, k - len, delta + len)) % mo;
ans = (ans + solve(x - len, len, k - len, delta + len)) % mo;
ans = (ans + solve(x - len, y - len, k, delta)) % mo;
}
return ans;
}
int main() {
cin.sync_with_stdio(0);
for (int i = 0; i < 30; ++i) {
long long p = 1 << i;
sum[i] = (p + 1) * p / 2 * p;
}
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
long long a1 = solve(x2, y2, k, 0);
long long a2 = solve(x2, y1 - 1, k, 0);
long long a3 = solve(x1 - 1, y2, k, 0);
long long a4 = solve(x1 - 1, y1 - 1, k, 0);
cout << ((a1 + a4 - a2 - a3) % mo + mo) % mo << endl;
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int P = 1e9 + 7, INF = 0x3f3f3f3f;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long qpow(long long a, long long n) {
long long r = 1 % P;
for (a %= P; n; a = a * a % P, n >>= 1)
if (n & 1) r = r * a % P;
return r;
}
long long inv(long long first) {
return first <= 1 ? 1 : inv(P % first) * (P - P / first) % P;
}
inline int rd() {
int first = 0;
char p = getchar();
while (p < '0' || p > '9') p = getchar();
while (p >= '0' && p <= '9') first = first * 10 + p - '0', p = getchar();
return first;
}
const int N = 1e6 + 50;
int f[33][2][2][2], g[33][2][2][2];
void add(int &a, long long b) { a = (a + b) % P; }
int calc(int first, int second, int k) {
if (first < 0 || second < 0) return 0;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
f[32][1][1][1] = 1;
for (int i = 31; i >= 0; --i)
for (int a1 = 0; a1 <= 1; ++a1)
for (int a2 = 0; a2 <= 1; ++a2)
for (int a3 = 0; a3 <= 1; ++a3) {
int &r = f[i + 1][a1][a2][a3];
if (!r) continue;
int l1 = a1 && !(first >> i & 1), l2 = a2 && !(second >> i & 1),
l3 = a3 && !(k >> i & 1);
for (int b1 = 0; b1 <= 1; ++b1)
for (int b2 = 0; b2 <= 1; ++b2) {
if (l1 && b1) continue;
if (l2 && b2) continue;
if (l3 && (b1 ^ b2)) continue;
int c1 = a1 && b1 >= (first >> i & 1),
c2 = a2 && b2 >= (second >> i & 1),
c3 = a3 && (b1 ^ b2) >= (k >> i & 1);
add(f[i][c1][c2][c3], r);
add(g[i][c1][c2][c3], g[i + 1][a1][a2][a3] * 2ll + (b1 ^ b2) * r);
}
}
int ans = 0;
for (int a1 = 0; a1 <= 1; ++a1)
for (int a2 = 0; a2 <= 1; ++a2)
for (int a3 = 0; a3 <= 1; ++a3)
add(ans, g[0][a1][a2][a3] + f[0][a1][a2][a3]);
return ans;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x1, y1, x2, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
--x1, --y1, --x2, --y2, --x1, --y1, --k;
int ans = (calc(x2, y2, k) - calc(x1, y2, k) - calc(x2, y1, k) +
calc(x1, y1, k)) %
P;
if (ans < 0) ans += P;
printf("%d\n", ans);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 35;
const long long MOD = 1000000007LL;
long long dp[MAX_N][2][2][2][2];
int query, k;
int a[MAX_N], b[MAX_N], c[MAX_N];
void inc(long long &X, long long val) { (X += val) %= MOD; }
long long calc(int x, int y) {
if (x == 0 || y == 0) {
return 0LL;
}
long long res = 0LL;
memset(dp, 0, sizeof(dp));
int xx = x - 1;
int yy = y - 1;
int kk = k - 1;
for (int i = 0; i < 32; i++) {
a[i] = xx >> i & 1;
b[i] = yy >> i & 1;
c[i] = kk >> i & 1;
}
dp[31][1][1][1][1] = 1;
for (int i = 31; i >= 1; i--) {
for (int flagX = 0; flagX < 2; flagX++) {
for (int flagY = 0; flagY < 2; flagY++) {
for (int flagK = 0; flagK < 2; flagK++) {
if (dp[i][flagX][flagY][flagK][1] > 0) {
for (int f1 = 0; f1 < 2; f1++)
for (int f2 = 0; f2 < 2; f2++) {
if (flagX == 1 && f1 > a[i - 1] ||
flagY == 1 && f2 > b[i - 1] ||
flagK == 1 && ((f1 ^ f2) > c[i - 1])) {
continue;
}
inc(dp[i - 1][flagX == 1 && f1 == a[i - 1]]
[flagY == 1 && f2 == b[i - 1]]
[flagK == 1 && ((f1 ^ f2) == c[i - 1])][1],
dp[i][flagX][flagY][flagK][1]);
inc(dp[i - 1][flagX == 1 && f1 == a[i - 1]]
[flagY == 1 && f2 == b[i - 1]]
[flagK == 1 && ((f1 ^ f2) == c[i - 1])][0],
(dp[i][flagX][flagY][flagK][0] * 2ll +
dp[i][flagX][flagY][flagK][1] * (f1 ^ f2)) %
MOD);
}
}
}
}
}
}
for (int flagX = 0; flagX < 2; flagX++) {
for (int flagY = 0; flagY < 2; flagY++) {
for (int flagK = 0; flagK < 2; flagK++) {
(res +=
dp[0][flagX][flagY][flagK][0] + dp[0][flagX][flagY][flagK][1]) %= MOD;
}
}
}
return res;
}
int main() {
scanf("%d", &query);
while (query--) {
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
scanf("%d", &k);
printf("%I64d\n", (calc(x2, y2) - calc(x1 - 1, y2) - calc(x2, y1 - 1) +
calc(x1 - 1, y1 - 1) + 3LL * MOD) %
MOD);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import java.util.StringTokenizer;
public class _809C {
/**
* Immutable class
*/
static class Key {
final int len;
final int height;
final int cap;
final int hash;
public Key(int len, int height, int cap) {
this.len = len;
this.height = height;
this.cap = cap;
int h = len;
h = h * 31 + height;
h = h * 31 + cap;
this.hash = h;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Key key = (Key) o;
if (len != key.len) return false;
if (height != key.height) return false;
return cap == key.cap;
}
@Override
public int hashCode() {
return hash;
}
@Override
public String toString() {
return "Key{" + "len=" + len + ", height=" + height + ", cap=" + cap + '}';
}
}
static class Value {
long cnt;
long sum;
@Override
public String toString() {
return "Value{" + "cnt=" + cnt + ", sum=" + sum + '}';
}
}
static final int MOD = 1000000007;
static final int INV_2 = 500000004;
static Map<Key, Value> f = new HashMap<>();
public static void main(String[] args) throws Exception {
Reader.init(System.in);
BufferedWriter cout = new BufferedWriter(new OutputStreamWriter(System.out));
final int MAX_LEN = 1 << 30;
StringJoiner joiner = new StringJoiner("\n");
for (int Q = Reader.nextInt(); Q > 0; Q--) {
int x1 = Reader.nextInt();
int y1 = Reader.nextInt();
int x2 = Reader.nextInt();
int y2 = Reader.nextInt();
int cap = Reader.nextInt();
long ans = g(MAX_LEN, x2, y2, cap).sum;
ans -= g(MAX_LEN, x1 - 1, y2, cap).sum;
ans -= g(MAX_LEN, x2, y1 - 1, cap).sum;
ans += g(MAX_LEN, x1 - 1, y1 - 1, cap).sum;
ans += 2L * MOD;
joiner.add("" + (ans % MOD));
}
System.out.println(joiner.toString());
cout.close();
}
static Value f(int len, int height, int cap) {
cap = Math.max(cap, 0);
cap = Math.min(cap, len);
height = Math.max(height, 0);
height = Math.min(height, len);
Key key = new Key(len, height, cap);
if (f.containsKey(key)) return f.get(key);
Value value = new Value();
if (cap < 1 || height < 1)
value.cnt = value.sum = 0;
else if (cap == len) {
value.cnt = (long) len * height % MOD;
value.sum = (1L + len) * len % MOD * INV_2 % MOD * height % MOD;
} else {
int halfLen = len / 2;
add(value, f(halfLen, height, cap), 0);
add(value, f(halfLen, height, cap - halfLen), halfLen);
add(value, f(halfLen, height - halfLen, cap - halfLen), halfLen);
add(value, f(halfLen, height - halfLen, cap), 0);
}
f.put(key, value);
return value;
}
static void add(Value acc, Value v, int extra) {
acc.cnt = (acc.cnt + v.cnt) % MOD;
acc.sum = (acc.sum + v.sum + v.cnt * extra) % MOD;
}
static Value g(int len, int height, int width, int cap) {
height = Math.min(height, len);
width = Math.min(width, len);
if (height > width)
return g(len, width, height, cap);
Value value = new Value();
if (cap < 1 || height < 1)
value.cnt = value.sum = 0;
else if (len == 1)
value.cnt = value.sum = 1;
else {
int halfLen = len / 2;
if (width <= halfLen)
add(value, g(halfLen, height, width, cap), 0);
else if (height <= halfLen) {
add(value, f(halfLen, height, cap), 0);
add(value, g(halfLen, height, width - halfLen, cap - halfLen), halfLen);
} else {
add(value, f(halfLen, halfLen, cap), 0);
add(value, f(halfLen, height - halfLen, cap - halfLen), halfLen);
add(value, f(halfLen, width - halfLen, cap - halfLen), halfLen);
add(value, g(halfLen, height - halfLen, width - halfLen, cap), 0);
}
}
return value;
}
/**
* Class for buffered reading int and double values
*/
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
} | JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll getrnd(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rng); }
template <typename T1, typename T2>
inline bool relax(T1& a, const T2& b) {
return a > b ? a = b, true : false;
}
template <typename T1, typename T2>
inline bool strain(T1& a, const T2& b) {
return a < b ? a = b, true : false;
}
const int MOD = 1e9 + 7;
int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
return a;
}
int mul(int a, int b) { return 1LL * a * b % MOD; }
int binpow(int a, ll n) {
if (n < 0) a = binpow(a, MOD - 2), n *= -1;
int res = 1;
while (n) {
if (n & 1) res = mul(res, a);
a = mul(a, a);
n /= 2;
}
return res;
}
int inv(int x) { return binpow(x, MOD - 2); }
const int N = 31;
int dp[N + 1][2][2][2], sum[N + 1][2][2][2];
int f(int x, int y, int k) {
if (x < 0 || y < 0) return 0;
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
dp[0][0][0][0] = 1;
for (int i = 0; i < N; ++i) {
for (int smx = 0; smx <= 1; ++smx) {
for (int smy = 0; smy <= 1; ++smy) {
for (int smk = 0; smk <= 1; ++smk) {
for (int bx = 0; bx <= 1; ++bx) {
for (int by = 0; by <= 1; ++by) {
int bk = bx ^ by;
if ((!smx && bx > ((x >> (N - i - 1)) & 1)) ||
(!smy && by > ((y >> (N - i - 1)) & 1)) ||
(!smk && bk > ((k >> (N - i - 1)) & 1)))
continue;
int nsmx = smx || (bx < ((x >> (N - i - 1)) & 1));
int nsmy = smy || (by < ((y >> (N - i - 1)) & 1));
int nsmk = smk || (bk < ((k >> (N - i - 1)) & 1));
dp[i + 1][nsmx][nsmy][nsmk] =
add(dp[i + 1][nsmx][nsmy][nsmk], dp[i][smx][smy][smk]);
sum[i + 1][nsmx][nsmy][nsmk] = add(
sum[i + 1][nsmx][nsmy][nsmk],
add(sum[i][smx][smy][smk], mul(mul(bk, binpow(2, N - i - 1)),
dp[i][smx][smy][smk])));
}
}
}
}
}
}
int ans = 0;
for (int smx = 0; smx <= 1; ++smx)
for (int smy = 0; smy <= 1; ++smy)
for (int smk = 0; smk <= 1; ++smk)
ans = add(ans, add(sum[N][smx][smy][smk], dp[N][smx][smy][smk]));
return ans;
}
void solve() {
int q;
cin >> q;
while (q--) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
--x1;
--y1;
--x2;
--y2;
--k;
int res = f(x2, y2, k);
res = add(res, -add(f(x1 - 1, y2, k), f(x2, y1 - 1, k)));
res = add(res, f(x1 - 1, y1 - 1, k));
cout << res << '\n';
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
while (t--) solve();
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int maxs = 40;
const int N = 30;
const int Mo = 1000000007;
int f[maxs][2][2][2], g[maxs][2][2][2];
int DP(int n, int m, int K) {
K--, n--, m--;
if (K < 0 || n < 0 || m < 0) return 0;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
g[N + 1][1][1][1] = 1;
int s, sn, sm, sk, i, j, k, x, y, z, ii, jj, kk, gg, ff;
for (s = N; s >= 0; --s) {
sn = ((n >> s) & 1), sm = ((m >> s) & 1), sk = ((K >> s) & 1);
for (i = 0; i < 2; ++i) {
for (j = 0; j < 2; ++j) {
for (k = 0; k < 2; ++k) {
gg = g[s + 1][i][j][k], ff = f[s + 1][i][j][k];
if (gg + ff == 0) continue;
for (x = 0; x < 2; ++x) {
for (y = 0; y < 2; ++y) {
z = (x ^ y), ii = i, jj = j, kk = k;
if (i && x < sn) ii = 0;
if (i && x > sn) continue;
if (j && y < sm) jj = 0;
if (j && y > sm) continue;
if (k && z < sk) kk = 0;
if (k && z > sk) continue;
g[s][ii][jj][kk] = (g[s][ii][jj][kk] + gg) % Mo;
f[s][ii][jj][kk] =
(1LL * z * (1 << s) * gg + ff + f[s][ii][jj][kk]) % Mo;
}
}
}
}
}
}
int ans = 0;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
ans = ((long long)ans + f[0][i][j][k] + g[0][i][j][k]) % Mo;
}
}
}
return ans;
}
int main() {
int sumt, x1, y1, x2, y2, k;
scanf("%d", &sumt);
while (sumt--) {
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
int ans = (DP(x2, y2, k) + DP(x1 - 1, y1 - 1, k)) % Mo;
ans = (ans - DP(x1 - 1, y2, k)) % Mo;
ans = (ans - DP(x2, y1 - 1, k)) % Mo;
ans = (ans % Mo + Mo) % Mo;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007ll;
struct data {
long long num, sum;
data() {}
data(long long n, long long s) {
num = n;
sum = s;
}
data operator+(const data& p) const {
return data((num + p.num) % mod, (sum + p.sum) % mod);
}
};
data dp[65][2][2][2];
long long x, y, k;
data func2(int pos, int fx, int fy, int fk) {
if (pos == -1) {
return data((fx == 0) && (fy == 0) && (fk == 0), 0);
}
data& res = dp[pos][fx][fy][fk];
if (res.num != -1ll) return res;
res = data(0, 0);
int lx = (fx) ? ((x >> pos) & 1) : 1;
int ly = (fy) ? ((y >> pos) & 1) : 1;
int lk = (fk) ? ((k >> pos) & 1) : 1;
for (int i = 0; i <= lx; i++)
for (int j = 0; j <= ly; j++) {
int xorr = i ^ j;
if (lk < xorr) continue;
data tem =
func2(pos - 1, fx && (i == lx), fy && (j == ly), fk && (xorr == lk));
if (xorr) {
long long sum = (1ll << pos) % mod;
sum *= tem.num;
sum %= mod;
tem = tem + data(0ll, sum);
}
res = res + tem;
}
return res;
}
long long func(long long xx, long long yy, long long kk) {
memset(dp, -1, sizeof(dp));
x = xx;
y = yy;
k = kk;
data res = func2(60, 1, 1, 1);
return (res.num + res.sum) % mod;
}
int main() {
int q;
long long x1, y1, x2, y2, k;
scanf("%d", &q);
while (q--) {
scanf("%lld %lld %lld %lld %lld", &x1, &y1, &x2, &y2, &k);
long long ans = func(x2, y2, k);
ans += func(x1 - 1, y1 - 1, k);
ans -= func(x2, y1 - 1, k);
ans -= func(x1 - 1, y2, k);
ans %= mod;
printf("%lld\n", (ans + mod) % mod);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline long long max(long long a, long long b) { return a > b ? a : b; }
inline long long min(long long a, long long b) { return a < b ? a : b; }
const long long mod = 1000000007;
int d[70][70], vis[70], visn;
long long p2[40];
void init() {
p2[0] = 1;
for (int i = 1; i < 40; ++i) p2[i] = p2[i - 1] * 2;
}
long long gl(long long v) { return *lower_bound(p2, p2 + 40, v); }
long long sum(long long v) { return v * (v + 1) / 2 % mod; }
long long calc(int x, int y, int k, long long &rn) {
rn = 0;
if (k <= 0) return 0;
if (x == 0 || y == 0) return 0;
if (x < y) swap(x, y);
long long n = gl(x), m;
if (n == 1) {
rn = 1;
return 1;
}
long long ret;
if (y >= n / 2) {
ret = calc(n - x, n - y, k, m);
rn = (min(n, k) * n % mod + mod - min(n, k) * (n - x + n - y) % mod + m) %
mod;
return (sum(min(n, k)) * n - sum(min(n, k)) * (n - x + n - y) % mod + ret) %
mod;
}
rn = min(n, k) * y % mod;
if (x == n) return sum(min(n, k)) * y % mod;
ret = calc(x - n / 2, y, k - n / 2, m);
rn = (min(n / 2, k) * y % mod + m) % mod;
return (sum(min(n / 2, k)) * y % mod + ret + m * (n / 2) % mod) % mod;
}
int main() {
int tc;
init();
for (scanf("%d", &tc); tc--;) {
int x1, y1, x2, y2, k;
long long m;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k), --x1, --y1;
printf("%lld\n", (calc(x2, y2, k, m) + mod - calc(x1, y2, k, m) + mod -
calc(x2, y1, k, m) + calc(x1, y1, k, m)) %
mod);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
long long f[33][2][2][2], g[33][2][2][2];
int dig1[33], dig2[33], dig3[33], k;
pair<long long, long long> dfs(int pos, int st1, int st2, int st3) {
if (pos == -1) return make_pair(1, 0);
if (f[pos][st1][st2][st3] != -1)
return make_pair(f[pos][st1][st2][st3], g[pos][st1][st2][st3]);
long long ans = 0;
long long sum = 0;
int n1 = st1 ? 1 : dig1[pos];
int n2 = st2 ? 1 : dig2[pos];
for (int i = 0; i <= n1; i++) {
for (int j = 0; j <= n2; j++) {
int now_st1 = st1 || i < n1;
int now_st2 = st2 || j < n2;
int now_st3 = st3 || (i ^ j) < dig3[pos];
if (st3 == 0 && (i ^ j) > dig3[pos]) continue;
pair<long long, long long> res = dfs(pos - 1, now_st1, now_st2, now_st3);
sum += (i ^ j) * res.first * 1ll * (1 << pos) % mod + res.second % mod;
ans += res.first;
}
}
ans %= mod;
sum %= mod;
return make_pair(f[pos][st1][st2][st3] = ans, g[pos][st1][st2][st3] = sum);
}
long long cal(int x, int y) {
memset(f, -1, sizeof f);
memset(g, -1, sizeof g);
memset(dig1, 0, sizeof dig1);
memset(dig2, 0, sizeof dig2);
int pos = 0;
while (x) dig1[pos++] = x & 1, x >>= 1;
pos = 0;
while (y) dig2[pos++] = y & 1, y >>= 1;
pair<long long, long long> ans = dfs(30, 0, 0, 0);
return ans.first + ans.second;
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
int x1, y1, x2, y2;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
memset(dig3, 0, sizeof dig3);
x1--, x2--, y1--, y2--;
int pos = 0, p = k - 1;
while (p) dig3[pos++] = p & 1, p >>= 1;
long long ans = 0;
ans += cal(x2, y2);
if (x1) ans -= cal(x1 - 1, y2);
if (y1) ans -= cal(x2, y1 - 1);
if (x1 && y1) ans += cal(x1 - 1, y1 - 1);
ans %= mod;
if (ans < 0) ans += mod;
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int modulo = (int)1e9 + 7;
inline int64_t progression(int l, int r) {
if (l > r) {
return 0;
}
return ((int64_t)(r - l + 1) * (l + r) / 2) % modulo;
}
inline int64_t getMaskAns(int head1, int head2, int len1, int len2, int k) {
if (len1 < len2) {
swap(len1, len2);
swap(head1, head2);
}
head2 = head2 & ~((1 << len1) - 1);
int minMask = head1 ^ head2;
int maxMask = minMask + (1 << len1) - 1;
k = min(k, maxMask);
int64_t res =
(((int64_t)1 << len2) * progression(minMask + 1, k + 1)) % modulo;
return res;
}
inline vector<pair<int, int> > toMasks(int val) {
vector<pair<int, int> > ans;
for (int i = 30; i >= 0; i--) {
if ((val >> i) & 1) {
int msk = val & ~((1 << (i + 1)) - 1);
int len = i;
ans.emplace_back(msk, len);
}
}
ans.emplace_back(val, 0);
return ans;
}
inline int64_t getAns(int x, int y, int k) {
if (x < 0 || y < 0) {
return 0;
}
int64_t ans = 0;
for (pair<int, int> pX : toMasks(x)) {
for (pair<int, int> pY : toMasks(y)) {
ans += getMaskAns(pX.first, pY.first, pX.second, pY.second, k);
ans %= modulo;
}
}
return ans;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--;
y1--;
x2--;
y2--;
k--;
int64_t theAns = getAns(x2, y2, k) - getAns(x1 - 1, y2, k) -
getAns(x2, y1 - 1, k) + getAns(x1 - 1, y1 - 1, k);
while (theAns < 0) {
theAns += modulo;
}
theAns %= modulo;
cout << theAns << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
static long long dp[32][2][2][2];
static long long sum[32][2][2][2];
static long long pot[32];
void add(long long &x, long long y) {
x += y;
if (x >= (int)(1e9 + 7)) {
x -= (int)(1e9 + 7);
}
}
void sub(long long &x, long long y) {
x -= y;
if (x < 0) {
x += (int)(1e9 + 7);
}
}
long long mul(long long x, long long y) { return x * y % (int)(1e9 + 7); }
long long solve(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0) {
return 0;
}
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
vector<int> A, B, C;
for (int i = 0; i < 31; i++) {
A.push_back(x % 2);
x /= 2;
B.push_back(y % 2);
y /= 2;
C.push_back(z % 2);
z /= 2;
}
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
reverse(C.begin(), C.end());
dp[0][1][1][1] = 1;
sum[0][1][1][1] = 0;
for (int i = 0; i < 31; i++) {
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int z = x ^ y;
if (a == 1 && A[i] == 0 && x == 1) {
continue;
}
if (b == 1 && B[i] == 0 && y == 1) {
continue;
}
if (c == 1 && C[i] == 0 && z == 1) {
continue;
}
add(dp[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
dp[i][a][b][c]);
add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
sum[i][a][b][c]);
add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
mul(z << (30 - i), dp[i][a][b][c]));
}
}
}
}
}
}
long long res = 0;
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
add(res, sum[31][a][b][c]);
add(res, dp[31][a][b][c]);
}
}
}
return res;
}
int main(int argc, const char *argv[]) {
int test_case = 0;
cin >> test_case;
for (int test = 0; test < test_case; test++) {
int x1 = 0, y1 = 0, x2 = 0, y2 = 0, k = 0;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--;
x2--;
y1--;
y2--;
k--;
long long res = 0;
add(res, solve(x2, y2, k));
sub(res, solve(x2, y1 - 1, k));
sub(res, solve(x1 - 1, y2, k));
add(res, solve(x1 - 1, y1 - 1, k));
cout << res << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 1e9 + 7, L = 31;
int get(int x, int y, int k) {
if (x == -1 || y == -1) return 0;
int dp[L + 1][2][2][2], cnt[L + 1][2][2][2];
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
cnt[L][1][1][1] = 1;
for (int i = L - 1; i >= 0; i--) {
for (int prex = 0; prex < 2; prex++) {
for (int prey = 0; prey < 2; prey++) {
for (int prek = 0; prek < 2; prek++) {
for (int ix = 0; ix < 2; ix++) {
for (int iy = 0; iy < 2; iy++) {
if (prex && ix && !((1 << i) & x)) continue;
if (prey && iy && !((1 << i) & y)) continue;
if ((ix ^ iy) && prek && !((1 << i) & k)) continue;
dp[i][prex & (min(((1 << i) & x), 1) == ix)]
[prey & (min(((1 << i) & y), 1) == iy)]
[prek & (min(((1 << i) & k), 1) == (ix ^ iy))] +=
(dp[i + 1][prex][prey][prek] +
1ll * cnt[i + 1][prex][prey][prek] * (1 << i) * (ix ^ iy) %
mod) %
mod;
cnt[i][prex & (min(((1 << i) & x), 1) == ix)]
[prey & (min(((1 << i) & y), 1) == iy)]
[prek & (min(((1 << i) & k), 1) == (ix ^ iy))] +=
cnt[i + 1][prex][prey][prek];
dp[i][prex & (min(((1 << i) & x), 1) == ix)]
[prey & (min(((1 << i) & y), 1) == iy)]
[prek & (min(((1 << i) & k), 1) == (ix ^ iy))] %= mod;
cnt[i][prex & (min(((1 << i) & x), 1) == ix)]
[prey & (min(((1 << i) & y), 1) == iy)]
[prek & (min(((1 << i) & k), 1) == (ix ^ iy))] %= mod;
}
}
}
}
}
}
int ret = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
ret += (dp[0][i][j][k] + cnt[0][i][j][k]) % mod;
ret %= mod;
}
return ret;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) {
int x, y, z, w, k;
cin >> x >> y >> z >> w >> k;
x--;
y--;
z--;
w--;
k--;
int ans = (get(z, w, k) - get(z, y - 1, k) - get(x - 1, w, k) +
get(x - 1, y - 1, k)) %
mod;
if (ans < 0) ans += mod;
cout << ans << "\n";
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class Main {
private static final String NO = "NO";
private static final String YES = "YES";
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long MOD = 1000000007;
void solve() {
int T = 1;
for (int i = 0; i < T; i++)
solve(i);
}
long p[];
void solve(int T) {
int n = ni();
p = new long[64];
p[0] = 1;
for (int i = 1; i < 60; i++)
p[i] = p[i - 1] * 2;
for (int i = 0; i < n; i++) {
int a[] = na(5);
long ans = work(a[2], a[3], a[4], 0) - work(a[0] - 1, a[3], a[4], 0) - work(a[2], a[1] - 1, a[4], 0)
+ work(a[0] - 1, a[1] - 1, a[4], 0);
ans %= MOD;
out.println((ans + MOD) % MOD);
}
}
long work(long x, long y, long k, long lazy) {
if (x <= 0 || y <= 0)
return 0;
if (k <= 0)
return 0;
if (x > y) {
long tmp = x;
x = y;
y = tmp;
}
int t = 0;
while (p[t] <= y)
t++;
long ans;
if (y == p[t - 1]) {
long tmpa, tmpb;
if (p[t - 1] < k)// all numbers is within [1..k], count them all
{
tmpa = p[t - 1];
tmpa = tmpa * (tmpa + 1) / 2;
tmpa %= MOD;
tmpa *= x;
tmpa %= MOD;
tmpb = (lazy * y) % MOD;
tmpb *= x;
tmpb %= MOD;
ans = (tmpa + tmpb) % MOD;
} else// p[t-1]>=k only less or equal to k is needed
{
tmpa = k;
tmpa = tmpa * (tmpa + 1) / 2;
tmpa %= MOD;
tmpa *= x;
tmpa %= MOD;
tmpb = (lazy * k) % MOD;
tmpb *= x;
tmpb %= MOD;
ans = (tmpa + tmpb) % MOD;
}
} else// y > p[t-1]
{
if (x < p[t - 1]) {
ans = work(x, p[t - 1], k, lazy);
ans = (ans + work(x, y - p[t - 1], k - p[t - 1], lazy + p[t - 1])) % MOD;
} else {
ans = work(p[t - 1], p[t - 1], k, lazy);
ans = (ans + work(x - p[t - 1], p[t - 1], k - p[t - 1], lazy + p[t - 1])) % MOD;
ans = (ans + work(p[t - 1], y - p[t - 1], k - p[t - 1], lazy + p[t - 1])) % MOD;
ans = (ans + work(x - p[t - 1], y - p[t - 1], k, lazy)) % MOD;
}
}
// tr(x, y, k, lazy, ans);
return ans;
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private boolean vis[];
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private List<Integer> na2(int n) {
List<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
a.add(ni());
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public class Pair<K, V> {
/**
* Key of this <code>Pair</code>.
*/
private K key;
/**
* Gets the key for this pair.
*
* @return key for this pair
*/
public K getKey() {
return key;
}
/**
* Value of this this <code>Pair</code>.
*/
private V value;
/**
* Gets the value for this pair.
*
* @return value for this pair
*/
public V getValue() {
return value;
}
/**
* Creates a new pair
*
* @param key The key for this pair
* @param value The value to use for this pair
*/
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
/**
* <p>
* <code>String</code> representation of this <code>Pair</code>.
* </p>
*
* <p>
* The default name/value delimiter '=' is always used.
* </p>
*
* @return <code>String</code> representation of this <code>Pair</code>
*/
@Override
public String toString() {
return key + "=" + value;
}
/**
* <p>
* Generate a hash code for this <code>Pair</code>.
* </p>
*
* <p>
* The hash code is calculated using both the name and the value of the
* <code>Pair</code>.
* </p>
*
* @return hash code for this <code>Pair</code>
*/
@Override
public int hashCode() {
// name's hashCode is multiplied by an arbitrary prime number (13)
// in order to make sure there is a difference in the hashCode between
// these two parameters:
// name: a value: aa
// name: aa value: a
return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
}
/**
* <p>
* Test this <code>Pair</code> for equality with another <code>Object</code>.
* </p>
*
* <p>
* If the <code>Object</code> to be tested is not a <code>Pair</code> or is
* <code>null</code>, then this method returns <code>false</code>.
* </p>
*
* <p>
* Two <code>Pair</code>s are considered equal if and only if both the names and
* values are equal.
* </p>
*
* @param o the <code>Object</code> to test for equality with this
* <code>Pair</code>
* @return <code>true</code> if the given <code>Object</code> is equal to this
* <code>Pair</code> else <code>false</code>
*/
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o instanceof Pair) {
Pair pair = (Pair) o;
if (key != null ? !key.equals(pair.key) : pair.key != null)
return false;
if (value != null ? !value.equals(pair.value) : pair.value != null)
return false;
return true;
}
return false;
}
}
} | JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <typename T>
T gcd(T a, T b) {
return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T LCM(T a, T b) {
return a * (b / gcd(a, b));
}
template <typename T>
T expo(T base, T e, T mod) {
T res = 1;
while (e > 0) {
if (e & 1) res = res * base % mod;
base = base * base % mod;
e >>= 1;
}
return res;
}
template <typename T, typename second>
T expo(T b, second e) {
if (e <= 1) return e == 0 ? 1 : b;
return (e & 1) == 0 ? expo((b * b), e >> 1) : (b * expo((b * b), e >> 1));
}
template <typename T, typename second>
T modinv(T a, second mod) {
return expo(a, mod - 2, mod);
}
template <class T, class second>
std::ostream &operator<<(std::ostream &os, const std::pair<T, second> &t) {
os << "(" << t.first << ", " << t.second << ")";
return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &t) {
os << "[";
for (__typeof(t.begin()) it = t.begin(); it != t.end(); it++) {
if (it != t.begin()) os << ", ";
os << *it;
}
os << "]";
return os;
}
template <typename T>
void in(T &x) {
T c = getchar();
while (((c < 48) || (c > 57)) && (c != '-')) c = getchar();
bool neg = false;
if (c == '-') neg = true;
x = 0;
for (; c < 48 || c > 57; c = getchar())
;
for (; c > 47 && c < 58; c = getchar()) x = (x * 10) + (c - 48);
if (neg) x = -x;
}
const int BITS = 31;
long long cnt[BITS + 3][2][2][2];
long long dp[BITS + 3][2][2][2];
void compute_cnt(long long r, long long c, long long k) {
memset(cnt, 0, sizeof cnt);
memset(dp, 0, sizeof dp);
cnt[BITS][1][1][1] = 1;
dp[BITS][1][1][1] = 0;
for (int bits = BITS; bits > 0; bits--) {
for (int r_flag = 0; r_flag < 2; r_flag++) {
for (int c_flag = 0; c_flag < 2; c_flag++) {
for (int k_flag = 0; k_flag < 2; k_flag++) {
for (int r_bit = 0; r_bit < 2; r_bit++) {
for (int c_bit = 0; c_bit < 2; c_bit++) {
int cr = (r >> (bits - 1)) & 1;
int cc = (c >> (bits - 1)) & 1;
int ck = (k >> (bits - 1)) & 1;
int xb = r_bit ^ c_bit;
if (r_flag * r_bit > cr || c_flag * c_bit > cc ||
k_flag * xb > ck) {
} else {
int nr_flag = r_flag & (r_bit == cr);
int nc_flag = c_flag & (c_bit == cc);
int nk_flag = k_flag & (xb == ck);
cnt[bits - 1][nr_flag][nc_flag][nk_flag] +=
cnt[bits][r_flag][c_flag][k_flag];
cnt[bits - 1][nr_flag][nc_flag][nk_flag] %= mod;
dp[bits - 1][nr_flag][nc_flag][nk_flag] +=
dp[bits][r_flag][c_flag][k_flag];
if (xb == 1) {
dp[bits - 1][nr_flag][nc_flag][nk_flag] +=
expo(2ll, (long long)bits - 1, mod) *
cnt[bits][r_flag][c_flag][k_flag] % mod;
}
dp[bits - 1][nr_flag][nc_flag][nk_flag] %= mod;
}
}
}
}
}
}
}
}
long long solve_prefix(long long r, long long c, long long k) {
if (r < 0 || c < 0) return 0;
compute_cnt(r, c, k);
long long res = 0;
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
for (int z = 0; z < 2; z++) {
res += dp[0][x][y][z] + cnt[0][x][y][z];
if (res >= mod) res -= mod;
}
}
}
return res;
}
long long solve(long long r1, long long c1, long long r2, long long c2,
long long k) {
long long res =
solve_prefix(r2, c2, k - 1) + solve_prefix(r1 - 1, c1 - 1, k - 1);
res -= solve_prefix(r1 - 1, c2, k - 1) + solve_prefix(r2, c1 - 1, k - 1);
res %= mod;
if (res < 0) res += mod;
return res;
}
int main() {
int q;
long long x1, y1, x2, y2, k;
in(q);
for (int i = 0; i < q; i++) {
in(x1), in(y1), in(x2), in(y2), in(k);
x1--, y1--, x2--, y2--;
printf("%lld\n", solve(x1, y1, x2, y2, k));
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long int Mod = 1000 * 1000 * 1000 + 7;
template <typename T, T fenmod = 0>
struct BIT {
T *fen;
int size;
BIT(int size) { fen = new T[size + 1], this->size = size; }
void inc(int idx, T val) {
for (idx++; idx <= size; idx += idx & -idx) {
fen[idx] += val;
if (fenmod) fen[idx] %= fenmod;
}
}
T get(int idx) {
T sum = 0;
for (; idx; idx -= idx & -idx) {
sum += fen[idx];
if (fenmod) sum %= fenmod;
}
return sum;
}
T get(int st, int en) { return get(en) - get(st); }
};
struct SegmentTree {
int *seg;
int segL, segR;
SegmentTree(int segl, int segr) {
segL = segl;
segR = segr;
seg = new int[(segr - segl) << 2];
}
int build(int l, int r, int id) {
if (r - l == 1) {
seg[id] = l;
return seg[id];
}
int mid = (l + r) >> 1;
int res = 0;
res += build(l, mid, id << 1 | 0);
res += build(mid, r, id << 1 | 1);
seg[id] = res;
return res;
}
int build() { return build(segL, segR, 1); }
int pointQuery(int idx, int val, int l, int r, int id) {
if (idx < l or r <= idx) return 0;
if (r - l == 1) {
seg[id] = val;
return seg[id];
}
int mid = (l + r) >> 1;
int res = 0;
res += pointQuery(idx, val, l, mid, id << 1 | 0);
res += pointQuery(idx, val, mid, r, id << 1 | 1);
return res;
}
int pointQuery(int idx, int val) {
return pointQuery(idx, val, segL, segR, 1);
}
int segQuery(int st, int en, int val, int l, int r, int id) {
if (st >= r or en <= l) return 0;
if (st <= l and r <= en) return seg[id];
int mid = (l + r) >> 1;
int res = 0;
res += segQuery(st, en, val, l, mid, id << 1 | 0);
res += segQuery(st, en, val, mid, r, id << 1 | 1);
return res;
}
int segQuery(int st, int en, int val) {
return segQuery(st, en, val, segL, segR, 1);
}
};
class SuffixStructure {
string s;
int *sa;
int *pos;
int *lcp;
int tn;
int *par;
int *h;
vector<pair<int, pair<int, int>>> *adj;
SuffixStructure(string str) { s = str + "$"; }
void build_sa() {
int n = ((int)(s).size());
sa = new int[n];
pos = new int[n];
for (int i = 0; i < n; i++) sa[i] = i, pos[i] = s[i];
int gap;
auto cmp = [&](int i, int j) -> bool {
if (pos[i] != pos[j]) return pos[i] < pos[j];
i += gap, j += gap;
return (i < n and j < n) ? pos[i] < pos[j] : i > j;
};
for (gap = 1;; gap <<= 1) {
sort(sa, sa + n, cmp);
int tmp = 0;
pos[sa[0]] = 0;
for (int i = 1; i < n; i++) {
tmp += cmp(sa[i - 1], sa[i]);
pos[sa[i]] = tmp;
}
if (tmp == n - 1) break;
}
}
void build_lcp() {
int n = ((int)(s).size());
lcp = new int[n];
for (int i = 0, cur = 0; i < n; i++) {
if (int j = pos[i] + 1; j < n) {
while (max(i, j) + cur < n and s[i + cur] == s[j + cur]) cur++;
lcp[pos[i] + 1] = cur;
}
if (cur) cur--;
}
}
void build_tree() {
int n = ((int)(s).size());
tn = 2 * n - 1;
adj = new vector<pair<int, pair<int, int>>>[tn];
par = new int[tn];
h = new int[tn];
memset(par, -1, tn * sizeof(int));
memset(h, 0, tn * sizeof(int));
stack<pair<int, pair<int, int>>> stc;
int verts = n;
stc.push(make_pair(verts, pair<int, int>(0, 0)));
verts++;
auto seg_break = [](pair<int, int> seg, int len) {
pair<int, int> le = seg, ri = seg;
le.second = le.first + len;
ri.first = le.second;
return make_pair(le, ri);
};
for (int i = 0; i < n; i++) {
while (h[stc.top().first] > lcp[i]) {
auto top = stc.top();
stc.pop();
if (h[stc.top().first] < lcp[i]) {
pair<pair<int, int>, pair<int, int>> brk =
seg_break(top.second, lcp[i] - h[stc.top().first]);
stc.push(make_pair(verts, brk.first));
h[verts] = lcp[i];
verts++;
top.second = brk.second;
}
par[top.first] = stc.top().first;
adj[stc.top().first].push_back(top);
}
h[i] = n - sa[i];
stc.push(make_pair(i, pair<int, int>(i + h[stc.top().first], n)));
}
while (h[stc.top().first] > 0) {
auto top = stc.top();
stc.pop();
par[top.first] = stc.top().first;
adj[stc.top().first].push_back(top);
}
}
};
long long int Pow(long long int x, long long int y) {
long long int ans = 1, an = x % Mod;
while (y) {
if (y & 1LL) ans = ans * an % Mod;
an = an * an % Mod;
y >>= 1;
}
return ans;
}
long long int Inv(long long int x) { return Pow(x, Mod - 2); }
int solve();
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
while (q--) solve();
return 0;
}
long long int getpw2(long long int base, long long int r, long long int c,
long long int k) {
if (r > c) swap(r, c);
long long int cm1 = (1 << c) - 1;
base ^= base & cm1;
if (base >= k) return 0;
long long int ri = min(1LL << c, k - base) % Mod;
long long int sum = (ri * (ri - 1) / 2) % Mod;
long long int cnt = (ri << r) % Mod;
long long int res = ((base + 1) * cnt % Mod + (sum << r) % Mod) % Mod;
return res;
}
long long int get(int x, int y, long long int k) {
long long int ans = 0;
int xt = x;
for (int i = 0; xt; i++)
if (((xt >> i) & 1LL)) {
xt ^= 1 << i;
int yt = y;
for (int j = 0; yt; j++)
if (((yt >> j) & 1)) {
yt ^= 1 << j;
ans = (ans + getpw2(xt ^ yt, i, j, k)) % Mod;
}
}
return ans;
}
int solve() {
long long int x1, x2, y1, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--, y1--;
long long int ans =
get(x2, y2, k) - get(x1, y2, k) - get(x2, y1, k) + get(x1, y1, k);
ans %= Mod;
ans = (ans + Mod) % Mod;
cout << ans << '\n';
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
long long cnt[32][2][2][2];
long long sum[32][2][2][2];
vector<int> A, B, C;
long long solve(int x, int y, int k) {
if (x < 0 || y < 0 || k < 0) {
return 0;
}
memset(cnt, 0, sizeof(cnt));
memset(sum, 0, sizeof(sum));
A.clear(), B.clear(), C.clear();
for (int i = 0; i <= 30; i++) {
A.push_back(x % 2);
x /= 2;
B.push_back(y % 2);
y /= 2;
C.push_back(k % 2);
k /= 2;
}
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
reverse(C.begin(), C.end());
cnt[0][1][1][1] = 1;
sum[0][1][1][1] = 0;
for (int i = 0; i <= 30; i++) {
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
for (int b1 = 0; b1 < 2; b1++) {
for (int b2 = 0; b2 < 2; b2++) {
int z = b1 ^ b2;
if (a == 1 && A[i] == 0 && b1 == 1) {
continue;
}
if (b == 1 && B[i] == 0 && b2 == 1) {
continue;
}
if (z == 1 && C[i] == 0 && c == 1) {
continue;
}
cnt[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] +=
cnt[i][a][b][c];
cnt[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] %=
MOD;
sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] +=
sum[i][a][b][c];
sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] %=
MOD;
sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] +=
(cnt[i][a][b][c] * z * (1ll << (30 - i))) % MOD;
sum[i + 1][a & (A[i] == b1)][b & (B[i] == b2)][c & (z == C[i])] %=
MOD;
}
}
}
}
}
}
long long ans = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
ans += sum[31][i][j][k];
ans %= MOD;
ans += cnt[31][i][j][k];
ans %= MOD;
}
}
}
return ans;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
int x1, x2, y1, y2, k;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
x1--, x2--, y1--, y2--;
k--;
long long ans = 0;
ans += solve(x1 - 1, y1 - 1, k);
ans %= MOD;
ans -= solve(x1 - 1, y2, k);
while (ans < 0) {
ans += MOD;
}
ans %= MOD;
ans -= solve(x2, y1 - 1, k);
while (ans < 0) {
ans += MOD;
}
ans %= MOD;
ans += solve(x2, y2, k);
ans %= MOD;
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7, lim = 31;
int q, K[33], A[33], B[33], tk, ta, tb, ans;
int f[33][2][2][2], g[33][2][2][2];
inline int rd() {
int res = 0;
char c;
while ((c = getchar()) < '0' || c > '9')
;
while (c >= '0' && c <= '9') res = res * 10 + c - '0', c = getchar();
return res;
}
inline void inc(int &x, int y) { x = (x + y) % mod; }
inline int solve(int n, int m) {
if (n < 0 || m < 0) return 0;
int i, j, a, b, x, y, z, r;
memset(A, 0, sizeof(A)), ta = 0;
for (i = n; i;) A[++ta] = i & 1, i >>= 1;
memset(B, 0, sizeof(B)), tb = 0;
for (i = m; i;) B[++tb] = i & 1, i >>= 1;
memset(f, 0, sizeof(f)), memset(g, 0, sizeof(g));
g[1][0][0][0] = 1;
for (i = 1; i <= lim; i++) {
for (j = 0; j <= 1; j++)
for (a = 0; a <= 1; a++)
for (b = 0; b <= 1; b++)
if ((z = g[i][j][a][b])) {
for (x = 0; x <= 1; x++)
for (y = 0; y <= 1; y++) {
r = x ^ y;
inc(f[i + 1][j + r > K[i]][a + x > A[i]][b + y > B[i]],
((1LL << (i - 1)) * z * r + f[i][j][a][b]) % mod);
inc(g[i + 1][j + r > K[i]][a + x > A[i]][b + y > B[i]], z);
}
}
}
return (f[lim + 1][0][0][0] + g[lim + 1][0][0][0]) % mod;
}
int main() {
q = rd();
int x1, y1, x2, y2, k;
while (q--) {
x1 = rd() - 1, y1 = rd() - 1, x2 = rd() - 1, y2 = rd() - 1, k = rd() - 1;
memset(K, 0, sizeof(K));
tk = 0;
while (k) K[++tk] = k & 1, k >>= 1;
ans = (solve(x2, y2) + solve(x1 - 1, y1 - 1) - solve(x1 - 1, y2) -
solve(x2, y1 - 1)) %
mod;
printf("%d\n", (ans + mod) % mod);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const double eps = 1e-9;
const long long MOD = 1e9 + 7;
map<int, map<int, map<int, pair<long long, long long>>>> dp;
pair<long long, long long> calc(int x, int y, int k) {
if (x <= 0 || y <= 0 || k <= 0) return make_pair(0, 0);
if (k == 1) return make_pair(min(x, y), min(x, y));
if (dp.find(x) != dp.end()) {
if (dp[x].find(y) != dp[x].end()) {
if (dp[x][y].find(k) != dp[x][y].end()) {
return dp[x][y][k];
}
}
}
long long resn = 0;
long long ress = 0;
int n = 1;
while (k > n * 2ll) n *= 2;
if (n * 2 == k) {
if (x == 2 * n && y == 2 * n) {
n *= 2;
ress = n * (long long)(n + 1);
ress %= MOD;
ress *= (MOD + 1) / 2;
ress %= MOD;
ress *= n;
ress %= MOD;
resn = n * (long long)n;
resn %= MOD;
return dp[x][y][k] = make_pair(resn, ress);
}
}
while (n * 2ll < x || n * 2ll < y) n *= 2;
auto res = calc(min(x, n), min(y, n), min(n, k));
resn += res.first;
ress += res.second;
res = calc(x - n, min(y, n), k - n);
resn += res.first;
ress += res.first * (long long)n + res.second;
res = calc(min(x, n), y - n, k - n);
resn += res.first;
ress += res.first * (long long)n + res.second;
res = calc(x - n, y - n, min(n, k));
resn += res.first;
ress += res.second;
resn %= MOD;
ress %= MOD;
return dp[x][y][k] = make_pair(resn, ress);
}
int main() {
int Q;
scanf("%d", &Q);
for (int q = (0); q < (Q); q++) {
int x1, y1, x2, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
--x1;
--y1;
long long res = 0;
auto tmp = calc(x1, y1, k);
res += tmp.second;
tmp = calc(x2, y2, k);
res += tmp.second;
tmp = calc(x1, y2, k);
res -= tmp.second;
tmp = calc(x2, y1, k);
res -= tmp.second;
res %= MOD;
res = (res + MOD) % MOD;
int ires = res;
printf("%d\n", ires);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
long long a[3], b[3], k, n, q;
pair<long long, long long> memo[40][4][4][2];
pair<long long, long long> dp(long long i, long long okt, long long okd,
long long okk) {
if (i < 0) return {1ll, 0};
if (memo[i][okt][okd][okk].first != -1ll) return memo[i][okt][okd][okk];
pair<long long, long long> res = {0, 0};
for (long long ad = 0; ad <= 3; ad++) {
long long newokt = okt, newokd = okd;
for (long long j = 0; j <= 1ll; j++) {
long long curt = (okt & (1ll << j)) != 0;
long long id = (ad & (1ll << j)) != 0;
long long curd = (okd & (1ll << j)) != 0;
long long cura = (a[j] & (1ll << i)) != 0;
long long curb = (b[j] & (1ll << i)) != 0;
if (curt == 0 && id > curb) {
newokt = -1ll;
break;
}
if (curd == 0 && id < cura) {
newokd = -1ll;
break;
}
newokt |= ((id < curb) << j);
newokd |= ((id > cura) << j);
}
if (newokt == -1ll || newokd == -1ll) continue;
long long id = (ad & 1ll) ^ ((ad & (1ll << 1ll)) != 0);
long long curk = (k & (1ll << i)) != 0;
if (okk == 0 && id > curk) continue;
long long newokk = okk | (id < curk);
pair<long long, long long> tmp = dp(i - 1ll, newokt, newokd, newokk);
res.first += tmp.first;
res.first %= M;
res.second += tmp.second + (id << i) % M * tmp.first % M;
res.second %= M;
}
return memo[i][okt][okd][okk] = res;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> q;
while (q) {
for (long long i = 0; i < 40; i++) {
for (long long j = 0; j < 4; j++) {
for (long long z = 0; z < 4; z++) {
memo[i][j][z][0] = memo[i][j][z][1ll] = {-1ll, -1ll};
}
}
}
q--;
cin >> a[0] >> a[1ll] >> b[0] >> b[1ll] >> k;
a[0]--;
a[1ll]--;
b[0]--;
b[1ll]--;
k--;
n = max(log2(b[0]), log2(b[1ll]));
pair<long long, long long> sol = dp(30, 0, 0, 0);
cout << ((sol.first + sol.second) % M) << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int q;
long long dp[32][2][2][2];
long long sum[32][2][2][2];
void add(long long &x, long long y) {
x += y;
if (x >= 1000000007LL) x -= 1000000007LL;
}
void sub(long long &x, long long y) {
x -= y;
if (x < 0) x += 1000000007LL;
}
long long mul(long long x, long long y) { return x * y % 1000000007LL; }
long long solve(long long x, long long y, long long k) {
if (x < 0LL || y < 0LL || k < 0LL) return 0;
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(dp));
vector<int> A, B, C;
for (int j = 0; j < 31; j++) {
A.push_back(x % 2LL);
B.push_back(y % 2LL);
C.push_back(k % 2LL);
x /= 2LL;
y /= 2LL;
k /= 2LL;
}
reverse(A.begin(), A.end());
reverse(B.begin(), B.end());
reverse(C.begin(), C.end());
dp[0][1][1][1] = 1;
for (int i = 0; i < 31; i++) {
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
for (int x = 0; x < 2; x++) {
for (int y = 0; y < 2; y++) {
int z = x ^ y;
if (a == 1 && A[i] == 0 && x == 1) continue;
if (b == 1 && B[i] == 0 && y == 1) continue;
if (c == 1 && C[i] == 0 && z == 1) continue;
add(dp[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
dp[i][a][b][c]);
add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
sum[i][a][b][c]);
add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)],
mul(z << (30 - i), dp[i][a][b][c]));
}
}
}
}
}
}
long long ans = 0;
for (int a = 0; a < 2; a++) {
for (int b = 0; b < 2; b++) {
for (int c = 0; c < 2; c++) {
add(ans, sum[31][a][b][c]);
add(ans, dp[31][a][b][c]);
}
}
}
return ans;
}
int main(void) {
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int x1, y1, x2, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
x1--;
y1--;
x2--;
y2--;
k--;
long long ans = 0;
ans += solve(x2, y2, k);
ans += 1000000007LL - solve(x2, y1 - 1, k);
ans += 1000000007LL - solve(x1 - 1, y2, k);
ans += solve(x1 - 1, y1 - 1, k);
ans %= 1000000007LL;
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
string itos(int x) {
stringstream ss;
ss << x;
return ss.str();
}
int dp[32][2][2][2], sum[32][2][2][2];
int f(int x, int y, int k) {
if (min(x, min(y, k)) < 0) return 0;
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
bitset<31> vx(x), vy(y), vk(k);
dp[31][1][1][1] = 1;
for (int i = 30; i >= 0; i--)
for (int a = 0; a < 2; a++)
for (int b = 0; b < 2; b++)
for (int c = 0; c < 2; c++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++) {
long long z = x ^ y;
if (x > vx[i] && a) continue;
if (y > vy[i] && b) continue;
if (z > vk[i] && c) continue;
(dp[i][a & (vx[i] == x)][b & (vy[i] == y)][c & (vk[i] == z)] +=
dp[i + 1][a][b][c]) %= 1000000007;
(sum[i][a & (vx[i] == x)][b & (vy[i] == y)][c & (vk[i] == z)] +=
sum[i + 1][a][b][c]) %= 1000000007;
(sum[i][a & (vx[i] == x)][b & (vy[i] == y)][c & (vk[i] == z)] +=
((z << i) * dp[i + 1][a][b][c]) % 1000000007) %= 1000000007;
}
int res = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++) {
(res += dp[0][i][j][k]) %= 1000000007;
(res += sum[0][i][j][k]) %= 1000000007;
}
return res;
}
int q;
int main() {
ios_base::sync_with_stdio(false);
cin >> q;
while (q--) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
int res = 0;
(res += f(--x2, --y2, --k)) %= 1000000007;
(res -= f(--x1 - 1, y2, k)) %= 1000000007;
(res -= f(x2, --y1 - 1, k)) %= 1000000007;
(res += f(x1 - 1, y1 - 1, k)) %= 1000000007;
if (res < 0) res += 1000000007;
cout << res << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void Add(int &x, int y) {
x += y;
if (x >= 1000000007) x -= 1000000007;
if (x < 0) x += 1000000007;
}
int lst1[40], lst2[40], lst3[40], sl;
int sum[40][2][2][2], cnt[40][2][2][2];
inline pair<int, int> Ask(int l, int s1, int s2, int s3) {
if (l == 0) return make_pair(0, 1);
if (cnt[l][s1][s2][s3] != -1)
return make_pair(sum[l][s1][s2][s3], cnt[l][s1][s2][s3]);
int t1 = s1 ? lst1[l] : 1;
int t2 = s2 ? lst2[l] : 1;
int t3 = s3 ? lst3[l] : 1;
sum[l][s1][s2][s3] = cnt[l][s1][s2][s3] = 0;
for (int i = 0; i <= t1; i++)
for (int j = 0; j <= t2; j++)
for (int k = 0; k <= t3; k++)
if ((i ^ j) == k) {
pair<int, int> tmp =
Ask(l - 1, s1 & (i == t1), s2 & (j == t2), s3 & (k == t3));
if (k == 1)
Add(sum[l][s1][s2][s3],
1ll * (1ll << (l - 1)) * tmp.second % 1000000007);
Add(sum[l][s1][s2][s3], tmp.first);
Add(cnt[l][s1][s2][s3], tmp.second);
}
return make_pair(sum[l][s1][s2][s3], cnt[l][s1][s2][s3]);
}
inline int calc(int x, int y, int k) {
memset(lst1, 0, sizeof lst1);
memset(lst2, 0, sizeof lst2);
memset(lst3, 0, sizeof lst3);
memset(sum, -1, sizeof sum);
memset(cnt, -1, sizeof cnt);
if (!x || !y) return 0;
x--;
y--;
k--;
while (x) lst1[++lst1[0]] = x & 1, x /= 2;
while (y) lst2[++lst2[0]] = y & 1, y /= 2;
while (k) lst3[++lst3[0]] = k & 1, k /= 2;
sl = max(max(lst1[0], lst2[0]), lst3[0]);
pair<int, int> rec = Ask(sl, 1, 1, 1);
return (1ll * rec.first + 1ll * rec.second) % 1000000007;
}
int main() {
int n = read();
for (int i = 1; i <= n; i++) {
int x1 = read(), FUCK_THE_Y1 = read(), x2 = read(), y2 = read(), k = read();
int Ret = calc(x1 - 1, FUCK_THE_Y1 - 1, k);
Add(Ret, -calc(x1 - 1, y2, k));
Add(Ret, -calc(x2, FUCK_THE_Y1 - 1, k));
Add(Ret, calc(x2, y2, k));
cout << Ret << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int q, x_1, y_1, x_2, y_2, k, len, a[35], b[35], c[35];
pair<int, int> dp[35][2][2][2];
int Add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int Minus(int a, int b) { return a < b ? a - b + mod : a - b; }
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
return make_pair(Add(a.first, b.first), Add(a.second, b.second));
}
pair<int, int> dfs(int now, int f1, int f2, int f3) {
if (now == -1) return make_pair(1, 0);
if (~dp[now][f1][f2][f3].first) return dp[now][f1][f2][f3];
pair<int, int> ret = make_pair(0, 0);
for (int i = 0; i <= (f1 ? a[now] : 1); i++)
for (int j = 0; j <= (f2 ? b[now] : 1); j++)
if ((i ^ j) <= (f3 ? c[now] : 1)) {
pair<int, int> tmp = dfs(now - 1, f1 && i == a[now], f2 && j == b[now],
f3 && (i ^ j) == c[now]);
ret = ret + tmp;
if (i ^ j) ret.second = (ret.second + (1ll << now) * tmp.first) % mod;
}
dp[now][f1][f2][f3] = ret;
return ret;
}
int calc(int x, int y, int k) {
if (x < 0 || y < 0 || k < 0) return 0;
memset(dp, -1, sizeof(dp));
len = 0;
while (x || y || k) {
a[len] = (x & 1);
b[len] = (y & 1);
c[len++] = (k & 1);
x >>= 1;
y >>= 1;
k >>= 1;
}
pair<int, int> ret = dfs(len - 1, 1, 1, 1);
return Add(ret.first, ret.second);
}
int main() {
scanf("%d", &q);
while (q--) {
scanf("%d%d%d%d%d", &x_1, &y_1, &x_2, &y_2, &k);
printf(
"%d\n",
Minus(
Add(calc(x_2 - 1, y_2 - 1, k - 1), calc(x_1 - 2, y_1 - 2, k - 1)),
Add(calc(x_1 - 2, y_2 - 1, k - 1), calc(x_2 - 1, y_1 - 2, k - 1))));
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline void Rd(int &res) {
char c;
res = 0;
while (c = getchar(), c < '0')
;
do {
res = (res << 1) + (res << 3) + (c ^ 48);
} while (c = getchar(), c >= '0');
}
const int N = 32;
const int M = 35;
const int P = (int)1e9 + 7;
int q;
int dp1[M][2][2][2], dp2[M][2][2][2], X[M], Y[M], K[M];
inline void Add(int &a, int b) { a = (a + b) % P; }
int solve(int x, int y, int k) {
if (x < 0 || y < 0) return 0;
memset(dp1, 0, sizeof(dp1));
memset(dp2, 0, sizeof(dp2));
for (int i = 1; i <= N; i++) {
X[i] = (x & 1), Y[i] = (y & 1), K[i] = (k & 1);
x >>= 1, y >>= 1, k >>= 1;
}
reverse(X + 1, X + N + 1), reverse(Y + 1, Y + N + 1),
reverse(K + 1, K + N + 1);
dp1[1][1][1][1] = 1;
for (int i = 1; i < N; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
for (int t = 0; t < 2; t++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++) {
if (!dp1[i][j][k][t]) continue;
int z = x ^ y;
if (j && (x > X[i + 1])) continue;
if (k && (y > Y[i + 1])) continue;
if (t && (z > K[i + 1])) continue;
Add(dp1[i + 1][j & (x == X[i + 1])][k & (y == Y[i + 1])]
[t & (z == K[i + 1])],
dp1[i][j][k][t]);
Add(dp2[i + 1][j & (x == X[i + 1])][k & (y == Y[i + 1])]
[t & (z == K[i + 1])],
dp2[i][j][k][t]);
Add(dp2[i + 1][j & (x == X[i + 1])][k & (y == Y[i + 1])]
[t & (z == K[i + 1])],
dp1[i][j][k][t] * ((long long)z << (N - 1 - i)) % P);
}
int res = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int t = 0; t < 2; t++)
Add(res, (dp1[N][i][j][t] + dp2[N][i][j][t]) % P);
return res;
}
int main() {
Rd(q);
while (q--) {
int x1, y1, x2, y2, k, ans = 0;
Rd(x1), Rd(y1), Rd(x2), Rd(y2), Rd(k);
x1--, y1--, x2--, y2--, k--;
ans = (ans + solve(x2, y2, k)) % P;
ans = (ans - solve(x1 - 1, y2, k) + P) % P;
ans = (ans - solve(x2, y1 - 1, k) + P) % P;
ans = (ans + solve(x1 - 1, y1 - 1, k)) % P;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long readi() {
long long input = 0;
char c = ' ';
while (c < '-') {
c = getchar();
}
bool negative = false;
if (c == '-') {
negative = true;
c = getchar();
}
while (c >= '0') {
input = 10 * input + (c - '0');
c = getchar();
}
if (negative) {
input = -input;
}
return input;
}
string reads() {
string input = "";
char c = ' ';
while (c <= ' ') {
c = getchar();
}
while (c > ' ') {
input += c;
c = getchar();
}
return input;
}
void printi(long long output) {
if (output == 0) {
putchar('0');
return;
}
if (output < 0) {
putchar('-');
output = -output;
}
vector<char> vout;
while (output) {
vout.push_back((output % 10) + '0');
output /= 10;
}
for (int i = vout.size() - 1; i >= 0; i--) {
putchar(vout[i]);
}
return;
}
void prints(string output) {
for (int i = 0; i < output.length(); i++) {
putchar(output[i]);
}
return;
}
int Q;
long long K;
long long X, Y;
long long dp[40][2][2][2], cnt[40][2][2][2];
long long ans;
void reset() {
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int m = 0; m < 2; m++) {
cnt[i][j][k][m] = -1;
dp[i][j][k][m] = -1;
}
}
}
}
}
long long num(int id, bool x, bool y, bool k) {
if (id < 0) {
return 1;
}
if (cnt[id][x][y][k] != -1) {
return cnt[id][x][y][k];
}
cnt[id][x][y][k] = 0;
bool bx = X & (1ll << id);
bool by = Y & (1ll << id);
bool bk = K & (1ll << id);
for (int i = 0; i < 2; i++) {
if (bx == 0 && x == 1 && i == 1) {
continue;
}
for (int j = 0; j < 2; j++) {
if (by == 0 && y == 1 && j == 1) {
continue;
}
int m = i ^ j;
if (bk == 0 && k == 1 && m == 1) {
continue;
}
bool rx, ry, rk;
if (x == 1 && i == bx) {
rx = true;
} else {
rx = false;
}
if (y == 1 && j == by) {
ry = true;
} else {
ry = false;
}
if (k == 1 && m == bk) {
rk = true;
} else {
rk = false;
}
cnt[id][x][y][k] += num(id - 1, rx, ry, rk);
cnt[id][x][y][k] %= 1000000007;
}
}
return cnt[id][x][y][k];
}
long long query(int id, bool x, bool y, bool k) {
if (id < 0) {
return 0;
}
if (dp[id][x][y][k] != -1) {
return dp[id][x][y][k];
}
dp[id][x][y][k] = 0;
bool bx = X & (1ll << id);
bool by = Y & (1ll << id);
bool bk = K & (1ll << id);
for (int i = 0; i < 2; i++) {
if (bx == 0 && x == 1 && i == 1) {
continue;
}
for (int j = 0; j < 2; j++) {
if (by == 0 && y == 1 && j == 1) {
continue;
}
int m = i ^ j;
if (bk == 0 && k == 1 && m == 1) {
continue;
}
bool rx, ry, rk;
if (x == 1 && i == bx) {
rx = true;
} else {
rx = false;
}
if (y == 1 && j == by) {
ry = true;
} else {
ry = false;
}
if (k == 1 && m == bk) {
rk = true;
} else {
rk = false;
}
dp[id][x][y][k] += query(id - 1, rx, ry, rk);
dp[id][x][y][k] +=
((1ll << id) % 1000000007) * m * num(id - 1, rx, ry, rk);
dp[id][x][y][k] %= 1000000007;
}
}
return dp[id][x][y][k];
}
int32_t main() {
ios_base::sync_with_stdio(false);
if (fopen("cf809c.in", "r")) {
freopen("cf809c.in", "r", stdin);
freopen("cf809c.out", "w", stdout);
}
cin >> Q;
for (int i = 0; i < Q; i++) {
ans = 0ll;
long long a, b, c, d;
cin >> a >> b >> c >> d >> K;
K--;
a--;
b--;
c--;
d--;
reset();
X = c;
Y = d;
ans += query(35, 1, 1, 1);
ans += num(35, 1, 1, 1);
reset();
X = a - 1;
Y = d;
if (X >= 0 && Y >= 0) {
ans -= query(35, 1, 1, 1);
ans -= num(35, 1, 1, 1);
}
reset();
X = c;
Y = b - 1;
if (X >= 0 && Y >= 0) {
ans -= query(35, 1, 1, 1);
ans -= num(35, 1, 1, 1);
}
reset();
X = a - 1;
Y = b - 1;
if (X >= 0 && Y >= 0) {
ans += query(35, 1, 1, 1);
ans += num(35, 1, 1, 1);
}
ans %= 1000000007;
ans += 1000000007;
ans %= 1000000007;
cout << ans << '\n';
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int a[1100][1100], r[258][258][258], s[258][258][258];
map<pair<int, pair<int, int> >, pair<int, int> > mp;
void operator+=(pair<int, int> &p1, pair<int, int> p2) {
p1.first += p2.first;
if (p1.first >= 1000000007) p1.first -= 1000000007;
p1.second += p2.second;
if (p1.second >= 1000000007) p1.second -= 1000000007;
}
pair<int, int> C(int x, int y, int up, int now) {
if (x <= 0 || y <= 0 || up <= 0) return (pair<int, int>){0, 0};
if (x > y) swap(x, y);
for (; y <= (1 << now) && now; now--)
;
up = min(up, (1 << now) << 1);
if (y <= 256)
return (pair<int, int>){s[min(up, 256)][x][y], r[min(up, 256)][x][y]};
if (mp.count((pair<int, pair<int, int> >){up, (pair<int, int>){x, y}}))
return mp[(pair<int, pair<int, int> >){up, (pair<int, int>){x, y}}];
pair<int, int> res = (pair<int, int>){0, 0}, t;
t = C(min(x, (1 << now)), min(y, (1 << now)), up, now);
res += t;
t = C(x - (1 << now), min(y, (1 << now)), up - (1 << now), now);
t.first = (t.first + (long long)t.second * (1 << now)) % 1000000007;
res += t;
t = C(min(x, (1 << now)), y - (1 << now), up - (1 << now), now);
t.first = (t.first + (long long)t.second * (1 << now)) % 1000000007;
res += t;
t = C(x - (1 << now), y - (1 << now), up, now);
res += t;
mp[(pair<int, pair<int, int> >){up, (pair<int, int>){x, y}}] = res;
return res;
}
void mcp(int sz, int x, int y, int ex) {
for (int i = 1; i <= sz; i++)
for (int j = 1; j <= sz; j++) a[x + i][y + j] = a[i][j] + ex;
}
int main() {
a[1][1] = a[2][2] = 1;
a[1][2] = a[2][1] = 2;
for (int i = 1; i < 10; i++) {
mcp(1 << i, 1 << i, 0, 1 << i);
mcp(1 << i, 0, 1 << i, 1 << i);
mcp(1 << i, 1 << i, 1 << i, 0);
}
memset(s, 0, sizeof s);
for (int up = 1; up <= 256; up++)
for (int i = 1; i <= 256; i++)
for (int j = 1; j <= 256; j++) {
s[up][i][j] = s[up][i - 1][j] + s[up][i][j - 1] - s[up][i - 1][j - 1] +
(a[i][j] > up ? 0 : a[i][j]);
r[up][i][j] = r[up][i - 1][j] + r[up][i][j - 1] - r[up][i - 1][j - 1] +
(a[i][j] > up ? 0 : 1);
}
int T, x, xx, y, yy, k;
for (scanf("%d", &T); T--;) {
scanf("%d%d%d%d%d", &x, &y, &xx, &yy, &k);
printf("%I64d\n", ((long long)C(xx, yy, k, 30).first -
C(x - 1, yy, k, 30).first - C(xx, y - 1, k, 30).first +
C(x - 1, y - 1, k, 30).first + 1000000007 * 233ll) %
1000000007);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 35;
const int mod = 1e9 + 7;
int a[N], b[N], c[N];
pair<int, int> dp[N][2][2][2];
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
pair<int, int> operator+(pair<int, int> x, pair<int, int> y) {
return make_pair((x.first + y.first) % mod, (x.second + y.second) % mod);
}
pair<int, int> dfs(int pos, int xl, int yl, int xyl) {
if (pos < 0) return pair<int, int>(1, 0);
if (~dp[pos][xl][yl][xyl].first) return dp[pos][xl][yl][xyl];
pair<int, int> &now = dp[pos][xl][yl][xyl];
now.first = now.second = 0;
for (register int i = 0, _r = xl ? a[pos] : 1; i <= _r; ++i)
for (register int j = 0, _r = yl ? b[pos] : 1; j <= _r; ++j)
if ((i ^ j) <= (xyl ? c[pos] : 1)) {
pair<int, int> p = dfs(pos - 1, xl & (i == a[pos]), yl & (j == b[pos]),
xyl & ((i ^ j) == c[pos]));
now = now + p;
if (i ^ j) now.second = (now.second + (1LL << pos) * p.first) % mod;
}
return now;
}
long long Solve(int x, int y, int k) {
if (x < 0 || y < 0 || k < 0) return 0;
int len = 0;
memset(dp, -1, sizeof dp);
while (x || y || k) {
a[len] = x & 1, b[len] = y & 1, c[len] = k & 1;
x >>= 1, y >>= 1, k >>= 1;
len++;
}
pair<int, int> p = dfs(len - 1, 1, 1, 1);
return p.first + p.second;
}
int main() {
int t = read();
while (t--) {
int l1 = read(), l2 = read(), r1 = read(), r2 = read(), k = read();
l1 -= 2, l2 -= 2, r1 -= 1, r2 -= 1, k -= 1;
printf("%d\n", ((Solve(r1, r2, k) + Solve(l1, l2, k) - Solve(l1, r2, k) -
Solve(r1, l2, k)) %
mod +
mod) %
mod);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const long long MOD = 1e9 + 7;
pair<long long, long long> dp[33][2][2][2];
int vis[33][2][2][2], vid;
pair<long long, long long> call(long long pos, bool t1, bool t2, bool t3,
long long x, long long y, long long k) {
if (pos == -1) {
return make_pair(1, 0);
}
if (vis[pos][t1][t2][t3] == vid) return dp[pos][t1][t2][t3];
if (vis[pos][t1][t2][t3] > 0 and t1 == 0 and t2 == 0 and t3 == 0)
return dp[pos][t1][t2][t3];
vis[pos][t1][t2][t3] = vid;
pair<long long, long long> ret = make_pair(0, 0);
bool h1 = (t1 ? (x & (1LL << pos)) : 1);
bool h2 = (t2 ? (y & (1LL << pos)) : 1);
bool h3 = (t3 ? (k & (1LL << pos)) : 1);
for (long long f = 0; f <= h1; f++) {
for (long long s = 0; s <= h2; s++) {
if ((f ^ s) > h3) continue;
bool newt1 = t1 & (f == h1);
bool newt2 = t2 & (s == h2);
bool newt3 = t3 & ((f ^ s) == h3);
pair<long long, long long> p =
call(pos - 1, newt1, newt2, newt3, x, y, k);
ret.first += p.first;
ret.second += ((f ^ s) * (1 << pos) * p.first) + p.second;
ret.second %= MOD;
}
}
ret.first %= MOD;
return dp[pos][t1][t2][t3] = ret;
}
long long solve(long long x, long long y, long long k) {
if (x == 0 || y == 0) return 0;
vid++;
x--;
y--;
k--;
pair<long long, long long> ans = call(30, 1, 1, 1, x, y, k);
return (ans.second + ans.first) % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
long long q;
cin >> q;
while (q--) {
long long x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
long long ans = solve(x2, y2, k) - solve(x1 - 1, y2, k) -
solve(x2, y1 - 1, k) + solve(x1 - 1, y1 - 1, k);
ans = (ans % MOD + MOD) % MOD;
cout << ans << "\n";
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
const int N = 100;
const int moder = 1e9 + 7;
int test;
int powermod(int a, int exp) {
int ret = 1;
for (; exp; exp >>= 1) {
if (exp & 1) {
ret = 1ll * ret * a % moder;
}
a = 1ll * a * a % moder;
}
return ret;
}
int sum(int x) { return 1ll * x * (x + 1) / 2 % moder; }
int solve(int x, int y, int k) {
int ret = 0;
for (int x1 = x, lowx; x1; x1 -= lowx) {
lowx = ((x1) & (-x1));
for (int y1 = y, lowy; y1; y1 -= lowy) {
lowy = ((y1) & (-y1));
int max = std::max(lowx, lowy), min = std::min(lowx, lowy);
int z1 = lowx ^ lowy ^ x1 ^ y1;
z1 = z1 / max * max;
int z2 = z1 + max - 1;
if (k < z1) {
continue;
}
int kk = std::min(z2, k);
ret =
(ret + 1ll * (min % moder) * (sum(kk) - sum(z1 - 1) + moder)) % moder;
ret = (ret + 1ll * (min % moder) * (kk - z1 + 1 + moder)) % moder;
}
}
return ret;
}
int main() {
scanf("%d", &test);
while (test--) {
int x1, x2, y1, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
--k;
int ans = (solve(x2, y2, k) + solve(x1 - 1, y1 - 1, k)) % moder;
ans = (ans - solve(x2, y1 - 1, k) + moder) % moder;
ans = (ans - solve(x1 - 1, y2, k) + moder) % moder;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline long long rd() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
if (ch == '-') {
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
return x * f;
}
inline void rt(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10)
rt(x / 10), putchar(x % 10 + '0');
else
putchar(x + '0');
}
const int p = 1000000007;
int k;
void upd(int &x, long long y) { x = (x + y) % p; }
int sum(int l, int r) {
if (l > r) return 0;
return ((long long)(l + r) * (r - l + 1) / 2) % p;
}
int solve(int x, int y) {
int ret = 0, mn = 1;
for (int z = (1 << 29); z && x && y; z >>= 1) {
if (mn > k) break;
if (z > x && z > y) continue;
if (x >= z && y >= z) {
upd(ret, (long long)z * sum(mn, min(k, mn + z - 1)));
upd(ret,
(long long)(x - z + y - z) * sum(mn + z, min(k, mn + 2 * z - 1)));
x -= z, y -= z;
} else {
upd(ret, (long long)min(x, y) * sum(mn, min(k, mn + z - 1)));
mn += z, x -= x & z, y -= y & z;
}
}
return ret;
}
int main() {
for (int q = rd(); q--;) {
int x1 = rd(), y1 = rd(), x2 = rd(), y2 = rd();
k = rd();
rt(((long long)solve(x2, y2) + p - solve(x1 - 1, y2) + p -
solve(x2, y1 - 1) + solve(x1 - 1, y1 - 1)) %
p),
putchar('\n');
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename TP>
inline bool rd(TP& r) {
r = 0;
char tmp = getchar();
while (tmp < '0' || tmp > '9') {
if (tmp == EOF) return 0;
tmp = getchar();
}
while ('0' <= tmp && tmp <= '9') {
r = (r << 3) + (r << 1) + tmp - '0';
tmp = getchar();
}
return 1;
}
long long ni2 = 500000004;
long long sm(long long a, long long y, long long kk, long long add) {
add = add % 1000000007;
long long lth = 1LL << a;
kk = max(kk, 0LL);
lth = min(lth, kk);
long long re =
(((lth + 1) * lth % 1000000007) * ni2 % 1000000007) * y % 1000000007;
return (re + (lth * y % 1000000007) * add % 1000000007) % 1000000007;
}
long long ask(long long x, long long y, long long k, long long add) {
add = add % 1000000007;
if (x == 0 || y == 0 || k == 0) return 0;
if (x < y) swap(x, y);
long long a = floor(log(x) / log(2));
long long re = 0;
if ((1LL << a) >= y) {
re += sm(a, y, k, add);
re += ask(x - (1LL << a), y, max(k - (1LL << a), 0LL),
((1LL << a) % 1000000007 + add) % 1000000007);
re = re % 1000000007;
return re;
} else {
re += sm(a, 1LL << a, k, add);
re = (re + sm(a, y - (1LL << a), k - (1LL << a),
((1LL << a) % 1000000007 + add) % 1000000007)) %
1000000007;
re = (re + sm(a, x - (1LL << a), k - (1LL << a),
((1LL << a) % 1000000007 + add) % 1000000007)) %
1000000007;
re = (re + ask(x - (1LL << a), y - (1LL << a), k, add)) % 1000000007;
return re;
}
}
int main() {
int q;
long long x1, x2, y1, y2;
long long k;
rd(q);
long long ans;
for (int i = 1; i <= q; i++) {
rd(x1), rd(y1), rd(x2), rd(y2);
rd(k);
ans = 0;
ans = (((ask(x2, y2, k, 0) - ask(x1 - 1, y2, k, 0) + 1000000007) %
1000000007 -
ask(x2, y1 - 1, k, 0) + 1000000007) %
1000000007 +
ask(x1 - 1, y1 - 1, k, 0)) %
1000000007;
printf("%I64d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)(1e9 + 7);
long long INV2;
long long mul(long long a, long long b) { return (a * b) % MOD; }
long long pw(long long a, long long b) {
long long ans = 1;
for (; b; b >>= 1, a = mul(a, a)) {
if (b & 1) {
ans = mul(ans, a);
}
}
return ans;
}
struct Node {
int x, y, k;
long long v;
Node() {}
Node(int _x, int _y, int _k, int _v) : x(_x), y(_y), k(_k), v(_v) {}
bool operator<(const Node& rhs) const {
if (x != rhs.x) return x < rhs.x;
if (y != rhs.y) return y < rhs.y;
if (k != rhs.k) return k < rhs.k;
return v < rhs.v;
}
};
map<Node, long long> Cache;
long long calc(int x, int y, long long k, long long v) {
if (x <= 0 || y <= 0 || k <= 0) {
return 0;
}
Node hash_val(x, y, k, v);
if (Cache.count(hash_val)) return Cache[hash_val];
int d = 0;
for (; (1LL << d) < max(x, y); ++d) {
}
long long f = (1LL << d);
if ((x == f || y == f) && f <= k) {
if (y == f) swap(x, y);
long long a = mul(y, mul(f, mul(f + 1, INV2)));
long long b = mul(y, mul(f, v));
return Cache[hash_val] = (a + b) % MOD;
}
long long ans = 0;
long long m = (1LL << (d - 1));
ans = (ans + calc(min(m, 1LL * x), min(m, 1LL * y), k, v)) % MOD;
ans = (ans + calc(x - m, min(m, 1LL * y), k - m, (v + m) % MOD)) % MOD;
ans = (ans + calc(min(m, 1LL * x), y - m, k - m, (v + m) % MOD)) % MOD;
ans = (ans + calc(x - m, y - m, k, v)) % MOD;
return Cache[hash_val] = ans;
}
int main() {
INV2 = pw(2, MOD - 2);
int q, x1, y1, x2, y2;
long long k;
cin >> q;
while (q--) {
scanf("%d%d%d%d%lld", &x1, &y1, &x2, &y2, &k);
long long ans = calc(x2, y2, k, 0);
ans = (ans + MOD - calc(x2, y1 - 1, k, 0)) % MOD;
ans = (ans + MOD - calc(x1 - 1, y2, k, 0)) % MOD;
ans = (ans + calc(x1 - 1, y1 - 1, k, 0)) % MOD;
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007, C = 31;
int f[C + 9][8], g[C + 9][8];
int Query(int x, int y, int z) {
if (!x || !y) return 0;
for (int i = 0; i < C; ++i) {
for (int s = 0; s < 8; ++s) {
f[i][s] = g[i][s] = 0;
int a = s & 1 ? x >> i & 1 : 2, b = s >> 1 & 1 ? y >> i & 1 : 2,
c = s >> 2 & 1 ? z >> i & 1 : 2;
for (int u = 0; u <= a && u < 2; ++u)
for (int v = 0; v <= b && v < 2; ++v) {
if ((u ^ v) > c) continue;
int t = u == a | (v == b) << 1 | ((u ^ v) == c) << 2,
cnt = i ? f[i - 1][t] : !t;
f[i][s] = (f[i][s] + cnt) % mod;
if (u ^ v) g[i][s] = (g[i][s] + 1LL * cnt * (1 << i)) % mod;
if (i) g[i][s] = (g[i][s] + g[i - 1][t]) % mod;
}
}
}
return (f[C - 1][7] + g[C - 1][7]) % mod;
}
int cq;
void into() {
scanf("%d", &cq);
for (; cq--;) {
int x0, x1, y0, y1, lim;
scanf("%d%d%d%d%d", &x0, &y0, &x1, &y1, &lim);
printf("%d\n", ((Query(x1, y1, lim) - Query(x0 - 1, y1, lim) -
Query(x1, y0 - 1, lim) + Query(x0 - 1, y0 - 1, lim)) %
mod +
mod) %
mod);
}
}
void work() {}
void outo() {}
int main() {
into();
work();
outo();
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
long long memo[33][2][2][2];
long long sum[33][2][2][2];
long long dp(long long xx, long long yy, long long kk) {
memset(memo, 0, sizeof memo);
memset(sum, 0, sizeof sum);
memo[0][1][1][1] = 1;
sum[0][1][1][1] = 0;
for (long long pst = 0; pst <= 31; pst++) {
for (long long flx = 0; flx <= 1; flx++) {
for (long long fly = 0; fly <= 1; fly++) {
for (long long flk = 0; flk <= 1; flk++) {
for (long long clx = 0; clx <= 1; clx++) {
for (long long cly = 0; cly <= 1; cly++) {
long long r = clx ^ cly;
long long x = xx >> (31 - pst);
long long y = yy >> (31 - pst);
long long k = kk >> (31 - pst);
x &= 1;
y &= 1;
k &= 1;
if (r && !k && flk) continue;
if (clx && !x && flx) continue;
if (cly && !y && fly) continue;
memo[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] += memo[pst][flx][fly][flk];
if (memo[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] > 1000000007)
memo[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] -= 1000000007;
sum[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] += sum[pst][flx][fly][flk];
if (sum[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] > 1000000007)
sum[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] -= 1000000007;
sum[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] +=
((r << (31 - pst)) * memo[pst][flx][fly][flk]) % 1000000007;
if (sum[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] > 1000000007)
sum[pst + 1][flx & ~(x ^ clx)][fly & ~(y ^ cly)]
[flk & ~(k ^ r)] -= 1000000007;
}
}
}
}
}
}
long long resp = 0;
for (long long i = 0; i < 8; i++) {
resp = (resp + sum[32][(i >> 2) & 1][(i >> 1) & 1][i & 1]) % 1000000007;
resp = (resp + memo[32][(i >> 2) & 1][(i >> 1) & 1][i & 1]) % 1000000007;
}
return resp;
}
int main() {
long long q;
scanf("%lld", &q);
for (long long i = 0; i < q; i++) {
long long x1, y1, x2, y2, k;
scanf("%lld %lld %lld %lld %lld", &x1, &y1, &x2, &y2, &k);
x1--;
x2--;
y1--;
y2--;
k--;
long long resp = dp(x2, y2, k);
if (y1) resp = (resp - dp(x2, y1 - 1, k) + 1000000007) % 1000000007;
if (x1) resp = (resp - dp(x1 - 1, y2, k) + 1000000007) % 1000000007;
if (x1 && y1) resp = (resp + dp(x1 - 1, y1 - 1, k)) % 1000000007;
printf("%lld\n", resp);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007LL;
long long sumRange(long long lo, long long hi, long long k, long long shift) {
assert(lo <= hi);
lo += shift;
assert(lo < 2e9 + 123);
hi += shift;
assert(hi < 2e9 + 123);
if (k < lo) return 0;
hi = min(hi, k);
return ((lo + hi) * (hi - lo + 1) / 2) % MOD;
}
long long largestPowOf2(long long hi) {
long long p = 1;
while (p * 2 <= hi) p *= 2;
return p;
}
long long solve(long long D, long long R, long long k, long long shift) {
if (D < R) swap(D, R);
if (R == 0) return 0;
if (R == 1) return sumRange(1, D, k, shift);
long long r = largestPowOf2(R);
long long d = largestPowOf2(D);
if (r == d) {
long long S = sumRange(1, r, k, shift) * r % MOD;
S += (D + R - 2 * r) * sumRange(1, r, k, r + shift);
S %= MOD;
S += solve(D - r, R - r, k, shift);
S %= MOD;
return S;
} else {
long long S = sumRange(1, d, k, shift) * R % MOD;
S += solve(D - d, R, k, shift + d);
S %= MOD;
return S;
}
}
int main() {
int tc;
cin >> tc;
while (tc--) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--, y1--;
long long ans = 0;
ans += solve(x2, y2, k, 0);
ans += solve(x1, y1, k, 0);
ans -= solve(x1, y2, k, 0);
ans -= solve(x2, y1, k, 0);
ans += 10 * MOD;
ans %= MOD;
cout << ans << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x, int y) {
while (x >= mod) x -= mod;
while (y >= mod) y -= mod;
x += y;
while (x >= mod) x -= mod;
return x;
}
int T, k;
int calc(int, int, long long);
int main() {
scanf("%d", &T);
while (T--) {
int x1, y1, x2, y2;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
printf("%d\n", add(add(calc(x2, y2, 0), calc(x1 - 1, y1 - 1, 0)),
mod - add(calc(x1 - 1, y2, 0), calc(x2, y1 - 1, 0))));
}
return 0;
}
int calc(int x, int y, long long p) {
if (x == 0 || y == 0) return 0;
if (p >= k) return 0;
if (x < y) swap(x, y);
int a = 1;
while (a <= x) a <<= 1;
a >>= 1;
if (a >= y) {
long long w = min((long long)k, p + a);
return add((long long)(w - p) * (p + 1 + w) / 2 % mod * y % mod,
calc(x - a, y, p + a));
} else {
long long w = min(p + a, (long long)k);
return add((long long)(w - p) * (w + p + 1) / 2 % mod * a % mod,
add(calc(x - a, a, p + a),
add(calc(a, y - a, p + a), calc(x - a, y - a, p))));
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
void amax(X& x, const Y& y) {
if (x < y) x = y;
}
template <class X, class Y>
void amin(X& x, const Y& y) {
if (x > y) x = y;
}
const int INF = 1e9 + 10;
const long long INFL = 1e18 + 10;
const int BASE = 1e9 + 7;
int q;
int nearest_p2(int x) {
int p = (int)log2l(x);
for (int i = -3; i <= 3; i++)
if (p + i >= 0 && p + i < 31 && (1 << (p + i)) >= x) return (1 << (p + i));
return 0;
}
long long getsum(int x) { return ((long long)x * (x + 1) / 2) % BASE; }
long long getsum(int l, int r) { return (getsum(r) - getsum(l - 1)) % BASE; }
long long calc(int x, int y, int start, int k) {
if (x < 1 || y < 1 || start > k) return 0;
int p2 = nearest_p2(max(x, y));
int vmax = max(x, y), vmin = min(x, y);
if (p2 == vmax) return getsum(start, min(k, vmax + start - 1)) * vmin;
int hp2 = p2 / 2;
long long res = calc(min(x, hp2), min(y, hp2), start, k);
if (y > hp2) res = (res + calc(min(x, hp2), y - hp2, start + hp2, k)) % BASE;
if (x > hp2) res = (res + calc(x - hp2, min(y, hp2), start + hp2, k)) % BASE;
if (x > hp2 && y > hp2) res = (res + calc(x - hp2, y - hp2, start, k)) % BASE;
return res;
}
long long calc(int djfowi, int oewoie, int ejowlw, int lzoiew, int k) {
long long res = calc(ejowlw, lzoiew, 1, k) - calc(djfowi - 1, lzoiew, 1, k) -
calc(ejowlw, oewoie - 1, 1, k) +
calc(djfowi - 1, oewoie - 1, 1, k);
res %= BASE;
if (res < 0) res += BASE;
return res;
}
void process() {
cin >> q;
while (q--) {
int djfowi, oewoie, ejowlw, lzoiew, k;
cin >> djfowi >> oewoie >> ejowlw >> lzoiew >> k;
cout << calc(djfowi, oewoie, ejowlw, lzoiew, k) << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
process();
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
char c = getchar();
int x = 0;
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c & 15);
c = getchar();
}
return x;
}
long long qpow(long long x, long long y) {
long long e = 1;
while (y) {
if (y % 2) e = (e * x) % 1000000007;
x = x * x % 1000000007;
y /= 2;
}
return e;
}
long long low(long long x) {
for (long long i = 1; i <= x; i <<= 1) {
if (i << 1 > x) return i;
}
}
long long getS(long long x, long long y, long long n) {
return (y - x + 1) * (x + y) % 1000000007 * qpow(2, 1000000007 - 2) %
1000000007 * n % 1000000007;
}
long long dfs(long long a, long long b, long long k, long long t) {
if (a <= 0 || b <= 0 || k <= 0) return 0;
long long l = min(a, b);
long long r = max(a, b);
long long x = low(l);
long long limit = min(r / x, k / x);
long long ans = 0;
if (l == r && l == x) {
ans = (ans + getS(t + 1, t + min(x, k), x)) % 1000000007;
} else {
if (limit % 2 == 0) {
long long rema = min(r - limit * x, x);
ans = (ans + dfs(x, rema, k - limit * x, t + limit * x)) % 1000000007;
ans = (ans + dfs(l - x, rema, k - limit * x - x, t + limit * x + x)) %
1000000007;
rema = min(r - limit * x - x, x);
ans = (ans + dfs(x, rema, k - limit * x - x, t + limit * x + x)) %
1000000007;
ans = (ans + dfs(l - x, rema, k - limit * x, t + limit * x)) % 1000000007;
} else {
limit--;
ans = (ans + dfs(x, x, k - limit * x, t + limit * x)) % 1000000007;
ans = (ans + dfs(l - x, x, k - limit * x - x, t + limit * x + x)) %
1000000007;
long long rema = min(r - limit * x - x, x);
ans = (ans + dfs(x, rema, k - limit * x - x, t + limit * x + x)) %
1000000007;
ans = (ans + dfs(l - x, rema, k - limit * x, t + limit * x)) % 1000000007;
}
ans = (ans + getS(t + 1, t + limit * x, l)) % 1000000007;
}
return ans;
}
int main() {
int N, x1, x2, y1, y2, k;
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
x1 = read();
y1 = read();
x2 = read();
y2 = read();
k = read();
long long ans = ((dfs(x2, y2, k, 0) - dfs(x1 - 1, y2, k, 0) -
dfs(x2, y1 - 1, k, 0) + dfs(x1 - 1, y1 - 1, k, 0)) %
1000000007 +
1000000007) %
1000000007;
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
const int N = 100;
const int moder = 1e9 + 7;
int power[N];
int test;
int powermod(int a, int exp) {
int ret = 1;
for (; exp; exp >>= 1) {
if (exp & 1) {
ret = 1ll * ret * a % moder;
}
a = 1ll * a * a % moder;
}
return ret;
}
int sum(int x) { return 1ll * x * (x + 1) / 2 % moder; }
int solve(int x, int y, int k) {
if (!x || !y) {
return 0;
}
int ret = 0;
for (int i = 0; i < 30; ++i) {
for (int j = 0; j < 30; ++j) {
if ((x >> i & 1) && (y >> j & 1)) {
int x1 = (x >> i) - 1, y1 = (y >> j) - 1;
if (i < j) {
x1 >>= j - i;
} else {
y1 >>= i - j;
}
int z1 = x1 ^ y1, z2 = z1, max = std::max(i, j);
for (int u = 0; u < max; ++u) {
z1 = z1 << 1;
z2 = z2 << 1 | 1;
}
if (k < z1) {
continue;
}
int kk = std::min(z2, k);
ret = (ret +
1ll * power[std::min(i, j)] * (sum(kk) - sum(z1 - 1) + moder)) %
moder;
ret =
(ret + 1ll * power[std::min(i, j)] * (kk - z1 + 1 + moder)) % moder;
}
}
}
return ret;
}
int main() {
power[0] = 1;
for (int i = 1; i < N; ++i) {
power[i] = 2 * power[i - 1] % moder;
}
scanf("%d", &test);
while (test--) {
int x1, x2, y1, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
--k;
int ans = (solve(x2, y2, k) + solve(x1 - 1, y1 - 1, k)) % moder;
ans = (ans - solve(x2, y1 - 1, k) + moder) % moder;
ans = (ans - solve(x1 - 1, y2, k) + moder) % moder;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using std::cerr;
using std::cin;
using std::max;
using std::min;
int n, a, b, c, d, K, A[40], B[40], C[40], f[40][2][2][2], g[40][2][2][2], ans;
int cal(int x) {
x -= x < 1000000007 ? 0 : 1000000007;
return x;
}
int Solve(int x, int y, int v) {
if (x < 0 || y < 0) return 0;
int An = 0, Bn = 0, Cn = 0, len;
for (; x > 0; x >>= 1) A[++An] = x & 1;
for (; y > 0; y >>= 1) B[++Bn] = y & 1;
for (; v > 0; v >>= 1) C[++Cn] = v & 1;
len = max(max(An, Bn), max(Cn, 1));
for (; An < len;) A[++An] = 0;
for (; Bn < len;) B[++Bn] = 0;
for (; Cn < len;) C[++Cn] = 0;
std::reverse(A + 1, A + 1 + An), std::reverse(B + 1, B + 1 + Bn),
std::reverse(C + 1, C + 1 + Cn);
for (int i = 0; i <= len; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
for (int l = 0; l < 2; l++) f[i][j][k][l] = g[i][j][k][l] = 0;
f[0][1][1][1] = 1, g[0][1][1][1] = 0;
for (int i = 0, nj, nk, nl; i < len; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
for (int l = 0; l < 2; l++)
if (f[i][j][k][l])
for (int s1 = 0; s1 < 2; s1++) {
if (j && s1 && !A[i + 1]) continue;
for (int s2 = 0; s2 < 2; s2++) {
if (k && s2 && !B[i + 1]) continue;
if (l && s1 ^ s2 && !C[i + 1]) continue;
nj = j & (s1 == A[i + 1]), nk = k & (s2 == B[i + 1]),
nl = l & ((s1 ^ s2) == C[i + 1]);
f[i + 1][nj][nk][nl] =
cal(f[i + 1][nj][nk][nl] + f[i][j][k][l]);
g[i + 1][nj][nk][nl] =
(g[i + 1][nj][nk][nl] + 2ll * g[i][j][k][l]) % 1000000007;
if (s1 ^ s2)
g[i + 1][nj][nk][nl] =
cal(g[i + 1][nj][nk][nl] + f[i][j][k][l]);
}
}
int res = 0;
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
res = ((long long)res + f[len][i][j][k] + g[len][i][j][k]) % 1000000007;
return res;
}
int main() {
for (scanf("%d", &n); n--;) {
scanf("%d%d%d%d%d", &a, &b, &c, &d, &K), a--, b--, c--, d--, K--;
ans = ((long long)Solve(c, d, K) - Solve(c, b - 1, K) - Solve(a - 1, d, K) +
Solve(a - 1, b - 1, K)) %
1000000007;
printf("%d\n", cal(ans + 1000000007));
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.*;
import java.util.*;
public class E415 {
static final int MOD = (int)1e9 + 7;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int q = in.nextInt();
int x1, y1, x2, y2, k;
for(int i = 0; i < q; ++i) {
x1 = in.nextInt() - 1;
y1 = in.nextInt() - 1;
x2 = in.nextInt() - 1;
y2 = in.nextInt() - 1;
k = in.nextInt() - 1;
int ans = 0;
ans = add(ans, query(x2, y2, k));
ans = sub(ans, query(x2, y1 - 1, k));
ans = sub(ans, query(x1 - 1, y2, k));
ans = add(ans, query(x1 - 1, y1 - 1, k));
out.println(ans);
}
out.close();
}
static int query(int x, int y, int k) {
if(x < 0 || y < 0 || k < 0) {
return 0;
}
int cnt[][][][] = new int[32][2][2][2];
int sum[][][][] = new int[32][2][2][2];
cnt[0][1][1][1] = 1;
int X[] = new int[32];
int Y[] = new int[32];
int K[] = new int[32];
for(int i = 0; i < 32; ++i) {
X[31 - i] = x & 1;
x >>= 1;
Y[31 - i] = y & 1;
y >>= 1;
K[31 - i] = k & 1;
k >>= 1;
}
int z;
int aa, bb, cc;
for(int i = 1; i < 32; ++i) {
for(int a = 0; a <= 1; ++a)
for(int b = 0; b <= 1; ++b)
for(int c = 0; c <= 1; ++c) {
for(x = 0; x <= 1; ++x)
for(y = 0; y <= 1; ++y) {
z = x ^ y;
if(a == 1 && X[i] == 0 && x == 1) continue;
if(b == 1 && Y[i] == 0 && y == 1) continue;
if(c == 1 && K[i] == 0 && z == 1) continue;
aa = X[i] == x ? a : 0;
bb = Y[i] == y ? b : 0;
cc = K[i] == z ? c : 0;
cnt[i][aa][bb][cc] = add(cnt[i][aa][bb][cc], cnt[i-1][a][b][c]);
sum[i][aa][bb][cc] = add(sum[i][aa][bb][cc], sum[i-1][a][b][c]);
sum[i][aa][bb][cc] = add(sum[i][aa][bb][cc],
mult(cnt[i-1][a][b][c], (z << (31 - i))));
}
}
}
int ans = 0;
for(int a = 0; a <= 1; ++a)
for(int b = 0; b <= 1; ++b)
for(int c = 0; c <= 1; ++c) {
ans = add(ans, sum[31][a][b][c]);
ans = add(ans, cnt[31][a][b][c]);
}
return ans;
}
static int add(int x, int y) {
x %= MOD;
y %= MOD;
return (x + y) % MOD;
}
static int sub(int x, int y) {
x %= MOD;
y %= MOD;
return (x + (MOD - y)) % MOD;
}
static int mult(int x, int y) {
x %= MOD;
y %= MOD;
long prod = (long) x * y;
prod %= MOD;
return (int) prod;
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void printf(String format, Object... objects) {
writer.printf(format, objects);
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == -1) {
throw new InputMismatchException();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException var2) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
;
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
}
}
| JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.*;
import java.util.*;
public class _809C {
static long MOD = 1_000_000_007;
static Map<List<Integer>, List<Long>> f = new HashMap<>();
public static void main(String[] args) throws Exception {
Reader.init(System.in);
BufferedWriter cout = new BufferedWriter(new OutputStreamWriter(System.out));
final int MAX_LEN = 1 << 30;
StringJoiner joiner = new StringJoiner("\n");
for (int Q = Reader.nextInt(); Q > 0; Q--) {
int x1 = Reader.nextInt();
int y1 = Reader.nextInt();
int x2 = Reader.nextInt();
int y2 = Reader.nextInt();
int cap = Reader.nextInt();
long ans = G(MAX_LEN, x2, y2, cap).get(0);
ans -= G(MAX_LEN, x2, y1 - 1, cap).get(0);
ans -= G(MAX_LEN, x1 - 1, y2, cap).get(0);
ans += G(MAX_LEN, x1 - 1, y1 - 1, cap).get(0);
ans = (ans + 4 * MOD) % MOD;
joiner.add("" + ans);
f.clear();
}
System.out.println(joiner.toString());
cout.close();
}
static List<Long> F(int len, int h, int cap) {
if (cap < 1)
return Arrays.asList(0L, 0L);
if (cap > len)
return F(len, h, len);
if (h > len)
return F(len, len, cap);
if (len == 1)
return Arrays.asList(1L, 1L);
List<Integer> states = Arrays.asList(len, h, cap);
if (f.containsKey(states))
return f.get(states);
long sum = 0;
long cnt = 0;
int halfLen = len / 2;
List<Long> tmp = F(halfLen, h, cap);
sum += tmp.get(0);
cnt += tmp.get(1);
tmp = F(halfLen, h, cap - halfLen);
sum += tmp.get(0) + tmp.get(1) * halfLen % MOD;
cnt += tmp.get(1);
if (h > halfLen) {
tmp = F(halfLen, h - halfLen, cap);
sum += tmp.get(0);
cnt += tmp.get(1);
tmp = F(halfLen, h - halfLen, cap - halfLen);
sum += tmp.get(0) + tmp.get(1) * halfLen % MOD;
cnt += tmp.get(1);
}
f.put(states, Arrays.asList(sum % MOD, cnt % MOD));
return f.get(states);
}
static List<Long> G(int len, int r, int c, int cap) {
if (r < 1 || c < 1 || cap < 1)
return Arrays.asList(0L, 0L);
if (r < c)
return G(len, c, r, cap);
if (len == 1)
return F(len, r, cap); // in fact, we have r = c = 1
// Now, r >= c
int halfLen = len / 2;
long sum = 0;
long cnt = 0;
List<Long> tmp;
if (r <= halfLen)
return G(halfLen, r, c, cap);
else if (c <= halfLen) {
tmp = F(halfLen, c, cap);
sum += tmp.get(0);
cnt += tmp.get(1);
tmp = G(halfLen, r - halfLen, c, cap - halfLen);
sum += tmp.get(0) + halfLen * tmp.get(1) % MOD;
cnt += tmp.get(1);
} else {
tmp = F(halfLen, halfLen, cap);
sum += tmp.get(0);
cnt += tmp.get(1);
tmp = F(halfLen, r - halfLen, cap - halfLen);
sum += tmp.get(0) + halfLen * tmp.get(1) % MOD;
cnt += tmp.get(1);
tmp = F(halfLen, c - halfLen, cap - halfLen);
sum += tmp.get(0) + halfLen * tmp.get(1) % MOD;
cnt += tmp.get(1);
tmp = G(halfLen, r - halfLen, c - halfLen, cap);
sum += tmp.get(0);
cnt += tmp.get(1);
}
return Arrays.asList(sum % MOD, cnt % MOD);
}
static int[][] bruteForce(int n) {
int[][] ans = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Set<Integer> visited = new HashSet<>();
for (int k = 0; k < i; k++)
visited.add(ans[k][j]);
for (int k = 0; k < j; k++)
visited.add(ans[i][k]);
for (int x = 1; ; x++)
if (!visited.contains(x)) {
ans[i][j] = x;
break;
}
}
}
return ans;
}
/**
* Class for buffered reading int and double values
*/
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
} | JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
int addMod(int a, int b, int m = MOD) {
a += b;
if (m <= a) {
a -= m;
}
return a;
}
int mulMod(int a, int b, int m = MOD) { return a * 1ll * b % m; }
int dp[32][2][2][2], dp2[32][2][2][2];
bool bit(int x, int p) { return x & (1 << p); }
int calc(int n, int m, int k) {
memset(dp, 0, sizeof dp);
memset(dp2, 0, sizeof dp2);
dp[31][0][0][0] = 1;
for (int i = 30; i >= 0; --i) {
for (int kl = 0; kl < 2; ++kl) {
for (int nl = 0; nl < 2; ++nl) {
for (int ml = 0; ml < 2; ++ml) {
for (int x = 0; x < 2; ++x) {
for (int y = 0; y < 2; ++y) {
if (nl == 0 && x > bit(n, i)) {
continue;
}
if (ml == 0 && y > bit(m, i)) {
continue;
}
int z = (x ^ y);
if (kl == 0 && z > bit(k, i)) {
continue;
}
int nnl = (nl | (x < bit(n, i)));
int nml = (ml | (y < bit(m, i)));
int nkl = (kl | (z < bit(k, i)));
dp[i][nkl][nnl][nml] =
addMod(dp[i][nkl][nnl][nml], dp[i + 1][kl][nl][ml]);
dp2[i][nkl][nnl][nml] =
addMod(dp2[i][nkl][nnl][nml], dp2[i + 1][kl][nl][ml]);
dp2[i][nkl][nnl][nml] = addMod(
dp2[i][nkl][nnl][nml], mulMod(z << i, dp[i + 1][kl][nl][ml]));
}
}
}
}
}
}
return addMod(dp[0][1][1][1], dp2[0][1][1][1]);
}
void solve() {
int x1, y1, x2, y2, k;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
int ans = calc(x2, y2, k);
ans = addMod(ans, MOD - calc(x1 - 1, y2, k));
ans = addMod(ans, MOD - calc(x2, y1 - 1, k));
ans = addMod(ans, calc(x1 - 1, y1 - 1, k));
printf("%d\n", ans);
}
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
solve();
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.io.*;
import java.util.*;
public class Codeforces810E {
public static int[] global = new int[100];
public static void main(String[] args) throws IOException {
global[0] = 1;
for (int i = 1; i < 100; i++) {
global[i] = (2*global[i-1])%1000000007;
}
int[] twopower = new int[100];
twopower[0] = 1;
for (int i = 1; i < 100; i++) {
twopower[i] = (twopower[i-1]*2)%1000000007;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
String[] sp = br.readLine().split(" ");
int q = Integer.parseInt(sp[0]);
for (int i = 0; i < q; i++) {
sp = br.readLine().split(" ");
int x1 = Integer.parseInt(sp[0]);
int y1 = Integer.parseInt(sp[1]);
int x2 = Integer.parseInt(sp[2]);
int y2 = Integer.parseInt(sp[3]);
int k = Integer.parseInt(sp[4]);
int answer = 0;
if ((x1 == 1) && (y1 == 1)) {
answer = answer(x2, y2, k);
}
else if (x1 == 1) {
answer = answer(x2, y2, k)-answer(x2, y1-1, k);
}
else if (y1 == 1) {
answer = answer(x2, y2, k) - answer(x1-1, y2, k);
}
else {
answer = answer(x2, y2, k) - answer(x1-1, y2, k) - answer(x2, y1-1, k) + answer(x1-1, y1-1, k);
}
answer %= 1000000007;
if (answer < 0)
answer += 1000000007;
pw.println(answer);
}
pw.close();
}
public static int answer(int m, int n, int k) {
int tmp = m;
int counter = 0;
int counter2 = 0;
while (tmp > 0) {
if (tmp%2 == 1)
counter++;
counter2++;
tmp = tmp/2;
}
int[][] mlist = new int[counter][2];
counter--;
for (int i = counter2-1; i >= 0; i--) {
if ((m >> i)%2 == 1) {
mlist[counter][0] = (m >> i)-1;
mlist[counter][1] = i;
counter--;
}
}
tmp = n;
counter = 0;
counter2 = 0;
while (tmp > 0) {
if (tmp%2 == 1)
counter++;
counter2++;
tmp = tmp/2;
}
int[][] nlist = new int[counter][2];
counter--;
for (int i = counter2-1; i >= 0; i--) {
if ((n >> i)%2 == 1) {
nlist[counter][0] = (n >> i)-1;
nlist[counter][1] = i;
counter--;
}
}
int answer = 0;
for (int i = 0; i < mlist.length; i++) {
for (int j = 0; j < nlist.length; j++) {
answer = (answer + sum(mlist[i][1], nlist[j][1], mlist[i][0], nlist[j][0], k))%1000000007;
}
}
return answer;
}
public static int sum(int m, int n, int r, int s, int k) {
if (m < n) {
int temp = m;
m = n;
n = temp;
temp = r;
r = s;
s = temp;
}
int t = s >> (m-n);
int u = r^t;
long val = 0;
if (k > u*global[m]) {
int min = u*global[m];
int max = Math.min(k, (u+1)*global[m]);
val = (((long) max)*((long) max+1))/2 - (((long) min)*((long)min+1))/2;
}
val %= 1000000007;
val *= global[n];
val %= 1000000007;
return (int) val;
}
}
| JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
void add(long long &x, long long y) {
x += y;
x %= MOD;
}
void sub(long long &x, long long y) {
x = x - y + (long long)MOD;
x %= MOD;
}
long long calc(long long x, long long y, long long k, long long d = 0) {
if (x > y) {
swap(x, y);
}
if (x <= 0 || k <= 0) {
return 0;
}
long long p = (1ll << ((int)log2(y)));
long long ans = 0;
long long t = min(p, k);
long long rowSum = ((1 + t) * t / 2) % MOD;
if (x <= p) {
ans = (rowSum * (x % MOD)) % MOD;
long long rem = (((t * x) % MOD) * (d % MOD)) % MOD;
add(ans, (rem + calc(x, y - p, k - p, d + p)) % MOD);
return ans;
}
ans = ((rowSum * p) % MOD + (((p * t) % MOD) * (d % MOD)) % MOD) % MOD;
add(ans, calc(x - p, y - p, k, d));
add(ans, calc(x - p, p, k - p, d + p));
add(ans, calc(p, y - p, k - p, d + p));
return ans;
}
int main(void) {
long long x1, y1, x2, y2, k, ans;
int q;
scanf("%d", &q);
while (q--) {
scanf("%lld %lld %lld %lld %lld", &x1, &y1, &x2, &y2, &k);
ans = 0;
add(ans, calc(x2, y2, k));
sub(ans, calc(x2, y1 - 1, k));
sub(ans, calc(x1 - 1, y2, k));
add(ans, calc(x1 - 1, y1 - 1, k));
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | mod = 1000000007
def sum(x, y, k, add) :
if k < add : return 0
up = x + add
if up > k : up = k
add = add + 1
return y * ( ( (add + up) * (up - add + 1) // 2 ) % mod ) % mod
def solve(x, y, k, add = 0) :
if x == 0 or y == 0 : return 0
if x > y :
x, y = y, x
pw = 1
while (pw << 1) <= y :
pw <<= 1
if pw <= x :
return ( sum(pw, pw, k, add)\
+ sum(pw, x + y - pw - pw, k, add + pw)\
+ solve(x - pw, y - pw, k, add) ) % mod
else :
return ( sum(pw, x, k, add)\
+ solve(x, y - pw, k, add + pw) ) % mod
q = int(input())
for i in range(0, q) :
x1, y1, x2, y2, k = list(map(int, input().split()))
ans = ( solve(x2, y2, k)\
- solve(x1 - 1, y2, k)\
- solve(x2, y1 - 1, k)\
+ solve(x1 - 1, y1 - 1, k) ) % mod
if ans < 0 : ans += mod
print(ans)
| PYTHON3 |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long pwr(long long base, long long p, long long mod = (1000000007LL)) {
long long ans = 1;
while (p) {
if (p & 1) ans = (ans * base) % mod;
base = (base * base) % mod;
p /= 2;
}
return ans;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int xbits[32], ybits[32], kbits[32];
long long DP_sum[32][2][2][2], DP_cnt[32][2][2][2];
long long dp_cnt(int i, int xless, int yless, int kless) {
if (i == -1) return 1;
long long &ans = DP_cnt[i][xless][yless][kless];
if (ans != -1) return ans;
ans = 0;
for (int next_x = 0; next_x < 2; next_x++) {
if (xless == 0 && next_x > xbits[i]) continue;
int new_xless = xless | (next_x < xbits[i]);
for (int next_y = 0; next_y < 2; next_y++) {
if (yless == 0 && next_y > ybits[i]) continue;
int new_yless = yless | (next_y < ybits[i]);
int next_k = next_x ^ next_y;
if (kless == 0 && next_k > kbits[i]) continue;
int new_kless = kless | (next_k < kbits[i]);
ans += dp_cnt(i - 1, new_xless, new_yless, new_kless);
ans %= (1000000007LL);
}
}
return ans;
}
long long dp_sum(int i, int xless, int yless, int kless) {
if (i == -1) return 0;
long long &ans = DP_sum[i][xless][yless][kless];
if (ans != -1) return ans;
ans = 0;
for (int next_x = 0; next_x < 2; next_x++) {
if (xless == 0 && next_x > xbits[i]) continue;
int new_xless = xless | (next_x < xbits[i]);
for (int next_y = 0; next_y < 2; next_y++) {
if (yless == 0 && next_y > ybits[i]) continue;
int new_yless = yless | (next_y < ybits[i]);
int next_k = next_x ^ next_y;
if (kless == 0 && next_k > kbits[i]) continue;
int new_kless = kless | (next_k < kbits[i]);
ans += dp_sum(i - 1, new_xless, new_yless, new_kless) +
(next_k) * (1LL << i) *
dp_cnt(i - 1, new_xless, new_yless, new_kless);
ans %= (1000000007LL);
}
}
return ans;
}
long long solve(int x, int y, int k) {
if (x < 0 || y < 0) return 0;
int logn = 31;
for (int i = 0; i < logn; i++) {
xbits[i] = x % 2;
x /= 2;
ybits[i] = y % 2;
y /= 2;
kbits[i] = k % 2;
k /= 2;
}
memset(DP_sum, -1, sizeof(DP_sum));
memset(DP_cnt, -1, sizeof(DP_cnt));
long long ans = dp_sum(logn - 1, 0, 0, 0) + dp_cnt(logn - 1, 0, 0, 0);
return ans % (1000000007LL);
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
int x1, y1, x2, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
x1--;
y1--;
x2--;
y2--;
k--;
long long ans = solve(x2, y2, k) - solve(x1 - 1, y2, k) -
solve(x2, y1 - 1, k) + solve(x1 - 1, y1 - 1, k);
ans %= (1000000007LL);
ans += (1000000007LL);
ans %= (1000000007LL);
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = (1e9) + 7;
void add(int &x, int y) {
x += y;
if (x >= mod) x -= mod;
}
void sub(int &x, int y) {
x -= y;
if (x < 0) x += mod;
}
int f[33][2][2][2], g[33][2][2][2];
int solve(int X, int Y, int K) {
X--;
Y--;
K--;
if (X < 0 || Y < 0 || K < 0) return 0;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
int res = 0;
for (int i = 0; i <= 32; ++i) {
for (int j = 0; j < 2; ++j)
for (int k = 0; k < 2; ++k)
for (int t = 0; t < 2; ++t) {
if (i == 0) {
g[i][j][k][t] = 1;
continue;
}
int xi = (X & (1 << (i - 1))) > 0;
int yi = (Y & (1 << (i - 1))) > 0;
int ki = (K & (1 << (i - 1))) > 0;
for (int x = 0; x < 2; ++x)
for (int y = 0; y < 2; ++y) {
if (j && x > xi) continue;
if (k && y > yi) continue;
int z = x ^ y;
if (t && z > ki) continue;
int jj = j;
if (j && x < xi) jj = 0;
int kk = k;
if (k && y < yi) kk = 0;
int tt = t;
if (t && z < ki) tt = 0;
add(f[i][j][k][t], f[i - 1][jj][kk][tt]);
add(f[i][j][k][t],
(1ll << (i - 1)) * z * g[i - 1][jj][kk][tt] % mod);
add(g[i][j][k][t], g[i - 1][jj][kk][tt]);
}
}
}
add(res, f[31][1][1][1]);
add(res, g[31][1][1][1]);
return res;
}
int main() {
int q;
cin >> q;
for (int _ = 0; _ < q; ++_) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
int ret = 0;
add(ret, solve(x2, y2, k));
sub(ret, solve(x1 - 1, y2, k));
sub(ret, solve(x2, y1 - 1, k));
add(ret, solve(x1 - 1, y1 - 1, k));
cout << ret << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long mo = 1e9 + 7;
long long k;
set<long long> s;
inline long long work(long long x, long long y, long long f) {
if (x <= 0 || y <= 0 || f > k) return 0;
if (x < y) swap(x, y);
if (s.find(x) != s.end()) {
long long l = f, r = min(f + x - 1, k);
return ((l + r) * (r - l + 1) / 2) % mo * y % mo;
}
long long o = *(--(s.lower_bound(x)));
long long res = 0;
res = (res + work(o, min(o, y), f)) % mo;
res = (res + work(o, y - o, f + o)) % mo;
res = (res + work(x - o, min(y, o), f + o)) % mo;
res = (res + work(x - o, y - o, f)) % mo;
return res;
}
inline void solve() {
long long x1, y1, x2, y2;
scanf("%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &k);
long long o1 = work(x2, y2, 1);
long long o2 = work(x1 - 1, y2, 1);
long long o3 = work(x2, y1 - 1, 1);
long long o4 = work(x1 - 1, y1 - 1, 1);
printf("%lld\n", ((o1 + o4) % mo - o2 - o3 + mo + mo) % mo);
}
int main() {
long long o = 1;
while (o <= 1e9) s.insert(o), o *= 2;
int T = 1;
scanf("%d", &T);
while (T--) solve();
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const long long MOD = 1e9 + 7;
pair<long long, long long> dp[33][2][2][2];
int vis[33][2][2][2], vid;
pair<long long, long long> call(int pos, bool t1, bool t2, bool t3, int x,
int y, int k) {
if (pos == -1) {
return make_pair(1, 0);
}
if (vis[pos][t1][t2][t3] == vid) return dp[pos][t1][t2][t3];
if (vis[pos][t1][t2][t3] > 0 and t1 == 0 and t2 == 0 and t3 == 0)
return dp[pos][t1][t2][t3];
vis[pos][t1][t2][t3] = vid;
pair<long long, long long> ret = make_pair(0, 0);
bool h1 = (t1 ? (x & (1LL << pos)) : 1);
bool h2 = (t2 ? (y & (1LL << pos)) : 1);
bool h3 = (t3 ? (k & (1LL << pos)) : 1);
for (int f = 0; f <= h1; f++) {
for (int s = 0; s <= h2; s++) {
if ((f ^ s) > h3) continue;
bool newt1 = t1 & (f == h1);
bool newt2 = t2 & (s == h2);
bool newt3 = t3 & ((f ^ s) == h3);
pair<long long, long long> p =
call(pos - 1, newt1, newt2, newt3, x, y, k);
ret.first += p.first;
ret.second += ((f ^ s) * (1 << pos) * p.first) + p.second;
}
}
ret.first %= MOD;
ret.second %= MOD;
return dp[pos][t1][t2][t3] = ret;
}
long long solve(int x, int y, int k) {
if (x == 0 || y == 0) return 0;
vid++;
x--;
y--;
k--;
pair<long long, long long> ans = call(30, 1, 1, 1, x, y, k);
return (ans.second + ans.first) % MOD;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int q;
cin >> q;
while (q--) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
long long ans = solve(x2, y2, k) - solve(x1 - 1, y2, k) -
solve(x2, y1 - 1, k) + solve(x1 - 1, y1 - 1, k);
ans = (ans % MOD + MOD) % MOD;
cout << ans << "\n";
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long MODD = 1e9 + 7;
int q;
long long a, b, u, v, k;
long long calc(long long u, long long v, long long k, long long o = 0) {
if (u > v) swap(u, v);
if (u < 1 || k <= 0) return 0;
long long p = 1, res = 0;
while (p * 2LL <= v) p *= 2LL;
long long tmp = min(p, k);
if (u <= p) {
res = (tmp * (tmp + 1) / 2 % MODD) * u % MODD;
res += (o * tmp % MODD) * u % MODD + calc(u, v - p, k - p, o + p);
res %= MODD;
} else {
res = (tmp * (tmp + 1) / 2 % MODD) * p % MODD;
res += (o * tmp % MODD) * p % MODD;
res += calc(u - p, v - p, k, o) + calc(p, v - p, k - p, o + p) +
calc(u - p, p, k - p, o + p);
}
return res;
}
int main() {
scanf("%d", &q);
while (q-- > 0) {
scanf("%I64d %I64d %I64d %I64d %I64d", &a, &b, &u, &v, &k);
long long res = (calc(u, v, k) + calc(a - 1, b - 1, k)) % MODD;
res = (res - calc(a - 1, v, k) - calc(u, b - 1, k) + MODD * MODD) % MODD;
printf("%lld\n", res);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
const long long MOD = 1e9 + 7;
long long rec(long long x1, long long x2, long long y1, long long y2,
long long upper, long long len, long long offs) {
if (upper <= 0) return 0;
if (x1 > y1 || (x1 == y1 && x2 < y2)) swap(x1, y1), swap(x2, y2);
if (x1 == 0 && x2 == len) {
upper = min(upper, len);
long long sum = ((upper * (upper - 1)) / 2) % MOD;
long long res = ((y2 - y1) * ((sum + (offs * upper) % MOD) % MOD)) % MOD;
return res;
}
long long res = 0;
len /= 2;
if (x1 < len && y1 < len)
res += rec(x1, min(x2, len), y1, min(y2, len), upper, len, offs);
if (x2 > len && y1 < len)
res += rec(max(x1 - len, 0LL), x2 - len, y1, min(y2, len), upper - len, len,
offs + len);
if (x1 < len && y2 > len)
res += rec(x1, min(x2, len), max(y1 - len, 0LL), y2 - len, upper - len, len,
offs + len);
if (x2 > len && y2 > len)
res += rec(max(x1 - len, 0LL), x2 - len, max(y1 - len, 0LL), y2 - len,
upper, len, offs);
return res % MOD;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long q;
cin >> q;
while (q--) {
long long x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--, y1--;
cout << rec(x1, x2, y1, y2, k, 1 << 30, 1) << "\n";
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
const long long INFL = 1001001001001001001LL;
template <typename T>
void pv(T a, T b) {
for (T i = a; i != b; ++i) cout << *i << " ";
cout << endl;
}
template <class T>
bool uin(T &a, T b) {
return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
return a < b ? (a = b, true) : false;
}
int in() {
int x;
cin >> x;
return x;
}
double fin() {
double x;
cin >> x;
return x;
}
string sin() {
string x;
cin >> x;
return x;
}
long long lin() {
long long x;
cin >> x;
return x;
}
long long mod = 1000000007;
long long solve(long long n, long long x1, long long y1, long long x2,
long long y2, long long k, long long plus) {
if (0 > x2 || 0 > y2 || n - 1 < x1 || n - 1 < y1 || plus >= k) return 0;
if (x1 <= 0 && n - 1 <= x2 && y1 <= 0 && n - 1 <= y2) {
long long a = min(plus + n, k);
long long b = plus + 1;
long long sol = (a + b) * (a - b + 1) / 2 % mod * n % mod;
return sol;
} else if (x1 <= 0 && n - 1 <= x2) {
long long a = min(plus + n, k);
long long b = plus + 1;
long long my1 = max(0LL, y1);
long long my2 = min(n - 1, y2);
long long sol = (a + b) * (a - b + 1) / 2 % mod * (my2 - my1 + 1) % mod;
return sol;
} else if (y1 <= 0 && n - 1 <= y2) {
long long a = min(plus + n, k);
long long b = plus + 1;
long long mx1 = max(0LL, x1);
long long mx2 = min(n - 1, x2);
long long sol = (a + b) * (a - b + 1) / 2 % mod * (mx2 - mx1 + 1) % mod;
return sol;
} else {
long long m = n / 2;
long long sol1 = solve(m, x1, y1, x2, y2, k, plus);
long long sol2 = solve(m, x1 - m, y1, x2 - m, y2, k, plus + m);
long long sol3 = solve(m, x1, y1 - m, x2, y2 - m, k, plus + m);
long long sol4 = solve(m, x1 - m, y1 - m, x2 - m, y2 - m, k, plus);
return (sol1 + sol2 + sol3 + sol4) % mod;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int q = in();
for (int cs = 0; cs < (int)(q); ++cs) {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--;
y1--;
x2--;
y2--;
long long cur = 1;
while (cur <= x2 || cur <= y2) cur <<= 1;
cout << solve(cur, x1, y1, x2, y2, k, 0) << '\n';
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
inline long long read() {
long long x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + c - '0';
c = getchar();
}
return x * f;
}
int Q, a[35], b[35], c[35], ans;
int dp[35][2][2][2], t[35][2][2][2];
pair<int, int> dfs(int u, int x, int y, int z) {
if (u < 0) return make_pair(1, 0);
int &tp = dp[u][x][y][z];
int &s = t[u][x][y][z];
if (~tp) {
ans = (ans + s) % mod;
return make_pair(tp, s);
}
tp = 0;
int pa = (x ? a[u] : 1);
int pb = (y ? b[u] : 1);
int pc = (z ? c[u] : 1);
for (int i = 0; i <= pa; ++i) {
for (int j = 0; j <= pb; ++j) {
for (int k = 0; k <= pc; ++k) {
if ((i ^ j) == k) {
auto [d1, d2] =
dfs(u - 1, x & (i == pa), y & (j == pb), z & (k == pc));
s = (s + d2) % mod;
if (k) s = (s + 1LL * (1 << u) * d1) % mod;
tp = (tp + d1) % mod;
}
}
}
}
ans = (ans + s) % mod;
return make_pair(tp, s);
}
int Solve(int x, int y, int k) {
if (!x || !y) return 0;
--x, --y, --k;
memset(dp, -1, sizeof(dp));
memset(t, 0, sizeof(t));
ans = 0;
for (int i = 0; i <= 30; ++i) {
a[i] = (x >> i & 1);
b[i] = (y >> i & 1);
c[i] = (k >> i & 1);
}
auto [A, B] = dfs(30, 1, 1, 1);
return (A + B) % mod;
}
int main() {
Q = read();
while (Q--) {
int l1 = read(), l2 = read(), r1 = read(), r2 = read(), k = read();
int ans = Solve(r1, r2, k) - Solve(l1 - 1, r2, k) - Solve(r1, l2 - 1, k) +
Solve(l1 - 1, l2 - 1, k);
ans = (ans % mod + mod) % mod;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int const MOD = 1e9 + 7;
int const N = 31;
int dp[N][2][2][2], dc[N][2][2][2];
int sum(int a, int b) { return (a + b) % MOD; }
void add(int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
}
int mul(int a, int b) { return a * 1LL * b % MOD; }
void sub(int &a, int b) {
a -= b;
if (a < 0) a += MOD;
}
void clear() {
for (int i = 0; i < N; ++i)
for (int a = 0; a < 2; ++a)
for (int b = 0; b < 2; ++b)
for (int c = 0; c < 2; ++c) dp[i][a][b][c] = dc[i][a][b][c] = 0;
}
bool bit(int mask, int id) { return ((1 << id) & mask); }
int calc(int x, int y, int k) {
if (x < 0 || y < 0) return 0;
clear();
int ret = 0;
dc[N - 1][0][0][0] = 1;
vector<int> vx, vy, vk;
for (int i = 0; i < N; ++i) {
vx.push_back(bit(x, i));
vy.push_back(bit(y, i));
vk.push_back(bit(k, i));
}
for (int i = N - 1; i >= 0; --i) {
for (int fx = 0; fx < 2; ++fx)
for (int fy = 0; fy < 2; ++fy)
for (int fk = 0; fk < 2; ++fk)
for (int cx = 0; cx < 2; ++cx)
for (int cy = 0; cy < 2; ++cy) {
if (!dc[i][fx][fy][fk] && !dp[i][fx][fy][fk]) continue;
if (fx == 0 && cx > vx[i]) continue;
if (fy == 0 && cy > vy[i]) continue;
int ck = (cx ^ cy);
if (fk == 0 && ck > vk[i]) continue;
int nfx = max(fx, (cx < vx[i] ? 1 : 0));
int nfy = max(fy, (cy < vy[i] ? 1 : 0));
int nfk = max(fk, (ck < vk[i] ? 1 : 0));
if (i > 0) {
add(dc[i - 1][nfx][nfy][nfk], dc[i][fx][fy][fk]);
int add1 = dp[i][fx][fy][fk];
int add2 = mul((1 << i), mul(dc[i][fx][fy][fk], ck));
int addv = sum(add1, add2);
add(dp[i - 1][nfx][nfy][nfk], addv);
} else {
if (fk == 0 && vk[i] == ck) continue;
add(ret, dc[i][fx][fy][fk]);
int add1 = dp[i][fx][fy][fk];
int add2 = mul((1 << i), mul(dc[i][fx][fy][fk], ck));
int addv = sum(add1, add2);
add(ret, addv);
}
}
}
return ret;
}
void solve() {
int x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
int ans = 0;
--x1;
--y1;
--x2;
--y2;
add(ans, calc(x2, y2, k));
sub(ans, calc(x1 - 1, y2, k));
sub(ans, calc(x2, y1 - 1, k));
add(ans, calc(x1 - 1, y1 - 1, k));
cout << ans << endl;
}
int main() {
int test;
cin >> test;
while (test--) {
solve();
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long int fun(long long int x, long long int y, long long int a,
long long int k, long long int l) {
long long int c[2][2], d[2][2], val, i, j, flag = 0, val1 = k, ans = 0, b = 0;
if (x == 0 || y == 0) return 0;
val = (x - 1) / a;
if (val == 0) {
c[1][0] = -1;
c[0][0] = ((x - 1) % a == a - 1) ? 1 : 0;
c[0][1] = (x - 1) % a + 1;
} else {
c[0][0] = 1;
c[0][1] = a;
c[1][0] = ((x - 1) % a == a - 1) ? 1 : 0;
c[1][1] = (x - 1) % a + 1;
}
val = (y - 1) / a;
if (val == 0) {
d[1][0] = -1;
d[0][0] = ((y - 1) % a == a - 1) ? 1 : 0;
d[0][1] = (y - 1) % a + 1;
} else {
d[0][0] = 1;
d[0][1] = a;
d[1][0] = ((y - 1) % a == a - 1) ? 1 : 0;
d[1][1] = (y - 1) % a + 1;
}
for (i = 0; i < 2; i++) {
if (c[i][0] == -1) continue;
for (j = 0; j < 2; j++) {
if (d[j][0] == -1) continue;
if (c[i][0] == 0 && d[j][0] == 0) {
flag = 1;
break;
}
val = min(c[i][1], d[j][1]);
b = 0;
if ((i ^ j) == 1) {
if (a > k - 1)
val1 = 0;
else
val1 = min(k - a, a);
b = (a + l) % 1000000007;
} else {
val1 = min(a, k);
b = l;
}
if ((val1 % 2) == 0)
ans = (ans +
(((val * (val1 + 1)) % 1000000007 * (val1 / 2)) % 1000000007 +
((val * b) % 1000000007 * val1) % 1000000007) %
1000000007) %
1000000007;
else
ans = (ans +
(((val * (val1)) % 1000000007 * ((val1 + 1) / 2)) % 1000000007 +
((val * b) % 1000000007 * val1) % 1000000007) %
1000000007) %
1000000007;
}
if (flag == 1) break;
}
val = 0;
if (flag == 1) {
if ((i ^ j) == 1) {
if (a <= k - 1) {
val1 = k - 1 - a;
val = fun((x - 1) % a + 1, (y - 1) % a + 1, a / 2, val1 + 1,
(l + a) % 1000000007);
}
} else {
val1 = k - 1;
val = fun((x - 1) % a + 1, (y - 1) % a + 1, a / 2, val1 + 1, l);
}
}
return (ans + val) % 1000000007;
}
int main() {
long long int q, x1, x2, y1, y2, k, ans, xmin, xmax, ymin, ymax, x;
scanf("%lld", &q);
while (q--) {
scanf("%lld%lld%lld%lld%lld", &x1, &y1, &x2, &y2, &k);
xmin = min(x1, x2);
xmax = max(x1, x2);
ymin = min(y1, y2);
ymax = max(y1, y2);
x = pow(2ll, 30ll);
ans = (fun(xmax, ymax, x, k, 0) - fun(xmin - 1, ymax, x, k, 0) -
fun(xmax, ymin - 1, x, k, 0) + fun(xmin - 1, ymin - 1, x, k, 0) +
2 * 1000000007) %
1000000007;
printf("%lld\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int UP(int x) {
int l = 0, r = 32;
int ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (!(x >> mid)) {
ans = mid;
r = mid - 1;
} else
l = mid + 1;
}
return ans;
}
int qury(int x, int y, int k, int va) {
if (k <= 0) return 0;
if (x == 0 || y == 0) return 0;
int up1 = UP(x) - 1;
int up2 = UP(y) - 1;
int ret = 0;
int n = min(k, max(1 << up1, 1 << up2));
ret = ((1LL * n * (n + 1) / 2) % mod * min(1 << up1, 1 << up2)) % mod;
ret = (ret + (1LL * n * min(1 << up1, 1 << up2) % mod * va % mod)) % mod;
if (up1 >= up2) {
if (k - (1 << up1) > 0)
ret = (ret +
qury(x - (1 << up1), 1 << up2, k - (1 << up1), va + (1 << up1))) %
mod;
} else {
ret = (ret + (1LL * n * (n + 1) / 2) % mod * (x - (1 << up1)) % mod) % mod;
ret = (ret + (1LL * n * (x - (1 << up1)) % mod * va % mod)) % mod;
}
if (up2 >= up1) {
if (k - (1 << up2) > 0)
ret = (ret +
qury(1 << up1, y - (1 << up2), k - (1 << up2), va + (1 << up2))) %
mod;
} else {
ret = (ret + (1LL * n * (n + 1) / 2) % mod * (y - (1 << up2)) % mod) % mod;
ret = (ret + (1LL * n * (y - (1 << up2)) % mod * va % mod)) % mod;
}
if (up1 == up2)
ret = (ret + qury(x - (1 << up1), y - (1 << up2), k, va)) % mod;
else if (up1 > up2) {
int tmp =
((qury(x - (1 << up1), y, k - (1 << up1), va + (1 << up1)) -
qury(x - (1 << up1), 1 << up2, k - (1 << up1), va + (1 << up1))) %
mod +
mod) %
mod;
ret = (ret + tmp) % mod;
} else {
int tmp =
(((qury(x, y - (1 << up2), k - (1 << up2), va + (1 << up2))) -
qury((1 << up1), y - (1 << up2), k - (1 << up2), va + (1 << up2))) %
mod +
mod) %
mod;
ret = (ret + tmp) % mod;
}
return ret;
}
int main() {
int x1, y1, x2, y2;
int k;
int n;
scanf("%d", &n);
while (n--) {
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
int q1 = qury(x1 - 1, y1 - 1, k, 0);
int q2 = qury(x2, y1 - 1, k, 0);
int q3 = qury(x1 - 1, y2, k, 0);
int q4 = qury(x2, y2, k, 0);
int ans = (((q4 + q1) % mod - (q2 + q3) % mod) % mod + mod) % mod;
printf("%d\n", ans);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
char getc() {
char c = getchar();
while ((c < 'A' || c > 'Z') && (c < 'a' || c > 'z') && (c < '0' || c > '9'))
c = getchar();
return c;
}
int gcd(int n, int m) { return m == 0 ? n : gcd(m, n % m); }
int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return x * f;
}
struct data {
int x, y, k;
bool operator<(const data& a) const {
return x < a.x || x == a.x && y < a.y || x == a.x && y == a.y && k < a.k;
}
bool operator==(const data& a) const {
return x == a.x && y == a.y && k == a.k;
}
};
map<data, pair<int, int> > f;
pair<int, int> operator+(pair<int, int> a, pair<int, int> b) {
return make_pair(((a.first + b.first) % 1000000007),
((a.second + b.second) % 1000000007));
}
pair<int, int> calc(int x, int y, int k) {
if (x == 0 || y == 0 || k <= 0) return make_pair((0), (0));
if (x == 1 && y == 1) return make_pair((1), (1));
k = min(k, x + y - 1);
if (x > y) swap(x, y);
data qwq = (data){x, y, k};
if (f.find(qwq) != f.end()) return f[qwq];
int u = 1;
while ((u << 1) < y) u <<= 1;
pair<int, int> ans;
if (x <= u) {
ans = calc(x, u, k);
pair<int, int> tmp = calc(x, y - u, k - u);
ans = ans + tmp;
ans.first = (ans.first + 1ll * tmp.second * u) % 1000000007;
} else {
ans = calc(u, u, k);
ans = ans + calc(x - u, y - u, k);
pair<int, int> tmp = calc(x - u, u, k - u);
ans = ans + tmp;
ans.first = (ans.first + 1ll * tmp.second * u) % 1000000007;
tmp = calc(u, y - u, k - u);
ans = ans + tmp;
ans.first = (ans.first + 1ll * tmp.second * u) % 1000000007;
}
return f[qwq] = ans;
}
int bruteforce(int x1, int y1, int x2, int y2, int k) {
int ans = 0;
for (int i = x1; i <= x2; i++)
for (int j = y1; j <= y2; j++)
if ((i - 1 ^ j - 1) + 1 <= k)
ans = (ans + (i - 1 ^ j - 1) + 1) % 1000000007;
return ans;
}
signed main() {
int q = read();
while (q--) {
int x1 = read(), y1 = read(), x2 = read(), y2 = read(), k = read();
int ans = 0;
ans = (ans + calc(x2, y2, k).first) % 1000000007;
ans = (ans - calc(x1 - 1, y2, k).first + 1000000007) % 1000000007;
ans = (ans - calc(x2, y1 - 1, k).first + 1000000007) % 1000000007;
ans = (ans + calc(x1 - 1, y1 - 1, k).first) % 1000000007;
printf("%d\n", ans);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int K;
struct square {
int x1, y1, x2, y2;
square() {}
square(int a, int b, int c, int d) {
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
bool is_valid() {
if (x1 > x2)
return false;
else if (y1 > y2)
return false;
else
return true;
}
bool is_equal(square s) {
return ((x1 == s.x1) && (y1 == s.y1) && (x2 == s.x2) && (y2 == s.y2));
}
int x_range() { return x2 - x1 + 1; }
int y_range() { return y2 - y1 + 1; }
square apply_bias(int bx, int by) {
return square(x1 - bx, y1 - by, x2 - bx, y2 - by);
}
};
int calc_quest(square target, int binary_pivot, int bias) {
if (!target.is_valid()) return 0;
if (K <= bias) return 0;
if (target.x_range() == binary_pivot && target.y_range() == binary_pivot) {
long long bound = (long long)min(binary_pivot + bias, K);
long long lbias = (long long)bias;
long long temp;
temp = (lbias + 1ll + bound) * (bound - lbias) / 2ll;
temp %= 1000000007;
temp *= (long long)binary_pivot;
temp %= 1000000007;
return (int)temp;
}
if (target.x_range() == binary_pivot) {
long long bound = (long long)min(binary_pivot + bias, K);
long long lbias = (long long)bias;
long long temp;
temp = (lbias + 1ll + bound) * (bound - lbias) / 2ll;
temp %= 1000000007;
temp *= (long long)target.y_range();
temp %= 1000000007;
return (int)temp;
}
if (target.y_range() == binary_pivot) {
long long bound = (long long)min(binary_pivot + bias, K);
long long lbias = (long long)bias;
long long temp;
temp = (lbias + 1ll + bound) * (bound - lbias) / 2ll;
temp %= 1000000007;
temp *= (long long)target.x_range();
temp %= 1000000007;
return (int)temp;
}
int ans = 0;
int new_binary_pivot = binary_pivot / 2;
int x_bound, y_bound;
x_bound = min(target.x2, new_binary_pivot);
y_bound = min(target.y2, new_binary_pivot);
ans += calc_quest(square(target.x1, target.y1, x_bound, y_bound),
new_binary_pivot, bias);
y_bound = max(target.y1, new_binary_pivot + 1);
ans += calc_quest(square(target.x1, y_bound, x_bound, target.y2)
.apply_bias(0, new_binary_pivot),
new_binary_pivot, bias + new_binary_pivot);
ans %= 1000000007;
x_bound = max(target.x1, new_binary_pivot + 1);
ans += calc_quest(square(x_bound, y_bound, target.x2, target.y2)
.apply_bias(new_binary_pivot, new_binary_pivot),
new_binary_pivot, bias);
ans %= 1000000007;
y_bound = min(target.y2, new_binary_pivot);
ans += calc_quest(square(x_bound, target.y1, target.x2, y_bound)
.apply_bias(new_binary_pivot, 0),
new_binary_pivot, bias + new_binary_pivot);
ans %= 1000000007;
return ans;
}
int main() {
int Q;
int x1, y1, x2, y2;
scanf("%d", &Q);
for (int i = 0; i < Q; i++) {
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &K);
square target = square(x1, y1, x2, y2);
int temp = max(x2, y2);
int bound = 1;
while (temp > bound) bound <<= 1;
printf("%d\n", calc_quest(target, bound, 0));
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MAX_N = 35;
const long long MOD = 1000000007LL;
long long dp[MAX_N][2][2][2][2];
int query, k;
int a[MAX_N], b[MAX_N], c[MAX_N];
void inc(long long &X, long long val) { (X += val) %= MOD; }
long long calc(int x, int y) {
if (x == 0 || y == 0) {
return 0LL;
}
long long res = 0LL;
memset(dp, 0, sizeof(dp));
int xx = x - 1;
int yy = y - 1;
int kk = k - 1;
for (int i = 0; i < 32; i++) {
a[i] = xx >> i & 1;
b[i] = yy >> i & 1;
c[i] = kk >> i & 1;
}
dp[31][1][1][1][1] = 1;
for (int i = 31; i >= 1; i--) {
for (int flagX = 0; flagX < 2; flagX++) {
for (int flagY = 0; flagY < 2; flagY++) {
for (int flagK = 0; flagK < 2; flagK++) {
if (dp[i][flagX][flagY][flagK][1] > 0) {
for (int f1 = 0; f1 < 2; f1++)
for (int f2 = 0; f2 < 2; f2++) {
if (flagX == 1 && f1 > a[i - 1] ||
flagY == 1 && f2 > b[i - 1] ||
flagK == 1 && ((f1 ^ f2) > c[i - 1])) {
continue;
}
inc(dp[i - 1][flagX == 1 && f1 == a[i - 1]]
[flagY == 1 && f2 == b[i - 1]]
[flagK == 1 && ((f1 ^ f2) == c[i - 1])][1],
dp[i][flagX][flagY][flagK][1]);
inc(dp[i - 1][flagX == 1 && f1 == a[i - 1]]
[flagY == 1 && f2 == b[i - 1]]
[flagK == 1 && ((f1 ^ f2) == c[i - 1])][0],
(dp[i][flagX][flagY][flagK][0] * 2ll +
dp[i][flagX][flagY][flagK][1] * (f1 ^ f2)) %
MOD);
}
}
}
}
}
}
for (int flagX = 0; flagX < 2; flagX++) {
for (int flagY = 0; flagY < 2; flagY++) {
for (int flagK = 0; flagK < 2; flagK++) {
(res +=
dp[0][flagX][flagY][flagK][0] + dp[0][flagX][flagY][flagK][1]) %= MOD;
}
}
}
return res;
}
int main() {
scanf("%d", &query);
while (query--) {
int x1, y1, x2, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
scanf("%d", &k);
printf("%I64d\n", (calc(x2, y2) - calc(x1 - 1, y2) - calc(x2, y1 - 1) +
calc(x1 - 1, y1 - 1) + 2LL * MOD) %
MOD);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int bitx[32], bity[32], bitk[32];
long long dp[32][2][2][2], dp1[32][2][2][2], f[32];
pair<long long, long long> dfs(int pos, int limit1, int limit2, int limit3) {
if (pos == 0) return make_pair(1, 0);
if (dp[pos][limit1][limit2][limit3] != -1)
return make_pair(dp[pos][limit1][limit2][limit3],
dp1[pos][limit1][limit2][limit3]);
int x = limit1 ? bitx[pos] : 1;
int y = limit2 ? bity[pos] : 1;
long long ret = 0;
long long ret1 = 0;
for (int i = 0; i <= x; ++i) {
for (int j = 0; j <= y; ++j) {
if (limit3 && (i ^ j) > bitk[pos]) continue;
pair<long long, long long> t =
dfs(pos - 1, limit1 && (i == x), limit2 && (j == y),
limit3 && ((i ^ j) == bitk[pos]));
(ret += t.first) %= mod;
(ret1 += t.second + ((i ^ j) ? f[pos - 1] : 0) * t.first % mod) %= mod;
}
}
dp[pos][limit1][limit2][limit3] = ret,
dp1[pos][limit1][limit2][limit3] = ret1;
return make_pair(ret, ret1);
}
long long solve(long long x, long long y, long long k) {
memset(dp, -1, sizeof(dp));
if (x < 0 || y < 0) return 0;
memset(bitx, 0, sizeof(bitx));
memset(bity, 0, sizeof(bity));
memset(bitk, 0, sizeof(bitk));
int lenx = 0;
while (x) {
bitx[++lenx] = x & 1;
x >>= 1;
}
int leny = 0;
while (y) {
bity[++leny] = y & 1;
y >>= 1;
}
int lenk = 0;
while (k) {
bitk[++lenk] = k & 1;
k >>= 1;
}
pair<long long, long long> t = dfs(31, 1, 1, 1);
return (t.first + t.second) % mod;
}
int main() {
f[0] = 1;
for (int i = 1; i < 31; ++i) f[i] = f[i - 1] * 2 % mod;
int T, x1, y1, x2, y2, k;
scanf("%d", &T);
memset(dp, -1, sizeof(dp));
while (T--) {
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
x1--;
y1--;
x2--;
y2--;
k--;
printf("%I64d\n", ((solve(x2, y2, k) - solve(x1 - 1, y2, k) -
solve(x2, y1 - 1, k) + solve(x1 - 1, y1 - 1, k)) %
mod +
mod) %
mod);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int N = 300000 + 7;
const int M = 20;
const int inf = 1000000007;
const long long linf = 1e18 + 7;
const double pi = acos(-1);
const double eps = 1e-9;
const bool multipleTest = 0;
int a[M][M];
int limit;
long long sum[30];
int mn[30];
inline long long calc(int u0, int v0, int u1, int v1, int k, long long added) {
if (added + 1 > limit) return 0;
long long ans = 0;
int len = 1 << k;
if (u1 - u0 + 1 == len) {
int r = min(limit - added, 1ll * len);
ans = (((1ll * r * (r + 1) / 2)) % inf) * (v1 - v0 + 1);
ans = (ans + 1ll * r * (v1 - v0 + 1) % inf * added) % inf;
return ans;
}
if (v1 - v0 + 1 == len) {
int r = min(limit - added, 1ll * len);
ans = (((1ll * r * (r + 1) / 2)) % inf) * (u1 - u0 + 1);
ans = (ans + 1ll * r * (u1 - u0 + 1) % inf * added) % inf;
return ans;
}
if (k < 1) {
for (int i = u0; i <= u1; ++i) {
for (int j = v0; j <= v1; ++j) {
if (a[i][j] + added <= limit) {
ans += (a[i][j] + added);
}
}
}
return ans % inf;
}
int tmp = 1 << (k - 1);
while (k > 1 && tmp > max(u1, v1)) {
--k;
tmp >>= 1;
}
int x = tmp;
int y = tmp;
int ss = 1 << k;
if (u0 < x && v0 < y)
ans = (ans + calc(u0, v0, min(u1, x - 1), min(v1, y - 1), k - 1, added));
if (u1 >= x && v1 >= y)
ans = (ans +
calc(max(x, u0) - x, max(y, v0) - y, u1 - x, v1 - y, k - 1, added));
if (u0 < x && v1 >= y) {
ans = (ans + calc(u0, max(y, v0) - y, min(x - 1, u1), v1 - y, k - 1,
added + tmp));
}
if (u1 >= x && v0 < y) {
ans = (ans + calc(max(u0, x) - x, v0, u1 - x, min(v1, y - 1), k - 1,
added + tmp));
}
ans %= inf;
return ans;
}
void solve() {
a[0][0] = 1;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < M; ++j) {
if (i == 0 && j == 0) continue;
set<int> list;
for (int t = 0; t < i; ++t) list.insert(a[t][j]);
for (int t = 0; t < j; ++t) list.insert(a[i][t]);
int cur = 1;
for (int x : list) {
if (cur == x)
++cur;
else
break;
}
a[i][j] = cur;
}
}
sum[0] = 1;
mn[0] = 1;
for (int i = 1; i <= 30; ++i) {
long long added = 1 << (i - 1);
sum[i] = (4ll * sum[i - 1] + 2ll * added) % inf;
}
int q;
cin >> q;
while (q-- > 0) {
int u0, v0, u1, v1;
scanf("%d%d%d%d%d", &u0, &v0, &u1, &v1, &limit);
u0--;
v0--;
u1--;
v1--;
cout << calc(u0, v0, u1, v1, 30, 0) << '\n';
}
}
int main() {
int Test = 1;
if (multipleTest) {
cin >> Test;
}
for (int i = 0; i < Test; ++i) {
solve();
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Comparator;
import java.util.StringTokenizer;
public class Test{
public static void main(String args[]){
Reader in = new Reader();
Writer out = new Writer();
Query solver = new Query();
int t = in.nextInt();
for(int cs = 1; cs <= t; cs++){
int p,q,x,y,k;
p = in.nextInt();
q = in.nextInt();
x = in.nextInt();
y = in.nextInt();
k = in.nextInt();
out.writeln(solver.ask(p,q,x,y,k));
}
out.flush();
}
static class Query{
int [][][][] cnt = new int [32][2][2][2];
int [][][][] dp = new int [32][2][2][2];
final int bit = 32;
final int mod = 1000000000+7;
int k,x,y;
int getBit(int y, int x){
return (y>>x)&1;
}
int count(int cur, int smX, int smY, int smK){
if(cur<0){
return 1;
}
if(cnt[cur][smX][smY][smK]!=-1) return cnt[cur][smX][smY][smK];
int ans = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j <2; j++){
int pi = i^j;
if(smX == 0 && i > getBit(x, cur)) continue;
if(smY == 0 && j > getBit(y, cur)) continue;
if(smK == 0 && pi > getBit(k, cur)) continue;
int smx = smX;
int smy = smY;
int smk = smK;
if(smx == 0 && i < getBit(x, cur)) smx = 1;
if(smy == 0 && j < getBit(y, cur)) smy = 1;
if(smk == 0 && pi < getBit(k, cur)) smk = 1;
ans+=count(cur-1,smx,smy,smk);
ans%=mod;
}
}
return cnt[cur][smX][smY][smK] = ans;
}
int dynamic(int cur, int smX, int smY, int smK){
if(cur<0){
return 0;
}
if(dp[cur][smX][smY][smK]!=-1) return dp[cur][smX][smY][smK];
long ans = 0;
for(int i = 0; i < 2; i++){
for(int j = 0; j < 2; j++){
int pi = i^j;
if(smX == 0 && i > getBit(x, cur)) continue;
if(smY == 0 && j > getBit(y, cur)) continue;
if(smK == 0 && pi > getBit(k, cur)) continue;
int smx = smX;
int smy = smY;
int smk = smK;
if(smx == 0 && i < getBit(x, cur)) smx = 1;
if(smy == 0 && j < getBit(y, cur)) smy = 1;
if(smk == 0 && pi < getBit(k, cur)) smk = 1;
if(pi == 1) ans += 1L * (1 << cur)*count(cur-1,smx,smy,smk);
ans+=dynamic(cur-1,smx,smy,smk);
ans%=mod;
}
}
return dp[cur][smX][smY][smK] = (int)ans;
}
void init(){
for(int i = 0; i < bit; i++){
for(int j = 0; j < 2; j++){
for(int p = 0; p < 2; p++){
for(int l = 0; l < 2; l++){
cnt[i][j][p][l] = -1;
dp[i][j][p][l] = -1;
}
}
}
}
}
int brute(int x, int y, int k){
int ans = 0;
for(int i = 0; i < x; i++){
for(int j = 0; j < y; j++){
if((i^j) <= k-1) ans += 1;
}
}
return ans;
}
int ask(int x, int y, int k){
if(x == 0 || y == 0) return 0;
this.x = x - 1;
this.y = y - 1;
this.k = k - 1;
init();
return (dynamic(bit - 1, 0, 0, 0) + count(bit - 1, 0, 0, 0))% mod;
}
int ask(int p, int q, int x, int y, int k){
long ans = 0;
ans += ask(x,y,k);
ans -= ask(x,q-1,k);
ans -= ask(p-1,y,k);
ans += ask(p-1,q-1,k);
ans%=mod;
if(ans<0) ans+=mod;
return (int)ans;
}
}
static class Reader{
private StringTokenizer a;
private BufferedReader b;
Reader(){
a = null;
b = new BufferedReader(new InputStreamReader(System.in));
}
public String next(){
while(a == null || !a.hasMoreTokens()){
try{
a = new StringTokenizer(b.readLine());
} catch(IOException e){
e.printStackTrace();
}
}
return a.nextToken();
}
public int nextInt(){
return Integer.parseInt(this.next());
}
public long nextLong(){
return Long.parseLong(this.next());
}
public double nextDouble(){
return Double.parseDouble(this.next());
}
public String nextLine(){
try{
return b.readLine();
}
catch(IOException e){
e.printStackTrace();
}
return "";
}
}
static class Writer{
private PrintWriter a;
private StringBuffer b;
Writer(){
a = new PrintWriter(System.out);
b = new StringBuffer("");
}
public void write(Object s){
b.append(s);
}
public void writeln(Object s){
b.append(s).append('\n');
}
public void flush(){
a.print(b);
a.flush();
a.close();
}
}
static class Pair implements Comparator <Pair>{
int first;
int second;
Pair(int a, int b){
this.first = a;
this.second = b;
}
Pair(Pair a){
this.first = a.first;
this.second = a.second;
}
Pair(){}
public String toString(){
return "["+first+","+second+"]";
}
public int compare (Pair a, Pair b){
if(a.first == b.first){
return a.second-b.second;
}else{
return a.first - b.first;
}
}
}
} | JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool upmin(T &x, T y) {
return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool upmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
const long double eps = 1e-11;
const long double pi = acos(-1);
const int oo = 1 << 30;
const long long loo = 1ll << 62;
const int mods = 1e9 + 7;
const int MAXN = 1000005;
const int INF = 0x3f3f3f3f;
inline int read() {
int f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
int f[32][2][2][2], g[32][2][2][2];
int upd(int x, int y) { return x + y >= mods ? x + y - mods : x + y; }
long long get(int X, int Y, int k) {
if (X < 0 || Y < 0 || k < 0) return 0;
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
f[31][1][1][1] = 1;
g[31][1][1][1] = 0;
for (int i = 31; i >= 1; i--)
for (int j = 0; j < 32; j++) {
int xi = (X >> (i - 1)) & 1, yi = (Y >> (i - 1)) & 1,
ki = (k >> (i - 1)) & 1;
int p = (j >> 4) & 1, q = (j >> 3) & 1, x = (j >> 2) & 1,
y = (j >> 1) & 1, t = j & 1;
if (x && p && !xi) continue;
if (y && q && !yi) continue;
if ((x ^ y) && t && !ki) continue;
int _p = p & (xi == x), _q = q & (yi == y), _t = t & (ki == x ^ y);
f[i - 1][_p][_q][_t] = upd(f[i - 1][_p][_q][_t], f[i][p][q][t]);
g[i - 1][_p][_q][_t] = upd(
g[i - 1][_p][_q][_t],
(1ll * f[i][p][q][t] * ((x ^ y) << (i - 1)) + g[i][p][q][t]) % mods);
}
long long ans = 0;
for (int p = 0; p <= 1; p++)
for (int q = 0; q <= 1; q++)
for (int t = 0; t <= 1; t++) ans += f[0][p][q][t] + g[0][p][q][t];
return ans % mods;
}
signed main() {
int Case = read();
while (Case--) {
int x1 = read() - 1, y1 = read() - 1, x2 = read() - 1, y2 = read() - 1,
k = read() - 1;
printf("%lld\n", (mods + mods + get(x2, y2, k) - get(x1 - 1, y2, k) -
get(x2, y1 - 1, k) + get(x1 - 1, y1 - 1, k)) %
mods);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | //package round415;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class C2 {
InputStream is;
PrintWriter out;
String INPUT = "";
int mod = 1000000007;
void solve()
{
for(int T = ni();T > 0;T--){
cache = new LongHashMap();
int x1 = ni(), y1 = ni(), x2 = ni(), y2 = ni(), K = ni();
long ret =
count(x2, y2, 29, K)[0]
- count(x1-1, y2, 29, K)[0]
- count(x2, y1-1, 29, K)[0]
+ count(x1-1, y1-1, 29, K)[0];
ret %= mod;
if(ret < 0)ret += mod;
out.println(ret);
}
}
static final long[] Z = new long[]{0L, 0L};
static long[][] zs = new long[40][];
LongHashMap cache = new LongHashMap();
long[] count(int r, int c, int d, int K)
{
if(r <= 0 || c <= 0 || K <= 0)return Z;
if(r == 1<<d+1 && c == 1<<d+1 && K >= 1<<d+1){
if(zs[d+1] == null){
long u = 1L<<d+1;
zs[d+1] = new long[]{u*(u+1)/2%mod*u%mod, u*u%mod};
}
return zs[d+1];
}
long code = (long)r*1000000009 + c;
code = code * 1000000009L + d;
code = code * 1000000009L + K;
if(cache.containsKey(code))return (long[])cache.get(code);
if(r == 1<<d+1 && c == 1<<d+1){
long u = 1L<<d+1;
long[] res = new long[]{(long)K*(K+1)/2%mod*u%mod, K*u%mod};
cache.put(code, res);
return res;
}
// tr(r, c, d, K);
// 1<<d
long ret = 0;
long num = 0;
{
long[] res = count(Math.min(r, 1<<d), Math.min(c, 1<<d), d-1, Math.min(K, 1<<d+1));
ret += res[0];
num += res[1];
}
{
long[] res = count(Math.min(r, 1<<d), c-(1<<d), d-1, K-(1<<d));
ret += res[0] + res[1] * (1L<<d);
num += res[1];
}
{
long[] res = count(r-(1<<d), Math.min(c, 1<<d), d-1, K-(1<<d));
ret += res[0] + res[1] * (1L<<d);
num += res[1];
}
{
long[] res = count(r-(1<<d), c-(1<<d), d-1, Math.min(K, 1<<d+1));
ret += res[0];
num += res[1];
}
long[] re = new long[]{ret%mod, num%mod};
cache.put(code, re);
return re;
}
public static class LongHashMap {
public long[] keys;
public Object[] allocated;
private int scale = 1<<2;
private int rscale = 1<<1;
private int mask = scale-1;
public int size = 0;
public LongHashMap(){
allocated = new Object[scale];
keys = new long[scale];
}
public boolean containsKey(long x)
{
int pos = h(x)&mask;
while(allocated[pos] != null){
if(x == keys[pos])return true;
pos = pos+1&mask;
}
return false;
}
public Object get(long x)
{
int pos = h(x)&mask;
while(allocated[pos] != null){
if(x == keys[pos])return allocated[pos];
pos = pos+1&mask;
}
return null;
}
public Object put(long x, Object v)
{
int pos = h(x)&mask;
while(allocated[pos] != null){
if(x == keys[pos]){
Object oldval = allocated[pos];
allocated[pos] = v;
return oldval;
}
pos = pos+1&mask;
}
if(size == rscale){
resizeAndPut(x, v);
}else{
keys[pos] = x;
allocated[pos] = v;
}
size++;
return null;
}
public boolean remove(long x)
{
int pos = h(x)&mask;
while(allocated[pos] != null){
if(x == keys[pos]){
size--;
// take last and fill rmpos
int last = pos;
pos = pos+1&mask;
while(allocated[pos] != null){
int lh = h(keys[pos])&mask;
// lh <= last < pos
if(
lh <= last && last < pos ||
pos < lh && lh <= last ||
last < pos && pos < lh
){
keys[last] = keys[pos];
allocated[last] = allocated[pos];
last = pos;
}
pos = pos+1&mask;
}
keys[last] = 0;
allocated[last] = null;
return true;
}
pos = pos+1&mask;
}
return false;
}
private void resizeAndPut(long x, Object v)
{
int nscale = scale<<1;
int nrscale = rscale<<1;
int nmask = nscale-1;
Object[] nallocated = new Object[nscale];
long[] nkeys = new long[nscale];
for(int i = next(0);i < scale;i = next(i+1)){
long y = keys[i];
int pos = h(y)&nmask;
while(nallocated[pos] != null)pos = pos+1&nmask;
nkeys[pos] = y;
nallocated[pos] = allocated[i];
}
{
int pos = h(x)&nmask;
while(nallocated[pos] != null)pos = pos+1&nmask;
nkeys[pos] = x;
nallocated[pos] = v;
}
allocated = nallocated;
keys = nkeys;
scale = nscale;
rscale = nrscale;
mask = nmask;
}
public int next(int itr)
{
while(itr < scale && allocated[itr] == null)itr++;
return itr;
}
private int h(long x)
{
x ^= x>>>33;
x *= 0xff51afd7ed558ccdL;
x ^= x>>>33;
x *= 0xc4ceb9fe1a85ec53L;
x ^= x>>>33;
return (int)x;
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
for(int i = next(0);i < scale;i = next(i+1)){
sb.append(",");
sb.append(keys[i] + ":" + allocated[i]);
}
return sb.length() == 0 ? "" : sb.substring(1);
}
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new C2().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
pair<long long, long long> que_row(int y, int lvl, int k) {
long long top = min(k, (1 << lvl));
return {(y + 0LL) * (top * (top + 1) / 2 % 1000000007LL) % 1000000007LL,
(y + 0LL) * top % 1000000007LL};
}
pair<long long, long long> que(int x, int y, int lvl, int k) {
if (k <= 0 || x <= 0 || y <= 0) return {0, 0};
if (x == (1 << lvl)) {
return que_row(y, lvl, k);
}
if (y == (1 << lvl)) {
return que_row(x, lvl, k);
}
int mid = 1 << (lvl - 1);
auto tl = que(min(x, mid), min(y, mid), lvl - 1, k);
auto tr = que(min(x, mid), y - mid, lvl - 1, k - mid);
auto bl = que(x - mid, min(y, mid), lvl - 1, k - mid);
auto br = que(x - mid, y - mid, lvl - 1, k);
return {(tl.first + tr.first + bl.first + br.first +
mid * (tr.second + bl.second)) %
1000000007LL,
(tl.second + tr.second + bl.second + br.second) % 1000000007LL};
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
int x1, y1, x2, y2, k;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
x1--, y1--;
long long ans = que(x2, y2, 30, k).first - que(x1, y2, 30, k).first -
que(x2, y1, 30, k).first + que(x1, y1, 30, k).first;
ans %= 1000000007LL;
if (ans < 0) ans += 1000000007LL;
printf("%lld\n", ans);
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const long long l = 31;
const long long mod = 1e9 + 7;
void add(long long& a, long long b) { a = (a + b) % mod; }
long long solve(long long x, long long y, long long k) {
if (min({x, y, k}) < 0) return 0;
vector<vector<vector<vector<long long> > > > cnt(
2, vector<vector<vector<long long> > >(
2, vector<vector<long long> >(2, vector<long long>(l + 1, 0))));
vector<vector<vector<vector<long long> > > > dp(
2, vector<vector<vector<long long> > >(
2, vector<vector<long long> >(2, vector<long long>(l + 1, 0))));
cnt[1][1][1][l] = 1;
for (long long i = l; i > 0; i--) {
for (int eqx = 0; eqx < 2; eqx++) {
for (int eqy = 0; eqy < 2; eqy++) {
for (int eqk = 0; eqk < 2; eqk++) {
for (long long xi = 0; xi < 2; xi++) {
for (long long yi = 0; yi < 2; yi++) {
bool ox = (x & (1 << (i - 1))), oy = (y & (1 << (i - 1))),
ok = (k & (1 << (i - 1)));
if (eqx && xi && !ox) continue;
if (eqy && yi && !oy) continue;
if (eqk && (xi ^ yi) && !ok) continue;
bool nwx = (ox == xi ? eqx : 0);
bool nwy = (oy == yi ? eqy : 0);
bool nwk = (ok == (xi ^ yi) ? eqk : 0);
long long nwval =
(cnt[eqx][eqy][eqk][i] * (((xi ^ yi) << (i - 1)))) % mod;
add(dp[nwx][nwy][nwk][i - 1], dp[eqx][eqy][eqk][i] + nwval);
add(cnt[nwx][nwy][nwk][i - 1], cnt[eqx][eqy][eqk][i]);
}
}
}
}
}
}
long long ans = 0;
for (int eqx = 0; eqx < 2; eqx++) {
for (int eqy = 0; eqy < 2; eqy++) {
for (int eqk = 0; eqk < 2; eqk++) {
add(ans, dp[eqx][eqy][eqk][0] + cnt[eqx][eqy][eqk][0]);
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int q;
cin >> q;
while (q--) {
long long x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
long long ans22 = solve(x2 - 1, y2 - 1, k - 1);
long long ans12 = solve(x1 - 2, y2 - 1, k - 1);
long long ans21 = solve(x2 - 1, y1 - 2, k - 1);
long long ans11 = solve(x1 - 2, y1 - 2, k - 1);
cout << (mod * 2 + ans22 - ans12 - ans21 + ans11) % mod << "\n";
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
int psum(long long v) {
long long ans = (v * (v + 1000000007 - 1)) % 1000000007;
ans = (ans * 500000004LL) % 1000000007;
return (int)ans;
}
pair<long long, long long> getDP(int x, int y, int k) {
if (x == 0 || y == 0 || k == 0) return make_pair(0, 0);
if (x == 1) {
int low = min(y, k);
return make_pair(low, psum(low));
}
if (y == 1) {
int low = min(x, k);
return make_pair(low, psum(low));
}
if (k == 1) {
int low = min(x, y);
return make_pair(low, low);
}
pair<long long, long long> v1 = getDP((x + 1) / 2, (y + 1) / 2, (k + 1) / 2);
pair<long long, long long> v2 = getDP(x / 2, (y + 1) / 2, k / 2);
pair<long long, long long> v3 = getDP((x + 1) / 2, y / 2, k / 2);
pair<long long, long long> v4 = getDP(x / 2, y / 2, (k + 1) / 2);
int num = (((long long)v1.first) + ((long long)v2.first) +
((long long)v3.first) + ((long long)v4.first)) %
1000000007;
int ans = (2LL * v1.second + 1000000007 - v1.first) % 1000000007;
ans = (ans + 2LL * v4.second + 1000000007 - v4.first) % 1000000007;
ans = (ans + 2LL * v2.second + 2LL * v3.second) % 1000000007;
return make_pair(num, ans);
}
int mask[31];
pair<long long, long long> getWays(int x, int y, int k) {
if (x == 0 || y == 0 || k == 0) return make_pair(0, 0);
int totNum = 0;
int totSum = 0;
for (int t = 0; t < 2; t++) {
for (int i = 30; i >= 0; i--)
if (x & (1 << i)) {
x ^= (1 << i);
for (int j = i - 1; j >= 0; j--)
if (y & (1 << j)) {
y ^= (1 << j);
if (((x ^ y) >> i) < (k >> i)) {
long long cnum = ((1LL << i) * (1LL << j)) % 1000000007;
totNum = (totNum + cnum) % 1000000007;
totSum = (totSum + (1LL << j) * psum(1LL << i)) % 1000000007;
totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007;
} else if (((x ^ y) >> i) == (k >> i)) {
long long lowk = (k & ((1 << i) - 1));
long long cnum = (lowk * (1LL << j)) % 1000000007;
totNum = (totNum + cnum) % 1000000007;
totSum = (totSum + (1LL << j) * psum(lowk)) % 1000000007;
totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007;
}
y ^= (1 << j);
}
x ^= (1 << i);
}
swap(x, y);
}
for (int i = 30; i >= 0; i--)
if (x & (1 << i))
if (y & (1 << i)) {
x ^= (1 << i);
y ^= (1 << i);
if (((x ^ y) >> i) < (k >> i)) {
long long cnum = ((1LL << i) * (1LL << i)) % 1000000007;
totNum = (totNum + cnum) % 1000000007;
totSum = (totSum + (1LL << i) * psum(1LL << i)) % 1000000007;
totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007;
} else if (((x ^ y) >> i) == (k >> i)) {
long long lowk = (k & ((1 << i) - 1));
long long cnum = (lowk * (1LL << i)) % 1000000007;
totNum = (totNum + cnum) % 1000000007;
totSum = (totSum + (1LL << i) * psum(lowk)) % 1000000007;
totSum = (totSum + (((x ^ y) >> i) << i) * cnum) % 1000000007;
}
x ^= (1 << i);
y ^= (1 << i);
}
return make_pair(totNum, (totNum + totSum) % 1000000007);
}
int main() {
int Q, x1, y1, x2, y2, k;
cin >> Q;
for (int i = 0; i < Q; i++) {
cin >> x1 >> y1 >> x2 >> y2 >> k;
int ans = getWays(x2, y2, k).second;
ans = (ans + 1000000007 - getWays(x2, y1 - 1, k).second) % 1000000007;
ans = (ans + 1000000007 - getWays(x1 - 1, y2, k).second) % 1000000007;
ans = (ans + getWays(x1 - 1, y1 - 1, k).second) % 1000000007;
cout << ans << '\n';
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x) { return x; }
template <typename... T>
int add(int x, T... y) {
x += add(y...);
if (x >= mod) x -= mod;
return x;
}
template <typename... T>
int udd(int &x, T... y) {
return x = add(x, y...);
}
template <typename... T>
int sub(int x, T... y) {
return add(x, mod - add(y...));
}
int mul(int x) { return x; }
template <typename... T>
int mul(int x, T... y) {
return 1ll * x * mul(y...) % mod;
}
int bin(int x, int to) {
int y = 1;
while (to) {
if (to & 1) y = mul(x, y);
x = mul(x, x);
to >>= 1;
}
return y;
}
int inv(int x) {
assert(x != 0);
return bin(x, mod - 2);
}
int bit(int mask, int i) {
assert(i >= 0);
return (mask >> i) & 1;
}
const int M = 40;
int d[M][2][2][2];
int h[M][2][2][2];
int fun(int A, int B, int K) {
for (int i = 0; i < M; ++i)
for (int ea = 0; ea < 2; ++ea)
for (int eb = 0; eb < 2; ++eb)
for (int ek = 0; ek < 2; ++ek) {
d[i][ea][eb][ek] = 0;
h[i][ea][eb][ek] = 0;
}
const int n = 31;
d[0][1][1][1] = 1;
for (int i = 0; i <= n; ++i) {
int no = n - i;
for (int ea = 0; ea < 2; ++ea)
for (int eb = 0; eb < 2; ++eb)
for (int ek = 0; ek < 2; ++ek) {
int cnt = d[i][ea][eb][ek];
int sum = h[i][ea][eb][ek];
for (int ba = 0; ba < 2; ++ba)
for (int bb = 0; bb < 2; ++bb) {
int bk = ba ^ bb;
if (ea && ba && !bit(A, no)) continue;
if (eb && bb && !bit(B, no)) continue;
if (ek && bk && !bit(K, no)) continue;
int na = ea & (ba == bit(A, no) ? 1 : 0);
int nb = eb & (bb == bit(B, no) ? 1 : 0);
int nk = ek & (bk == bit(K, no) ? 1 : 0);
udd(d[i + 1][na][nb][nk], cnt);
udd(h[i + 1][na][nb][nk],
add(sum, bk ? mul(cnt, bin(2, no)) : 0));
}
}
}
return add(h[n + 1][0][0][0], d[n + 1][0][0][0]);
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int xl, xr, yl, yr, k;
cin >> xl >> yl >> xr >> yr >> k;
--xl, --yl;
int ans = 0;
udd(ans, fun(xr, yr, k));
udd(ans, fun(xl, yl, k));
ans = sub(ans, fun(xr, yl, k));
ans = sub(ans, fun(xl, yr, k));
cout << ans << "\n";
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void print_array(T arr[], int size_arr) {
for (int i = (0); i < (size_arr); i++) {
cout << arr[i];
if (i == size_arr - 1)
cout << endl;
else
cout << " ";
}
}
template <typename T>
void print_vector(vector<T> v) {
for (int i = (0); i < (v.size()); i++) {
cout << v[i];
if (i == v.size() - 1)
cout << endl;
else
cout << " ";
}
}
template <typename T>
void memset_array(T arr[], T value, int size_arr) {
for (int i = (0); i < (size_arr); i++) {
arr[i] = value;
}
}
bool compare_lexical_string(string a, string b) {
for (int i = (0); i < (min(a.length(), b.length())); i++) {
if (a[i] != b[i]) return a[i] < b[i];
}
return a.length() < b.length();
}
struct lex_string {
bool operator()(string a, string b) { return compare_lexical_string(a, b); }
};
const int MOD = 1e9 + 7;
long long mul(long long a, long long b) { return (a * b) % MOD; }
long long add(long long a, long long b) { return (a + b) % MOD; }
pair<pair<long long, long long>, pair<long long, long long> > rec_intersection(
pair<pair<long long, long long>, pair<long long, long long> > a,
pair<pair<long long, long long>, pair<long long, long long> > b) {
return pair<pair<long long, long long>, pair<long long, long long> >(
pair<long long, long long>(max(a.first.first, b.first.first),
max(a.first.second, b.first.second)),
pair<long long, long long>(min(a.second.first, b.second.first),
min(a.second.second, b.second.second)));
}
pair<long long, long long> solve(long long x1, long long y1, long long x2,
long long y2, long long k, long long power) {
if (x1 > x2 || y1 > y2 || k <= 0) return pair<long long, long long>(0, 0);
if (x1 == 1 && x2 == power) {
long long max_val = min(k, power);
return pair<long long, long long>(
mul(y2 - y1 + 1, mul(max_val * (max_val + 1) / 2, 1)),
mul(max_val, y2 - y1 + 1));
}
if (y1 == 1 && y2 == power) {
long long max_val = min(k, power);
return pair<long long, long long>(
mul(x2 - x1 + 1, mul(max_val * (max_val + 1) / 2, 1)),
mul(max_val, x2 - x1 + 1));
}
pair<pair<long long, long long>, pair<long long, long long> > rec =
make_pair(make_pair(x1, y1), make_pair(x2, y2));
long long res = 0, count = 0;
pair<pair<long long, long long>, pair<long long, long long> > r1 =
rec_intersection(
rec, make_pair(make_pair(1, 1), make_pair(power / 2, power / 2)));
pair<long long, long long> left_up =
solve(r1.first.first, r1.first.second, r1.second.first, r1.second.second,
k, power / 2);
res = add(left_up.first, res);
count = add(left_up.second, count);
pair<pair<long long, long long>, pair<long long, long long> > r2 =
rec_intersection(rec, make_pair(make_pair(power / 2 + 1, 1),
make_pair(power, power / 2)));
r2.first.first -= power / 2;
r2.second.first -= power / 2;
pair<long long, long long> left_down =
solve(r2.first.first, r2.first.second, r2.second.first, r2.second.second,
k - power / 2, power / 2);
res = add(add(mul(left_down.second, power / 2), left_down.first), res);
count = add(left_down.second, count);
pair<pair<long long, long long>, pair<long long, long long> > r3 =
rec_intersection(rec, make_pair(make_pair(1, power / 2 + 1),
make_pair(power / 2, power)));
r3.first.second -= power / 2;
r3.second.second -= power / 2;
pair<long long, long long> right_up =
solve(r3.first.first, r3.first.second, r3.second.first, r3.second.second,
k - power / 2, power / 2);
res = add(add(mul(right_up.second, power / 2), right_up.first), res);
count = add(right_up.second, count);
pair<pair<long long, long long>, pair<long long, long long> > r4 =
rec_intersection(rec, make_pair(make_pair(power / 2 + 1, power / 2 + 1),
make_pair(power, power)));
r4.first.first -= power / 2;
r4.first.second -= power / 2;
r4.second.first -= power / 2;
r4.second.second -= power / 2;
pair<long long, long long> right_down =
solve(r4.first.first, r4.first.second, r4.second.first, r4.second.second,
k, power / 2);
res = add(right_down.first, res);
count = add(right_down.second, count);
return pair<long long, long long>(res, count);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
for (int test = (0); test < (q); test++) {
long long x1, y1, x2, y2, k;
cin >> x1 >> y1 >> x2 >> y2 >> k;
cout << solve(x1, y1, x2, y2, k, (long long)pow(2, 30)).first << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
pair<long long, long long> dp[32][2][2][2];
long long R, C, K;
pair<long long, long long> getways(int pos, int rcap, int ccap, int kcap) {
if (pos == -1) return {1, 0};
auto &ans = dp[pos][rcap][ccap][kcap];
if (ans.first != -1) return ans;
int rbit = ((R >> pos) & 1);
int cbit = ((C >> pos) & 1);
int kbit = ((K >> pos) & 1);
ans = getways(pos - 1, (rcap & (rbit == 0)), (ccap & (cbit == 0)),
(kcap & (kbit == 0)));
if ((ccap == 0 || cbit == 1) && (kcap == 0 || kbit == 1)) {
auto cways = getways(pos - 1, (rcap & (rbit == 0)), ccap, kcap);
ans.first += cways.first;
ans.second += cways.second + (((1LL << pos) * cways.first) % 1000000007);
}
if ((rcap == 0 || rbit == 1) && (kcap == 0 || kbit == 1)) {
auto cways = getways(pos - 1, rcap, (ccap & (cbit == 0)), kcap);
ans.first += cways.first;
ans.second += cways.second + (((1LL << pos) * cways.first) % 1000000007);
}
if ((rcap == 0 || rbit == 1) && (ccap == 0 || cbit == 1)) {
auto cways = getways(pos - 1, rcap, ccap, (kcap & (kbit == 0)));
ans.first += cways.first;
ans.second += cways.second;
}
ans.first %= 1000000007;
ans.second %= 1000000007;
return ans;
}
long long getsum(long long x, long long y, long long k) {
if (x < 0 || y < 0) return 0;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
for (int l = 0; l < 2; l++) {
dp[i][j][k][l].first = -1;
}
}
}
}
R = x;
C = y;
K = k;
auto ret = getways(31, 1, 1, 1);
return (ret.first + ret.second) % 1000000007;
}
int main() {
int q;
int x1, y1, x2, y2, k;
scanf("%d", &q);
while (q--) {
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &k);
k--;
x1--;
x2--;
y1--;
y2--;
printf("%Ld\n",
(getsum(x2, y2, k) + 1000000007 - getsum(x1 - 1, y2, k) +
1000000007 - getsum(x2, y1 - 1, k) + getsum(x1 - 1, y1 - 1, k)) %
1000000007);
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
long long dp[32][2][2][2], cnt[32][2][2][2];
bool check[32][2][2][2];
void go(int pos, int flagX, int flagY, int flagK, int first, int second,
int k) {
int new_pos = pos - 1;
bool _x = ((first >> new_pos) & 1);
bool _y = ((second >> new_pos) & 1);
bool _k = ((k >> new_pos) & 1);
bool new_flagX, new_flagY, new_flagK;
for (int digitX = 0; digitX < 2; digitX++) {
if (flagX && digitX > _x) continue;
for (int digitY = 0; digitY < 2; digitY++) {
if (flagY && digitY > _y) continue;
if (flagK && (digitX ^ digitY) > _k) continue;
new_flagX = new_flagY = new_flagK = 0;
if (flagX && digitX == _x) new_flagX = 1;
if (flagY && digitY == _y) new_flagY = 1;
if (flagK && (digitX ^ digitY) == _k) new_flagK = 1;
check[new_pos][new_flagX][new_flagY][new_flagK] |=
check[pos][flagX][flagY][flagK];
cnt[new_pos][new_flagX][new_flagY][new_flagK] +=
cnt[pos][flagX][flagY][flagK];
cnt[new_pos][new_flagX][new_flagY][new_flagK] %= MOD;
int foo = ((int)(digitX ^ digitY) << new_pos) % MOD;
dp[new_pos][new_flagX][new_flagY][new_flagK] +=
dp[pos][flagX][flagY][flagK] + foo * cnt[pos][flagX][flagY][flagK];
dp[new_pos][new_flagX][new_flagY][new_flagK] %= MOD;
}
}
}
int solve(int first, int second, int k) {
memset(dp, 0, sizeof(dp));
memset(cnt, 0, sizeof(cnt));
memset(check, 0, sizeof(check));
if (first == 0 || second == 0) return 0;
first--;
second--;
k--;
cnt[31][1][1][1] = 1;
check[31][1][1][1] = 1;
for (int i = 31; i > 0; i--) {
for (int flagX = 0; flagX < 2; flagX++) {
for (int flagY = 0; flagY < 2; flagY++) {
for (int flagK = 0; flagK < 2; flagK++) {
if (check[i][flagX][flagY][flagK] == 0) continue;
go(i, flagX, flagY, flagK, first, second, k);
}
}
}
}
long long ans = 0;
for (int flagX = 0; flagX < 2; flagX++) {
for (int flagY = 0; flagY < 2; flagY++) {
for (int flagK = 0; flagK < 2; flagK++) {
ans += dp[0][flagX][flagY][flagK];
ans += cnt[0][flagX][flagY][flagK];
ans %= MOD;
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int ntest;
cin >> ntest;
while (ntest--) {
int first, second, xx, yy, k;
cin >> first >> second >> xx >> yy >> k;
long long ans = solve(xx, yy, k) - solve(xx, second - 1, k) -
solve(first - 1, yy, k) + solve(first - 1, second - 1, k);
ans %= MOD;
if (ans < 0) ans += MOD;
cout << ans << endl;
}
return 0;
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.TreeSet;
public class c {
static final long MOD = (long) (1e9+7);
public static void main(String[] args) {
memo = new long[51][2][2][2];
memo2 = new long[51][2][2][2];
Scanner in = new Scanner(System.in);
int q = in.nextInt();
while(q --> 0) {
int x1 = in.nextInt()-1, y1 = in.nextInt()-1, x2 = in.nextInt()-1, y2= in.nextInt()-1;
k = in.nextLong()-1;
long sum = query(x2,y2,k) - query(x1-1,y2, k) - query(x2,y1-1,k) + query(x1-1,y1-1,k);
// long sum = query(x2,y2,k);
sum = ((sum % MOD) + MOD) % MOD;
System.out.println(sum);
}
}
static long k, ux, uy;
static long query(int tux, int tuy, long k) {
if(tux < 0 || tuy < 0) return 0;
ux = tux;
uy = tuy;
for(long[][][] arr1 : memo)
for(long[][] arr2 : arr1)
for(long[] arr3 : arr2)
Arrays.fill(arr3, -1);
for(long[][][] arr1 : memo2)
for(long[][] arr2 : arr1)
for(long[] arr3 : arr2)
Arrays.fill(arr3, -1);
return (countWays(50,1,1,1) + countAnswer(50,1,1,1)) % MOD;
}
static long[][][][] memo;
static long[][][][] memo2;
static long countWays(int bit, int boundedk, int boundedx, int boundedy) {
if(bit == -1) {
return 1;
}
if(memo[bit][boundedk][boundedx][boundedy] != -1)
return memo[bit][boundedk][boundedx][boundedy];
int kval = (int) ((k >> bit) & 1);
int xval = (int) ((ux >> bit) & 1);
int yval = (int) ((uy >> bit) & 1);
long ans = 0;
for(int vx=0;vx<2;vx++) {
for(int vy=0;vy<2;vy++) {
int xor = vx ^ vy;
if(boundedk == 1 && xor > kval) continue;
if(boundedx == 1 && vx > xval) continue;
if(boundedy == 1 && vy > yval) continue;
int bk = boundedk;
if(xor < kval)
bk = 0;
int bx = boundedx;
if(vx < xval)
bx = 0;
int by = boundedy;
if(vy < yval)
by = 0;
ans += countWays(bit-1, bk,bx,by);
ans %= MOD;
}
}
return memo[bit][boundedk][boundedx][boundedy] = ans;
}
static long countAnswer(int bit, int boundedk, int boundedx, int boundedy) {
if(bit == -1) {
return 0;
}
if(memo2[bit][boundedk][boundedx][boundedy] != -1)
return memo2[bit][boundedk][boundedx][boundedy];
int kval = (int) ((k >> bit) & 1);
int xval = (int) ((ux >> bit) & 1);
int yval = (int) ((uy >> bit) & 1);
long ans = 0;
for(int vx=0;vx<2;vx++) {
for(int vy=0;vy<2;vy++) {
int xor = vx ^ vy;
if(boundedk == 1 && xor > kval) continue;
if(boundedx == 1 && vx > xval) continue;
if(boundedy == 1 && vy > yval) continue;
int bk = boundedk;
if(xor < kval)
bk = 0;
int bx = boundedx;
if(vx < xval)
bx = 0;
int by = boundedy;
if(vy < yval)
by = 0;
ans += countAnswer(bit-1, bk,bx,by);
ans += countWays(bit-1, bk,bx,by) * (((long)xor) << bit);
ans %= MOD;
}
}
return memo2[bit][boundedk][boundedx][boundedy] = ans;
}
}
| JAVA |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
using namespace std;
long long k;
bool less1(long long x1, long long y1, long long x2, long long y2) {
if (x1 <= x2 && y1 <= y2) return true;
return false;
}
long long val(long long x, long long y) { return x ^ y; }
long long sum(long long x1, long long y1, long long x2, long long y2) {
long long len = max(x2 - x1 + 1, y2 - y1 + 1),
mn = val(x1, y1) - val(x1, y1) % len, mx = mn + len - 1, end;
if (k < mn)
return 0;
else if (k > mx) {
end = mx;
} else {
end = k;
}
long long start = mn;
long long ans = ((start + end + 2) * (end - start + 1) / 2) % 1000000007;
ans = (ans * min(x2 - x1 + 1, y2 - y1 + 1)) % 1000000007;
return ans;
}
vector<long long> vx, vy;
void fill(long long x1, long long y1, long long x2, long long y2) {
long long p, x, tx, y, ty;
vx.clear();
x = x1;
while (x != x2 + 1) {
vx.push_back(x);
tx = x;
p = 1;
while (tx % 2 == 0) {
p *= 2;
tx /= 2;
if (x + p - 1 > x2) {
p /= 2;
break;
}
}
x = x + p;
}
vy.clear();
y = y1;
while (y != y2 + 1) {
vy.push_back(y);
ty = y;
p = 1;
while (ty % 2 == 0) {
p *= 2;
ty /= 2;
if (y + p - 1 > y2) {
p /= 2;
break;
}
}
y = y + p;
}
vx.push_back(x2 + 1);
vy.push_back(y2 + 1);
}
int main() {
long long q, x1, x2, y1, y2, i, j, ans;
cin >> q;
while (q--) {
cin >> x1 >> y1 >> x2 >> y2 >> k;
x1--;
x2--;
y1--;
y2--;
k--;
if (x2 == 0 && y2 == 0) {
cout << 1 << endl;
continue;
}
fill(x1, y1, x2, y2);
ans = 0;
for (i = 0; i < vx.size() - 1; i++) {
for (j = 0; j < vy.size() - 1; j++) {
ans += sum(vx[i], vy[j], vx[i + 1] - 1, vy[j + 1] - 1);
ans %= 1000000007;
}
}
cout << ans << endl;
}
}
| CPP |
809_C. Find a car | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help.
Formally the parking can be represented as a matrix 109 Γ 109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (x, y) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (i, y) and (x, j), 1 β€ i < x, 1 β€ j < y.
<image> The upper left fragment 5 Γ 5 of the parking
Leha wants to ask the watchman q requests, which can help him to find his car. Every request is represented as five integers x1, y1, x2, y2, k. The watchman have to consider all cells (x, y) of the matrix, such that x1 β€ x β€ x2 and y1 β€ y β€ y2, and if the number of the car in cell (x, y) does not exceed k, increase the answer to the request by the number of the car in cell (x, y). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109 + 7.
However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests.
Input
The first line contains one integer q (1 β€ q β€ 104) β the number of Leha's requests.
The next q lines contain five integers x1, y1, x2, y2, k (1 β€ x1 β€ x2 β€ 109, 1 β€ y1 β€ y2 β€ 109, 1 β€ k β€ 2Β·109) β parameters of Leha's requests.
Output
Print exactly q lines β in the first line print the answer to the first request, in the second β the answer to the second request and so on.
Example
Input
4
1 1 1 1 1
3 2 5 4 5
1 1 5 5 10000
1 4 2 5 2
Output
1
13
93
0
Note
Let's analyze all the requests. In each case the requested submatrix is highlighted in blue.
In the first request (k = 1) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1.
<image>
In the second request (k = 5) suitable numbers are 4, 1, 2, 3, 2, 1. Consequentally the answer is 4 + 1 + 2 + 3 + 2 + 1 = 13.
<image>
In the third request (k = 10000) Leha asks about the upper left frament 5 Γ 5 of the parking. Since k is big enough, the answer is equal to 93.
<image>
In the last request (k = 2) none of the cur's numbers are suitable, so the answer is 0.
<image> | 2 | 9 | #include <bits/stdc++.h>
int cnt[40][2][2][2], sum[40][2][2][2];
int A[40], B[40], C[40];
int add(long long ans, int x) { return (ans + x) % 1000000007; }
int sub(long long ans, int x) {
return ((ans - x) % 1000000007 + 1000000007) % 1000000007;
}
int mul(long long ans, int x) { return ans * x % 1000000007; }
int get(int x, int y, int k) {
if (x < 0 || y < 0 || k < 0) return 0;
for (int i = 0; i <= 30; i++) {
A[30 - i] = x % 2, x /= 2;
B[30 - i] = y % 2, y /= 2;
C[30 - i] = k % 2, k /= 2;
}
memset(sum, 0, sizeof(sum));
memset(cnt, 0, sizeof(cnt));
sum[0][1][1][1] = 0;
cnt[0][1][1][1] = 1;
for (int i = 0; i <= 30; i++)
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++)
for (int d = 0; d <= 1; d++)
for (int e = 0; e <= 1; e++) {
int f = d ^ e;
if (a == 1 && d == 1 && A[i] == 0) continue;
if (b == 1 && e == 1 && B[i] == 0) continue;
if (c == 1 && f == 1 && C[i] == 0) continue;
cnt[i + 1][a & (d == A[i])][b & (e == B[i])][c & (f == C[i])] =
add(cnt[i + 1][a & (d == A[i])][b & (e == B[i])]
[c & (f == C[i])],
cnt[i][a][b][c]);
sum[i + 1][a & (d == A[i])][b & (e == B[i])][c & (f == C[i])] =
add(sum[i + 1][a & (d == A[i])][b & (e == B[i])]
[c & (f == C[i])],
sum[i][a][b][c]);
sum[i + 1][a & (d == A[i])][b & (e == B[i])][c & (f == C[i])] =
add(sum[i + 1][a & (d == A[i])][b & (e == B[i])]
[c & (f == C[i])],
mul(cnt[i][a][b][c], f << (30 - i)));
}
int res = 0;
for (int a = 0; a <= 1; a++)
for (int b = 0; b <= 1; b++)
for (int c = 0; c <= 1; c++) {
res = add(res, sum[31][a][b][c]);
res = add(res, cnt[31][a][b][c]);
}
return res;
}
int main() {
int q;
scanf("%d", &q);
while (q--) {
int x1, x2, y1, y2, k;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k);
x1--, x2--, y1--, y2--, k--;
int ans = 0;
ans = add(ans, get(x2, y2, k));
ans = add(ans, get(x1 - 1, y1 - 1, k));
ans = sub(ans, get(x1 - 1, y2, k));
ans = sub(ans, get(x2, y1 - 1, k));
printf("%d\n", ans);
}
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.