Week13 作业 A -CodeForces - 1352B - Same Parity Summands

题目描述:

给定N和K,是否存在K个奇偶性相同的正整数,使得K个数的和是N,是Special Judge

思路:

  • 首先考虑如果存在可行方案,如何找到K个数?其实可以想到,如果K个数都是奇数,则一定能表示成K-1个1和N-K*1,比如N=100,K=6,则一定能表示成1 1 1 1 1 95;如果K个数是偶数,则一定能表示成2 2 2 2 2 2.....N-2*(K-1);
  • 那么怎么判断存不存在可行方案?就按上述方法看N-K是不是奇数;N-2*(K-1)是不是偶数,即可
  • 注意边界条件:如果K个数都是奇数,则N不能比K大,因为K个数至少都是1;如果K个数都是偶数,则N不能比K*2大,因为K个数至少都是2

代码:

#include <cstdio>
#include <iostream>
int N, K;
using namespace std;

void print(int t)
{
	printf("YES\n");
	for (int i = 1; i <= K - 1; i++)
		printf("%d ", t);
	printf("%d\n", N - (K - 1) * t);
}

int main()
{
	int T;
	cin >> T;
	while (T--)
	{

		scanf("%d %d", &N, &K);
		if ( ( (N - (K - 1) * 1) & 1 ) == 1)
		{
			if(N<K)
			{
				cout<<"NO"<<endl;
				continue; 
			}
			 
			print(1);
			continue;
		}
		if ( ( (N - (K - 1) * 2) & 1 ) == 0)
		{
			if(N<K*2)
			{
				cout<<"NO"<<endl;
				continue; 
			}
			print(2);
			continue;
		}
		cout << "NO" << endl;
	}
	return 0;
}

  

posted @ 2020-06-08 21:55  菜鸡今天学习了吗  阅读(160)  评论(0编辑  收藏  举报