传送门
题目描述
给出两个数
N
N
N,
S
S
S
问是否可以找到一个序列和一个数
K
K
K(
0
<
=
K
<
=
S
0<=K<=S
0<=K<=S)满足:存在一个长度为
N
N
N,和为
S
S
S序列,在其中找不到一个子段的和为
K
K
K或者
S
−
K
S-K
S−K
如果可以,输出
Y
E
S
YES
YES以及找到的序列还有
K
K
K
否则,输出
N
O
NO
NO
分析
很经典的构造题,也是我一直以来的短板
首先这种有解无解的题目,首先要判断的就是,什么情况下无解
当
S
<
2
∗
N
S < 2 * N
S<2∗N的情况下,一定无解
可以简单证明一下,因为
S
<
2
∗
N
S < 2 * N
S<2∗N,那么这个序列中必然存在任意个
1
1
1,那么我进行划分,肯定能划分出和为任意奇数的字区间,那么如果我们选择偶数,那么
S
−
K
S - K
S−K只能是奇数或者偶数,如果是奇数,根据刚才的结论一定能组成,如果是偶数,那么必然有偶数个
1
1
1,那么也可以组成任意偶数
那么我们怎么去构造正确答案呢,可以先输出
n
−
1
n - 1
n−1个2,然后把剩下的数字输出既可,
K
K
K的值取1即可,简单证明一下即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){return (b>0)?gcd(b,a%b):a;}
int main(){
int n,s;
read(n),read(s);
if(s < n * 2){
puts("NO");
return 0;
}
puts("YES");
for(int i = 1;i < n;i++) printf("2 ");
printf("%d\n1",s - (n - 1) * 2);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/