Strange Test
[Link](Problem - C - Codeforces)
题意
给你两个数 a , b a,b a,b每次有三种操作 1. a = a + 1 2. b = b + 1 3. a = a ∣ b 1.a=a+1\ 2.b=b+1\ 3.a = a | b 1.a=a+1 2.b=b+1 3.a=a∣b,问你最少操作多少次使得 a = = b a==b a==b。
思路
首先我们最多或一次,因为 a ∣ = b a|=b a∣=b后 a > = b a>=b a>=b所以只需要让 b b b加过去就可以了。
如果说不适用或的话,操作数就是 b − a b-a b−a,剩下就是使用一次或和它比较即可,贪心的来想就是一个变一个不变然后到达某个界限的时候直接或一下,例如变 a a a则当 a , ∣ b = = b 即 可 a^,|b==b即可 a,∣b==b即可,或者是 b , ∣ a = b , b^,|a=b^, b,∣a=b,,要保证 a ∣ a| a∣完不会让结果增加。
贪不出来我们可以写写式子,我们设 a 1 , b 1 a1,b1 a1,b1的时候他们或了一下就想同了,那么有
r e s = a 1 − a + ( a 1 ∣ b 1 ) − b + 1 = ( a 1 ∣ b 1 ) + a 1 + 1 − a − b res=a1-a+(a1|b1) -b+1=(a1|b1)+a1+1-a-b res=a1−a+(a1∣b1)−b+1=(a1∣b1)+a1+1−a−b
我们要找到 a 1 ∣ b 1 + a 1 a1|b1+a1 a1∣b1+a1的最小值,可以枚举 a a a然后贪心的改变 b b b的值保证 b 1 > = b b1>=b b1>=b且取得最小。发现从低位往高位枚举当前位置放的值会受前面放的影响,因此选择从高位往低位贪,如果当前位置 b b b是 0 且 a , 为 1 0且a^,为1 0且a,为1那么这一位将 b , 为 1 b^,为1 b,为1就可以保证 b , > b b^,>b b,>b后面只需和 a , a^, a,填一样即可。
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a, b;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T --) {
cin >> a >> b;
int res = b - a;
for (int i = a; i <= b; i ++) {
int k = b;
for (int j = 20; j >= 0; j --)
if ((i >> j) & 1)
if ((k >> j & 1) == 0) {
k |= 1 << j;
k &= ~((1 << j) - 1);
}
res = min(res, i - a + k - b + 1);
}
cout << res << endl;
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <set>
#include <queue>
#include <vector>
#include <map>
#include <bitset>
#include <unordered_map>
#include <cmath>
#include <stack>
#include <iomanip>
#include <deque>
#include <sstream>
#define x first
#define y second
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long double ld;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef unsigned long long ULL;
const int N = 1e5 + 10, M = 2 * N, INF = 0x3f3f3f3f, mod = 1e9 + 7;
const double eps = 1e-8, pi = acos(-1), inf = 1e20;
#define tpyeinput int
inline char nc() {static char buf[1000000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;}
inline void read(tpyeinput &sum) {char ch=nc();sum=0;while(!(ch>='0'&&ch<='9')) ch=nc();while(ch>='0'&&ch<='9') sum=(sum<<3)+(sum<<1)+(ch-48),ch=nc();}
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
int h[N], e[M], ne[M], w[M], idx;
void add(int a, int b, int v = 0) {
e[idx] = b, w[idx] = v, ne[idx] = h[a], h[a] = idx ++;
}
int n, m, k;
int a, b;
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T --) {
cin >> a >> b;
int res = INF;
res = b - a;
for (int i = a; i < b; i ++)
if ((i | b) == b) res = min(res, i - a + (i | b) - b + 1);
for (int i = b; i <= (a | b); i ++)
if ((i | a) == i) res = min(res, 1 + i - b);
cout << res << endl;
}
return 0;
}