跳到正文

剪贴板

剪贴板 gw0pj9nx

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const long long inf = 1e18;
const long long mod = 1e9 + 7;
const long long N = 5e5 + 5;
int n , m;
int a[65] = {0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,\
 1 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 3 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 2 , 0 ,\
  0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 2 , 4 , 0};//预处理
int s(int x) {
	int y = (x | 63) + 1;
	return min(__builtin_popcountll(x) - a[x & (63)], __builtin_popcountll(y) + y - x);
}//这里是处理x,y开头处不一样的部分需要处理的地方,同时也处理了y他通过+1可以解决的麻烦,注意到,此时我们已经把x变为和y位数相同了,我们注意到,>64的部分你不管怎么整都是亏的,因为只有64位数,因此我们实际上只需考虑64位,这个我下午第一版代码出来后,有人指出的96分做法,于是根据这个方向写出,然后我们看前面的popcnt

//打表有人说认为有问题,我在这里解释,我们注意到64对应的是六位二进制,于是我们就考虑他最多能减少的值和能增加的值,那么显然,我们应该考虑怎么加赚的最多,于是,那么显然,每多增加1位,我们就能减少1的代价,但是我们同样要付出一个+1的代价max(a[i], -j + popcnt(i) - popcnt(i+j))(0<=j<=6)
void solve() {
	int x , y;
	cin>>x>>y;
	if(x > y)swap(x , y);
	int cnt = 0;
	cnt += __lg(y) - __lg(x);//计算两个数字转二进制后的长度差
	x <<= cnt;//我一开始选择y左移,但后来发现这么做不是一个好选择,我们没有必要完全模仿题目步骤
	if (x > y)x >>= 1 , cnt--;//对应我原来代码中的,我一开始是通过一个if判断两个大小的,然后控制左移位数,后选择右移.
	cout<<cnt + min(((x << 1) - y + 1), (((y - x) >> cnt) + s(y - x & (1 << cnt) - 1)))<<endl;//输出统计的答案,前面的代码对应我之前在复杂度瓶颈之后的代码,这个对应复杂度瓶颈代码部分。
}
signed main()
{
//	freopen("ce.in" , "r" , stdin);
//	freopen("ce.out" , "w" , stdout);
	cin.tie(0) , cout.tie(0);
	ios::sync_with_stdio(0);
	int ___;
	cin>>___;
	while(___--)solve();
	return 0;
}
/*

101111
110000



*/