Codeforces Round 708 (Div. 2)C2. k-LCM (hard version)
题面翻译(摘自洛谷)
本题与 CF1497C1 的不同之处是在本题中, 3 ⩽ k ⩽ n 3\leqslant k\leqslant n 3⩽k⩽n。
给定一个整数 n n n,请找到 k k k 个和为 n n n 的正整数 a 1 , a 2 , … , a k a_1,a_2,\dots,a_k a1,a2,…,ak,使得 lcm { a 1 , a 2 , … , a k } ⩽ n 2 \operatorname{lcm}\{a_1,a_2,\dots,a_k\}\leqslant \dfrac n2 lcm{a1,a2,…,ak}⩽2n 。
t
t
t 组数据,
1
⩽
t
⩽
1
0
4
1\leqslant t\leqslant 10^4
1⩽t⩽104,
3
⩽
n
⩽
1
0
9
3\leqslant n\leqslant 10^9
3⩽n⩽109,
3
⩽
k
⩽
n
3\leqslant k\leqslant n
3⩽k⩽n。保证所有
t
t
t 组数据中
∑
k
⩽
1
0
5
\sum k\leqslant 10^5
∑k⩽105。
Translated by Eason_AC 2021.3.20
思路:
-
其实和普通版本真没啥区别,当 k ⩾ 3 k \geqslant 3 k⩾3 时,我们只需要输出 k − 1 k-1 k−1 次 1,得到 k = 3 , n = n − k + 3 k=3,n=n-k+3 k=3,n=n−k+3,然后将新的 n n n 拆成 3 3 3 个数:
- n n n 为奇数时,变成 n 2 , n 2 , 1 \frac{n}{2},\frac{n}{2},1 2n,2n,1,最小公倍数为 n 2 \frac{n}{2} 2n
- n n n 为偶数,并且 n n n 能被 4 4 4 整除时,拆成 n 2 , n 4 , n 4 \frac{n}{2},\frac{n}{4},\frac{n}{4} 2n,4n,4n,最小公倍数为 n 2 \frac{n}{2} 2n
- n n n 为偶数,但 n n n 不能被 4 4 4 整除时,拆成 n 2 − 1 , n 2 − 1 , 2 \frac{n}{2}-1,\frac{n}{2}-1,2 2n−1,2n−1,2,最小公倍数为 m a x ( n 2 − 1 , 2 ) max(\frac{n}{2}-1,2) max(2n−1,2)
input:
2
6 4
9 5
output:
1 2 2 1
1 3 3 1 1
code:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define Please return
#define Accepted 0
#define int long long
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<double, double> PDD;
const int N = 200010,M=2*N,INF=0x3f3f3f3f;
int n,k;
void slove(int _case)
{
cin>>n>>k;
n-=k-3;
while(k>3){
cout<<"1 ";k--;
}
if(n&1){
cout<<n/2<<" "<<n/2<<" 1"<<endl;
}else{
if(n%4)cout<<n/2-1<<" "<<n/2-1<<" 2"<<endl;
else cout<<n/2<<" "<<n/4<<" "<<n/4<<endl;
}
}
signed main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int T=1;
cin>>T;
for(int _case=1;_case<=T;_case++)
{
slove(_case);
}
Please Accepted;
}