题意
给定两个正整数 x , y x,y x , y ,每次操作可以:
x ← 2 x x\gets 2x x ← 2 x 。
x ← x + 1 x\gets x+1 x ← x + 1 。
y ← 2 y y\gets 2y y ← 2 y 。
y ← y + 1 y\gets y+1 y ← y + 1 。
求最小操作次数使得 x = y x=y x = y 。多测,T ≤ 5 × 10 7 T\leq 5\times 10^7 T ≤ 5 × 1 0 7 ,x < y ≤ 10 18 x<y\leq 10^{18} x < y ≤ 1 0 18 。
题解
场外选手来凑一下热闹,做了好久哦。
下文中将 __builtin_popcount 的时间复杂度视作 O ( log log V ) \mathcal{O}(\log\log{V}) O ( log log V ) 。
观察操作,发现每一次 × 2 \times 2 × 2 操作之后至多进行一次 + 1 +1 + 1 操作。也就是说,我们的操作必然形如:给操作数 a a a 一直 + 1 +1 + 1 变成 a ′ a' a ′ ,然后一直做 × 2 , ( + 1 ) , × 2 , ( + 1 ) , ⋯ \times 2,(+1),\times 2,(+1),\cdots × 2 , ( + 1 ) , × 2 , ( + 1 ) , ⋯ 的操作。从二进制表示来看,相当于给 a ′ a' a ′ 拼了一段后缀。
同时可以看出,我们一定不会给 y y y 进行 × 2 \times 2 × 2 操作,因为如果给 y y y 拼了某段后缀,x x x 也一定会拼(容易证明把 x x x 操作到 ≥ 2 x \geq 2x ≥ 2 x 的数必定会进行 × 2 \times 2 × 2 操作),此时我们同时把这段后缀对应的操作删去显然更优。
不妨考察如何计算将 x x x 操作成 y y y 的最小操作次数 f ( x , y ) f(x,y) f ( x , y ) 。枚举 y y y 的长度为 i i i 的后缀,表示 x x x 恰好拼上了这段后缀,可以得到:
f ( x , y ) = min 2 i x ≤ y { ⌊ y 2 i ⌋ − x + popcnt ( y m o d 2 i ) + i } f(x,y)=\min_{2^ix\leq y}\left\{\left\lfloor\frac{y}{2^i}\right\rfloor-x+\operatorname{popcnt}(y\bmod{2^i})+i\right\} f ( x , y ) = 2 i x ≤ y min { ⌊ 2 i y ⌋ − x + popcnt ( y mod 2 i ) + i }
这样我们可以单次 O ( log V log log V ) \mathcal{O}(\log{V}\log\log{V}) O ( log V log log V ) 地计算 f ( x , y ) f(x,y) f ( x , y ) 。
进一步可以证明,这个函数的最小值一定在 i = ⌊ log 2 ( y / x ) ⌋ i=\lfloor\log_2(y/x)\rfloor i = ⌊ log 2 ( y / x )⌋ 处取到。
证明 考察相邻项的差值:
Δ i = ( ⌊ y 2 i ⌋ − x + popcnt ( y m o d 2 i ) + i ) − ( ⌊ y 2 i + 1 ⌋ − x + popcnt ( y m o d 2 i + 1 ) + i + 1 ) = ( ⌊ y 2 i + 1 ⌋ + bit i ( y ) ) − bit i ( y ) − 1 = ⌊ y 2 i + 1 ⌋ − 1 \begin{align*}
\Delta_i&=\left(\left\lfloor\frac{y}{2^i}\right\rfloor-x+\operatorname{popcnt}(y\bmod{2^i})+i\right)-\left(\left\lfloor\frac{y}{2^{i+1}}\right\rfloor-x+\operatorname{popcnt}(y\bmod{2^{i+1}})+i+1\right)\\
&=\left(\left\lfloor\frac{y}{2^{i+1}}\right\rfloor+\operatorname{bit}_{i}(y)\right)-\operatorname{bit}_{i}(y)-1\\
&=\left\lfloor\frac{y}{2^{i+1}}\right\rfloor-1
\end{align*} Δ i = ( ⌊ 2 i y ⌋ − x + popcnt ( y mod 2 i ) + i ) − ( ⌊ 2 i + 1 y ⌋ − x + popcnt ( y mod 2 i + 1 ) + i + 1 ) = ( ⌊ 2 i + 1 y ⌋ + bit i ( y ) ) − bit i ( y ) − 1 = ⌊ 2 i + 1 y ⌋ − 1 显然在 i i i 的定义域内 Δ i ≥ 0 \Delta_i\geq 0 Δ i ≥ 0 ,因此最小值在 i = ⌊ log 2 ( y / x ) ⌋ i=\lfloor\log_2(y/x)\rfloor i = ⌊ log 2 ( y / x )⌋ 处取到。□ \Box □
这样我们可以单次 O ( log log V ) \mathcal{O}(\log\log{V}) O ( log log V ) 地计算 f ( x , y ) f(x,y) f ( x , y ) 了。
如果最终将 x , y x,y x , y 都操作成了 y + d y+d y + d ,则操作次数为 f ( x , y + d ) + d f(x,y+d)+d f ( x , y + d ) + d 。设 k = ⌊ log 2 ( y / x ) ⌋ k=\lfloor\log_2(y/x)\rfloor k = ⌊ log 2 ( y / x )⌋ ,考察 f ( x , 2 k + 1 x ) = k + 1 f(x,2^{k+1}x)=k+1 f ( x , 2 k + 1 x ) = k + 1 ,容易发现对于 t > 2 k + 1 x t>2^{k+1}x t > 2 k + 1 x ,必然有 f ( x , t ) > k + 1 f(x,t)>k+1 f ( x , t ) > k + 1 。于是我们可以将 k + 1 + 2 k + 1 x − y k+1+2^{k+1}x-y k + 1 + 2 k + 1 x − y 计入答案,接下来只用考虑 ⌊ log 2 ( ( y + d ) / x ) ⌋ = k \lfloor\log_2((y+d)/x)\rfloor=k ⌊ log 2 (( y + d ) / x )⌋ = k 的情况。
由于固定了 ⌊ log 2 ( ( y + d ) / x ) ⌋ \lfloor\log_2((y+d)/x)\rfloor ⌊ log 2 (( y + d ) / x )⌋ ,f ( x , y + d ) − f ( x , y ) f(x,y+d)-f(x,y) f ( x , y + d ) − f ( x , y ) 必然 ≤ log 2 V \leq\log_2{V} ≤ log 2 V 。这是因为只有 popcnt \operatorname{popcnt} popcnt 能使得函数值减小。于是我们只需要枚举 0 ≤ d ≤ log 2 V 0\leq d\leq \log_2{V} 0 ≤ d ≤ log 2 V ,将答案对 f ( x , y + d ) + d f(x,y+d)+d f ( x , y + d ) + d 取 min \min min 即可。至此我们得到了 O ( T log V log log V ) \mathcal{O}(T\log{V}\log\log{V}) O ( T log V log log V ) 的做法。期望得分 84 pts 84\ \text{pts} 84 pts 。
进一步优化,随着 d d d 增大,我们肯定想要 popcnt \operatorname{popcnt} popcnt 减小。从 y y y 开始,每次令 y ← y + lowbit ( y ) y\gets y+\operatorname{lowbit}(y) y ← y + lowbit ( y ) 即可,加到 y m o d 64 = 0 y\bmod{64}=0 y mod 64 = 0 就不用再加了。时间复杂度优化至 O ( T log 2 log V ) \mathcal{O}(T\log^2\log{V}) O ( T log 2 log V ) ,期望得分 96 pts 96\ \text{pts} 96 pts 。但是以我的实现已经可以在 QOJ 上通过本题 。
继续挖掘性质。我们知道 + lowbit +\operatorname{lowbit} + lowbit 的本质是推平最靠右的 1 1 1 段,可以发现不断 + lowbit +\operatorname{lowbit} + lowbit 的过程中,除了最后一次操作,其他操作给 popcnt \operatorname{popcnt} popcnt 带来的增量显然 ≤ 6 \leq 6 ≤ 6 。更具体来说,如果不是最后一次操作,且增量为 2 k ( k ≥ 2 ) 2^k(k\geq 2) 2 k ( k ≥ 2 ) ,那就无法带来正收益。因此我们只需要特判一下最后一次操作,然后依次考虑能否 + 1 , + 2 +1,+2 + 1 , + 2 即可。这样加上 f ( x , y ) f(x,y) f ( x , y ) 这次计算,只会计算 4 4 4 次 popcnt \operatorname{popcnt} popcnt 。
伟大的 @KazamaRuri 发现同时 + 1 , + 2 +1,+2 + 1 , + 2 同样无法带来正贡献,因此可以压到 3 3 3 次 popcnt \operatorname{popcnt} popcnt ,跑得挺快 。
其实可以直接预处理出低 6 6 6 位处 popcnt ( x + i ) + i \operatorname{popcnt}(x+i)+i popcnt ( x + i ) + i 的增量极值,这样就只有 2 2 2 次 popcnt \operatorname{popcnt} popcnt 了。代码跑得飞快,可以轻松通过本题 。
这样我们就得到了 O ( T log log V ) \mathcal{O}(T\log\log{V}) O ( T log log V ) 的做法。
代码 C++ 复制 #include "binary.h"
#include <bits/stdc++.h>
using namespace std ;
using ll = long long ;
template <typename T > inline void chk_min (T & x , T y ) { if (y < x) x = y; }
template <typename T > inline void chk_max (T & x , T y ) { if (x < y) x = y; }
int df[64 ];
void init (int t , int c ) {
for (int x = 0 ; x < 64 ; ++ x) {
int pc = __builtin_popcount (x);
for (int i = 0 ; i < 6 ; ++ i) chk_max (df[x], pc - __builtin_popcount (x + i) - i);
}
}
ll binary (ll x , ll y ) {
int k = 63 ^ __builtin_clzll (y / x), z = y & 63 ;
ll mask = (1 ll << k) - 1 , res = (x << k + 1 ) - y + k + 1 ;
auto calc = [& ](ll y ) { return (y >> k) - x + __builtin_popcountll (y & mask) + k; };
int p = z & 32 ? 64 - z : 0 ;
if (p) chk_min (res, calc (y + p) + p);
chk_min (res, calc (y) - df[y & mask & 63 ]);
return res;
}