Greedy: m minimizes the value of M based on the maximum result of the OR operation with N.
First, you can convert both the upper and lower bounds to binary values. First, you can determine the highest bits of M. The highest bits of M can start from the highest bits of U (The reason is very simple), and then determine the subsequent numbers, each number should be used to ensure that the number at the corresponding position of the binary of N is complementary. (If a bit of the binary of N is 1, the corresponding bit of m should be 0 as much as possible, in this way, the maximum value can be ensured, and the M value should be as small as possible.GuaranteeIn each greedy process, the m value cannot exceed the range of [n, m.
The Code is as follows:
# Include <iostream> # include <cstdlib> # include <cstring> # include <cstdio> # include <cmath> # include <ctime> using namespace STD; void d_bite (char * STR, unsigned long num) // convert data to binary {If (! Num) STR [0] = '0'; else for (INT I = 0; num; I ++) {STR [I] = num % 2 + '0 '; num/= 2 ;}} unsigned long B _dight (char * STR, int Len) // convert binary data into numbers {unsigned long num = 0; For (INT I = 0; I <Len; I ++) if (STR [I] = '1') num + = (1ll <I); Return num;} int main () {# ifdef test freopen ("in.txt", "r", stdin); # endif unsigned long l, U, N; char U [65], M [65], L [65], n [65]; while (scanf ("% LlU ", & N, & L, & U )! = EOF) {memset (u, 0, sizeof (u); memset (n, 0, sizeof (n); memset (M, 0, sizeof (m )); memset (L, 0, sizeof (l); d_bite (u, u); d_bite (L, L); d_bite (N, N ); int len_u = strlen (u), len_m = len_u, len_l = strlen (l); m [len_m-1] = '1'; If (! U) // when the interface is 0, special processing is required. M [len_m-1] = '0'; while (1) {If (N [len_m-1] = '1' & len_m> len_l) {-- len_m; m [len_m] = '\ 0 '; M [len_m-1] = '1';} else break;} int ffct = len_m-1; for (INT I = 0; I <len_m-1; I ++) M [I] = '0'; while (ffct> = 0) {If (N [ffct]! = '1') {M [ffct] = '1'; If (B _dight (M, len_m)> U) // when the corresponding position is set to '1, consider whether the upper limit M [ffct] = '0';} else {M [ffct] = '0'; If (B _dight (M, len_m) <L) // when the corresponding position is set to '0', consider whether the lower bound {memset (M, '1', sizeof (M [0]) * ffct) is exceeded ); if (B _dight (M, len_m) <L) m [ffct] = '1'; memset (M, '0', sizeof (M [0]) * ffct) ;}}-- ffct ;}printf ("% LlU \ n", B _dight (M, len_m) ;}return 0 ;}