Redaksiya
Problem Author: | Illia Shevchenko |
Prepared by: | Illia Shevchenko , Illia Fursov |
Editorial by: | Illia Shevchenko |
Let's begin by calculating the maximum value of where , .
We can calculate for each bit separately.
Let's see how the values of and in bit affect :
1) If the value of is and the value of is , then .
2) If the value of is and the value of is , then .
3) If the value of is and the value of is , then .
4) If the value of is and the value of is , then .
Let be the maximum power of such that and .
The maximum value of will be achieved when there is at least one set bit in every bit position up to in either or .
For example, take the numbers and .
.
Let's denote as the maximum value of , which equals .
1)First case , .
. This is possible only if , and there is only one such case.
2)Second case , .
Now, let's apply a standard idea: consider the length of the maximum common prefix of and , and the length of the maximum common prefix of and .
Let the maximum common prefix of and be , and the maximum common prefix of and be .
Then, the bit in and does not match (otherwise, we could extend the common prefix). But since , the bit in is , and the bit in is .
We do the same for .
Now there are two cases:
1).
2).
1)If , then both and will have a in the bit, so the result will be less than .
2)Let's consider the case (the case is analogous).
Thus, we have two prefixes: and .
Notice that there should be no zeroes in the prefix of length .
Also, notice that the bit in must be since the bit in is .
Now we have two separate non-overlapping bit segments: and [ ; _].
We can count the number of possible ways to assign bits in both of these segments and then multiply the results.
Let's consider a bit belonging to the segment [ ; ]. If it is , then the bit in must be ; if it is , then there are two options for the bit in .
Now let's consider the segment [ ; ]. Since and are already , we can place the bits as we like, as long as there is at least one in one of the numbers. This can be done in ways raised to the power of the length of the segment.
Full solution with time complexity (C++):
/* In honor of Anton Tsypko */ #include <vector> #include <algorithm> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <stack> #include <queue> #include <cmath> #include <random> #include <chrono> #include <iomanip> #include <iostream> #include <bitset> #include <cmath> using namespace std; int log_ = 3; int inf = 2000000007; int mod = 1000000007; int p = 501; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long n; cin >> n; long long a = 1; int c = 0; while (a * 2 <= n) { a *= 2; c++; } long long mx = 2 * (2*a - 1); vector <int> bit; long long n_ = n; while (n_ > 0) { bit.push_back(n_ % 2); n_ /= 2; } bit.push_back(5); reverse(bit.begin() , bit.end()); long long ans = 0; for (int i = 0; i <= c+1; i++) { for (int j = 0; j <= c + 1; j++) { if (bit[j] == 0) break; if (i + j == 0) continue; if (i < j) continue; if (i == j) continue; if ((i + 1 < bit.size()) and (bit[i + 1] == 0)) continue; if ((j + 1 < bit.size()) and (bit[j + 1] == 0)) continue; long long col = 1; for (int k = j + 2; k <= i; k++) { if (bit[k] == 1) col *= 2; col %= mod; } for (int k = i+2; k < bit.size(); k++) { col *= 3; col %= mod; } ans += col; ans %= mod; } } ans *= 2; if (2 * n == mx) ans++; cout << mx << " " << ans % mod; }
Time complexity: or or
Memory complexity:
There is also a solution using dynamic programming with a time complexity of .