TT的神秘任务1(思路)

问题描述

这一天,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;
}

猜你喜欢

转载自blog.csdn.net/weixin_43347376/article/details/106215504
tt1
TT