icpc:problems:usaco22jan_searching_for_soulmates_s
problems | |
---|---|
名称 | Searching for Soulmates S |
题目编号 | USACO22JAN_S1 |
题目链接 | luogu.com.cn/… |
来源 | USACO |
算法分类 | 数学, 思维 |
难易程度 | 一般般 |
Searching for Soulmates S
想法
题目大意:通过 *2, /2(偶数), +1 的操作,让x → y。
我们知道*2与/2一定不会交替进行,因为会抵消。所以让x先/2然后在*2。也就是x先下降,然后再提升。
枚举/2的操作数,然后再算x*2 → y的数量。算乘到y可以逆向思维成 算y除到x的数量,这样就不会选择*2还是+1。
最后除法与乘法的数量相加,取最优。
代码实现
#include<iostream> using namespace std; long long int DFS(long long int x, long long int y) { if(x > y) return 1e12; if(2 * x > y) return y-x; if(y & 1) return DFS(x, y-1) + 1; return DFS(x, y/2) + 1; } int main() { int T; cin >> T; while(T--) { long long int a, b; cin >> a >> b; long long int cnt = 0, ans = 1e12; do { long long int res = DFS(a, b); ans = min(ans, cnt + res); if(a & 1) a++; else a>>=1; cnt++; } while(a > 1); cout << ans << endl; } return 0; }
板书
/app/www/public/data/pages/icpc/problems/usaco22jan_searching_for_soulmates_s.txt · 最后更改: 2023/02/08 08:19 由 温婕莺