AT5749 Subarray Sum

前言

一道比较简单的题。(

完了完了完了要开学了要开学了。。。

题目大意

给定三个整数 \(N,K,S\)。

请你找到一个 \(N\) 个元素的整数序列,其中每一个元素在区间 \([1,10^9]\) 内。这个序列满足 \(K\) 个子序列的和为 \(S\)。

分析

要求有 \(K\) 个子序列的和是 \(S\),那么我们直接输出 \(K\) 个 \(S\),剩下的我们就让它始终不能出现和为 \(S\) 就行了,那么可以就可以再输出 \(n-k\) 个 \(10^9\),这可以在大部分情况下保证正确性。

上面仅仅是大部分情况下。

说一下特殊情况,比如说 4 2 1000000000 这样的例子,当 \(S=10^9\) 时,我们则输出 \(n-k\) 个 \(10^9-1\) 即可。

代码

//2021/8/30

#include <iostream>

#include <cstdio>

#define debug(c) cerr<<#c<<" = "<<c<<endl

namespace Newstd
{
	inline int read()
	{
		int x=0,f=1;char ch=getchar();
		while(ch<'0' || ch>'9')
		{
			if(ch=='-')
			{
				f=-1;ch=getchar();
			}
		}
		while(ch>='0' && ch<='9')
		{
			x=x*10+ch-48;ch=getchar();
		}
		return x*f;
	}
	inline void print(int x)
	{
		if(x<0)
		{
			putchar('-');x=-x;
		}
		if(x>9)
		{
			print(x/10);
		}
		putchar(x%10+'0');
	}
}

using namespace Newstd;

using namespace std;

int main(void)
{
	int n,k,s;
	
	scanf("%d%d%d",&n,&k,&s);
	
	for(register int i=1;i<=k;i++)
	{
		printf("%d ",s);
	}
	
	for(register int i=k+1;i<=n;i++)
	{
		if(s==1e9)
		{
			printf("99999999 ");
		}
		
		else
		{
			printf("1000000000 ");
		}
	}
	
	return 0;
}
上一篇:1712. 将数组分成三个子数组的方案数


下一篇:LeetCode91. 解码方法