問題描述
這一天,TT 遇到了一個神祕人。
神祕人給了兩個數字,分別表示 n 和 k,並要求 TT 給出 k 個奇偶性相同的正整數,使得其和等於 n。
例如 n = 10,k = 3,答案可以爲 [4 2 4]。
TT 覺得這個任務太簡單了,不願意做,你能幫他完成嗎?
本題是SPJ
Input
第一行一個整數 T,表示數據組數,不超過 1000。
之後 T 行,每一行給出兩個正整數,分別表示 n(1 ≤ n ≤ 1e9)、k(1 ≤ k ≤ 100)。
Output
如果存在這樣 k 個數字,則第一行輸出 “YES”,第二行輸出 k 個數字。
如果不存在,則輸出 “NO”。
Sample input
8
10 3
100 4
8 7
97 2
8 8
3 10
5 3
1000000000 9
Sample output
YES
4 2 4
YES
55 5 5 35
NO
NO
YES
1 1 1 1 1 1 1 1
NO
YES
3 1 1
YES
111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120
解題思路
由於這道題是spj,我們直接考慮極端的情況,題目要求將一個n分割成k個數,這k個數必須奇偶性相同,那麼可以考慮分割成k-1個1和最後一個n-(k-1),只要n-(k-1)是奇數就可以。同樣,可以分割成k-1個2和最後一個n-2*(k-1),只要剩下的這個數是偶數即可。然後處理一下一些細節就好了。
完整代碼
//#pragma GCC optimize(2)
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
int t,k,n;
int getint(){
int x=0,s=1; char ch=' ';
while(ch<'0' || ch>'9'){ ch=getchar(); if(ch=='-') s=-1;}
while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar();}
return x*s;
}
int main(){
//ios::sync_with_stdio(false);
//cin.tie(0);
cin>>t;
while(t--){
cin>>n>>k;
if(k>n) cout<<"NO"<<endl;
else if((n-k+1)%2==1){//可以奇數拆
printf("YES\n");
for (int i=1; i<k; i++) cout<<1<<' ';
cout<<n-k+1<<endl;
}
else if(n-2*k+2<=0) cout<<"NO"<<endl;
else if((n-2*k+2)%2==0){//可以偶數拆
printf("YES\n");
for (int i=1; i<k; i++) cout<<2<<' ';
cout<<n-2*k+2<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}