C. Divisor Chain Harbour.Space Scholarship Contest 2023-2024 (Div. 1 + Div. 2)

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只剩下最高位的1
		int 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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timidcatt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值