思维|Codeforces Round #643 (Div. 2) D.Game With Array

Codeforces Round #643 (Div. 2) D.Game With Array 题目链接
Petya and Vasya are competing with each other in a new interesting game as they always do.
At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that 0≤K≤S.
In order to win, Vasya has to find a non-empty subarray in Petya’s array such that the sum of all selected elements equals to either K or S?K. Otherwise Vasya loses.
You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
Input The first line contains two integers N and S (1≤N≤S≤1e6) — the required length of the array and the required sum of its elements.
Output 【思维|Codeforces Round #643 (Div. 2) D.Game With Array】If Petya can win, print “YES” (without quotes) in the first line. Then print Petya’s array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.
If Petya can’t win, print “NO” (without quotes).
You can print each letter in any register (lowercase or uppercase).
Examples input

1 4

output
YES 4 2

input
3 4

output
NO

input
3 8

output
YES 2 1 5 4

典型的构造题~
我们先考虑输的情况,即s < 2 ? n s<2*n s<2?n,为什么?
可以看一下我的思维证明,首先,此时构造的数组必然有1 1 1,这点很好想,那么k = 1o rk = s ? 1 k=1\ or\ k=s-1 k=1 or k=s?1就排除了
下面考虑再选一个数i i i,则可构成s = i + 1 s=i+1 s=i+1
下面考虑再选一个数j j j,则可构成s = i + j + 1 s=i+j+1 s=i+j+1
? \cdots ?
不难发现每多选一个数,就能多一种选的组合而n n n 个数恰好n n n 种组合,即1 ? s 1-s 1?s 的所有取值都可取到,所以此时一定会输
当s > 2 ? n s>2*n s>2?n 时, s > 2 ? ( n ? 1 ) + 2 s>2*(n-1)+2 s>2?(n?1)+2, s ? 2 ? ( n ? 1 ) > 2 s-2*(n-1)>2 s?2?(n?1)>2,所以我们可以选k = 1 k=1 k=1,构造数组: 2 , 2 , ? ? , s ? 2 ? ( n ? 1 ) 2,2,\cdots,s-2*(n-1) 2,2,?,s?2?(n?1)
AC代码如下:
#include using namespace std; typedef long long ll; main(){ int n,s; cin>>n>>s; if(s>=2*n){ puts("YES"); for(int i=0; i

    推荐阅读