Problem - C - Codeforces
题目大意:给出一个数k,每次操作可以选择当前k的一个因数x,并令k=k-x,要求令k变成1,且相同的x 出现次数不超过2,最大操作次数1000
2<=k<=1e9
思路:因为每个数的llowbit肯定是这个数的因数,那么我们把k拆成二进制,例如15就是1111,那么我们先操作3次就可以分别把它变成14,12,8,每个x都不会重复
接着再把其变成1,只要每次都除以2即可,每次使用的因数都是2的幂,也不会重复,这样两套操作下来相同的因数肯定不会超过2,总操作数不超过64
//#include<__msvc_all_public_headers.hpp>
#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
typedef long long ll;
const int INF = 0x7fffffff;
int ans[N];
int lowbit(int x)
{return x & (-x);
}
void solve()
{int k;cin >> k;vector<int>ans;ans.push_back(k);while (lowbit(k) != k){//令k只剩下最高位的1int temp = lowbit(k); k -= temp;ans.push_back(k);}while (k != 1){k >>= 1;ans.push_back(k); }cout << ans.size() << endl;for (int i = 0; i < ans.size(); i++){cout << ans[i] << " " ;}cout << endl;
}
int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int t;cin >> t;while (t--){solve();}return 0;
}