D. Game With Array
Posted yin101
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 NN positive integers. Sum of all elements in his array should be equal to SS. Then Petya has to select an integer KK such that 0≤K≤S0≤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 KK or S−KS−K. Otherwise Vasya loses.
You are given integers NN and SS. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.
The first line contains two integers NN and SS (1≤N≤S≤1061≤N≤S≤106) — the required length of the array and the required sum of its elements.
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 NN positive integers with sum equal to SS. In the third line print KK. 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).
地址:http://codeforces.com/contest/1355/problem/D
思路:由于数据过大,且要输出数组,所以无法枚举,通常时令数组的前 n-1个数为一个常数,再由最后一个元素去根据需求调整。此题中数组和K都是自己设定,只要有一种可能PETYA就赢,那么设K=1,数组中的前n-1个元素都设为2,第n个元素为S-(n-1)*2 ,只要第n个元素大于1 ,那么就成立.
int main() { int t = 1; while (t--) { int n, s; cin >> n >> s; if (s-2*(n-1)<=1) cout << "NO "; else { cout << "YES "; for (int i = 1; i < n; i++) cout << "2 "; cout << s - 2 * (n - 1) <<endl; cout << "1" << endl; } } return 0; }
以上是关于D. Game With Array的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #643 (Div. 2) D. Game With Array(构造)
Codeforces Round #643 (Div. 2) D. Game With Array(构造)
D. Game With Array1400 / 思维 构造
Codeforces Round #534 (Div. 2) D. Game with modulo(取余性质+二分)
AtCoder AISing Programming Contest 2019 Task D. Nearest Card Game