题意:给出 N , S N,S N,S要你求出一个长为 N N N,和为 S S S的数组,并问你能不能找到一个 K K K,使数组的任意一个子数组的和都不等与 K K K。
思路 1 1 1:如果 S / N > = 2 S/N>=2 S/N>=2,我们就能这样安排数组,前 N − 1 N-1 N−1个设为 1 1 1,最后一个设为 S − N + 1 S-N+1 S−N+1,只要设 K = S − N K= S-N K=S−N那就满足题意了,如果 S / N = 1 S/N=1 S/N=1,我们可以发现 N − 1 > S − N N-1>S-N N−1>S−N,所以前面 N − 1 N-1 N−1个1就可以组成 S − N S-N S−N,不满足题意。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
const int inf = 0x7ffffff;
const ll INF = 0x7fffffffffff;
ll f[][2] = {
1, 0, 0, 1, -1, 0, 0, -1}, n, m;
ll s[N];
int read() {
int x = 0; int f = 1; char s = getchar();
while(s < '0' || s > '9') {
if(s == '-') f = -1; s = getchar();}
while(s >= '0' && s <= '9') {
x = (x << 3) + (x << 1) + s - 48; s = getchar();}
return x * f;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
n = read(), m = read();
if(m / n == 1) printf("No\n");
else {
cout << "Yes\n";
if(n == 1) cout << m << '\n' << m-1 << '\n';
else {
for(int i=1; i<n; i++) {
cout << 1 << ' ';
}
cout << m-n + 1 << endl;
cout << m-n;
}
}
return 0;
}
更好懂的思路:我们始终设 K = 1 K = 1 K=1,这样一来,我们必须满足每个元素都 > = 2 >=2 >=2,所以 S > = 2 ∗ N S >= 2*N S>=2∗N, S / N > = 2 S/N >= 2 S/N>=2,否则不满足条件。