I.Sum of One-sequence | |||||
| |||||
Description | |||||
We say that a sequence of integers is a onesequence if the difference between any two consecutive numbers in this sequence is -1 or 1 and its first element is 0. More precisely: a1, a2, ..., an is a one-sequence if: · for any k (1 ≤ k < n): |ak - ak+1|=1, · a1=0. You are given n and s — sum of all elements in a. Construct a one-sequence with the given parameters. | |||||
Input | |||||
The first line contains a pair of integers n and s (1≤n ≤10000, |s| ≤ 50000000). | |||||
Output | |||||
Print “Yes" if required sequence exist ... or "No" if it doesn't exist. | |||||
Sample Input | |||||
8 4 | |||||
Sample Output | |||||
Yes |
题意:给你一个长度为n的数组,a1 =0,|ak-a(k+1)|=1....然后问你是否可以产生和为s的数
分析:这真是赤裸裸的靠数学智商啊。。。。哎。。。。
n = 1 0
n = 2 1 -1
n = 3 2 0 -2
n = 4 3 1 -1 -3
。。。。。。。
找规律吧,奇数+奇数=偶数,那么我们找由多少行是奇数行。比如现在n=4,总共由2个奇数行吧,这两行随意取个数相加答案就是偶数,因为其余行都是偶数,所以我们找出有多少个奇数行就行,如果奇数行行数是奇数那么答案一定是个奇数,否则是偶数
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int n,s;
while(cin>>n>>s)
{
s= abs(s);
int sum = n*(n-1)/2;
if(abs(sum) >= s && s%2 == (n/2)%2)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}