k-LCM ( easy version && hard version ) -( 思维 - 构造 )

题目链接(easy):点击进入
题目链接(hard):点击进入
题目

在这里插入图片描述

题意

长度为 k 的数组,整个数组的和为 n ,整个数组的最小公倍数不超过 n / 2,让你构造这样一个数组
简单版本k=3,困难版本 3 <= k <= n。

思路

简单版本:
因为明确告诉你了 k = 3 ,同时要求三个数的最小公倍数不能超过 n / 2,所以我们可以根据 n 来构造这个三个数。
首先根据 n 的奇偶性分类讨论
如果 n 是奇数,那么三个数可以变为 n / 2 , n / 2 , 1 ;
如果 n 是偶数,那么这时候再看 tmp = n / 2 的奇偶性 :
如果 tmp 是奇数,那么三个数可以变为 n / 2 - 1 , n / 2 - 1 , 2 ;
如果 tmp 是偶数,那么三个数可以变为 n / 4 , n / 4 , n / 2 ;

困难版本:
k - 3 个数为 1 ,剩下三个数按照简单版本组合

代码(easy)
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
#define int long long
//#define double long double
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e6+10;
const int mod=1e9+7;
const double eps=1e-9;
int t,n,m,k,p[maxn];
bool vis[maxn];
map<int,int>mp;
signed main()
{	
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);	
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		int a,b,c;
		if(n&1) 
			a=b=n/2,c=1;
		else 
		{
			int tmp=n/2;
			if(tmp&1)
				a=b=tmp-1,c=2;
			else
				a=b=tmp/2,c=tmp;
		}
		cout<<a<<' '<<b<<' '<<c<<endl;
	}
    return 0;
}
代码(hard)
#include<iostream>
#include<string>
#include<map>
#include<set>
//#include<unordered_map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<fstream>
#define X first
#define Y second
#define best 131 
#define INF 0x3f3f3f3f3f3f3f3f
#define pii pair<int,int>
#define lowbit(x) x & -x
#define inf 0x3f3f3f3f
#define int long long
//#define double long double
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pai=acos(-1.0);
const int maxn=1e6+10;
const int mod=1e9+7;
const double eps=1e-9;
int t,n,m,k,p[maxn];
bool vis[maxn];
map<int,int>mp;
signed main()
{	
//	ios::sync_with_stdio(false);
//	cin.tie(0);cout.tie(0);	
	cin>>t;
	while(t--)
	{
		cin>>n>>k;
		for(int i=4;i<=k;i++) p[i]=1,n--;
		int a,b,c;
		if(n&1) 
			a=b=n/2,c=1;
		else 
		{
			int tmp=n/2;
			if(tmp&1)
				a=b=tmp-1,c=2;
			else
				a=b=tmp/2,c=tmp;
		}
		p[1]=a;
		p[2]=b;
		p[3]=c;
		for(int i=1;i<=k;i++) cout<<p[i]<<' ';
		cout<<endl;
	}
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值