牛妹的平行宇宙题解
思路
这道题要求我们从编号为
我们注意到第二种跳跃方式,编号可以变为原来的两倍;反过来,如果当前编号是偶数,就可以通过除以
第三种跳跃方式:当编号满足
就是如果我们从目标编号开始倒推:偶数就除以
由于是从目标到起点的路径,最后注意下把路径反转就行。
代码实现
#include <bits/stdc++.h>
#define ll long long
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
int main() {
IOS;
freopen("space.in", "r", stdin);
freopen("space.out", "w", stdout);
int q;//也是用上q这种高级玩意了
ll d, l;
cin >> q >> d >> l;
while (q--) {
ll x;
cin >> x;
vector<ll> path = {x};
while (x != 0 && x != 1 && x != -1 && x != -5 && x != -17) {
x = (x % 2 == 0 ? x / 2 : 3 * x + 1);
path.push_back(x);
}
while (x <= 0) {
x += d;
path.push_back(x);
}
while (x != 1) {
x = (x % 2 == 0 ? x / 2 : 3 * x + 1);
path.push_back(x);
}
assert(path.size() - 1 <= 1500);//path如果小于1500就退出
cout << path.size() + 2 << " 1 2 4";
for (auto i = path.rbegin(); i != path.rend(); i++) {
cout << ' ' << *i;
}
cout << '\n';
}
return 0;
}
复杂度分析
整体时间复杂度为