练习cf1916A. 2023

题目如下
A. 2023
time limit per test1 second
memory limit per test256 megabytes
In a sequence 𝑎, whose product was equal to 2023, 𝑘 numbers were removed, leaving a sequence 𝑏 of length 𝑛. Given the resulting sequence 𝑏, find any suitable sequence 𝑎 and output which 𝑘 elements were removed from it, or state that such a sequence could not have existed.

Notice that you are not guaranteed that such array exists.

Input
Each test consists of several test cases. The first line contains a single integer 𝑡 (1≤𝑡≤100) — the number of test cases. This is followed by a description of the test cases.

The first line of each test case contains two integers 𝑛 (1≤𝑛,𝑘≤5) — the size of sequence 𝑏 and the number of numbers removed from sequence 𝑎.

The second line contains 𝑛 integers 𝑏1,𝑏2,…,𝑏𝑛 (1≤𝑏𝑖≤2023) — the remaining sequence. The values of 𝑏𝑖 might not be divisors of 2023.

Output
For each test case, output "YES" if the sequence 𝑎 exists, and in the following line output 𝑘 non-negative integers that were removed from the sequence 𝑎. If the sequence 𝑎 does not exist, output "NO" in a single line.

You can output the answer in any case (uppercase or lowercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive answers.

题目大意
每组原来有n + k个数,有k个数被移除了数组,能否找到k个数使得整个数组的乘积等于2023。找到输出yes并输出k个数使得整个数组乘积等于2023,否则输出no即可。

题目分析
虽然我一开始没看出来,但是仔细看就发现2023总共就6个因数分别是1,2023,7,17,289,119,那么只要先判断余下的数组的乘积是否能整除2023即可。若能整除,便把除数分配给k个数即可,可以直接用1来凑数。

点击查看代码
#include <iostream>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n, k;
        cin >> n >> k;
        int b[2024];
        long long left = 1;
        for(int i = 0; i < n; i++){
            cin >> b[i];
            left *= b[i];
        }
        if(2023 % left == 0){
            printf("YES\n");
            printf("%d ", 2023 / left);
            if(k > 1){
                for(int i = 0; i < k - 1; i++){
                    printf("1 ");
                }
            }
            printf("\n");
        }else{
            printf("NO\n");
        }
    }
    return 0;
}
posted @ 2025-07-25 22:01  sirro1uta  阅读(13)  评论(0)    收藏  举报