B. Same Parity Summands
题目描述
You are given two positive integers n (1≤n≤109) and k (1≤k≤100). Represent the number n as the sum of k positive integers of the same parity (have the same remainder when divided by 22).
In other words, find a1,a2,…,ak such that all ai>0, n=a1+a2+…+ak and either all ai are even or all ai are odd at the same time.
If such a representation does not exist, then report it.
输入描述:
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Next, t test cases are given, one per line.
Each test case is two positive integers n (1≤n≤10^9) and k (1≤k≤100).
输出描述:
For each test case print:
- YES and the required values ai, if the answer exists (if there are several answers, print any of them);
- NO if the answer does not exist.
The letters in the words YES and NO can be printed in any case.
输入
8
10 3
100 4
8 7
97 2
8 8
3 10
5 3
1000000000 9
输出
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
题目大意及解题思路
大意:
给定两个正整数n(1≤n≤109)和k(1≤k≤100)。将数n表示为相同奇偶校验的k个正整数的和(除以2时具有相同的余数)。换句话说,找到a1,a2,…,ak,使得所有ai>0,n=a1+a2+…+ak,并且所有ai都是偶数或者所有ai都同时是奇数。如果不存在这样的表述,则报告它。YES和所需值ai,如果答案存在(如果有多个答案,打印其中任何一个);如果答案不存在,则为否。单词YES和NO中的字母在任何情况下都可以打印。
思路:
这是一道思维题,或者说是一道数学题,1200分,对于我这种蒻冓来说还是要考虑很长时间的。 他说要将给定的n分成k个奇数或者偶数,找不到的就输出NO。很显然,当k>n时,无论是奇数还是偶数都是找不到的,先将这种情况排除,其次我们就从最简单的地方入手,让我们化成奇数的和或偶数的和,最简单的奇数和偶数就是1和2,尽可能多的用1/2将n表示(即前k-1个都是1/2只需要判断最后一个数的奇偶性即可)。
所以,每一组数据,我们继续讨论两种情况(前k-1个都是1或都是2),看最后一个数的奇偶,需要注意的是,每个数不能小于0,奇数不用判断,偶数时:n-2*k+2>0。剩下的其它情况都为NO。
AC代码
#include<iostream>
#include<algorithm>
using namespace std;
#define int long long
#define endl '\n'
typedef long long ll;
const int N = 100010;
signed main()
{
int t;
cin >> t;
while (t--)
{
int n, k;
cin >> n >> k;
if (k > n)
{
cout << "NO" << endl;
}
else if ((n - (k - 1)) % 2 == 1)
{
cout << "YES" << endl;
for (int i = 1; i < k; i++)
{
cout << "1 ";
}
cout << n - (k - 1) << endl;
}
else if ((n - ((k - 1) * 2)) % 2 == 0 && n-2*k+2>0)
{
cout << "YES" << endl;
for (int i = 1; i < k; i++)
{
cout << "2 ";
}
cout << n - ((k - 1) * 2) << endl;
}
else cout << "NO" << endl;
}
return 0;
}