Educational Codeforces Round 84 (Rated for Div. 2) A. Sum of Odd Integers(思维题)
You are given two integers nn and kk. Your task is to find if nn can be represented as a sum of kk distinct positive odd (not divisible by 22) integers or not.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤1051≤t≤105) — the number of test cases.
The next tt lines describe test cases. The only line of the test case contains two integers nn and kk (1≤n,k≤1071≤n,k≤107).
Output
For each test case, print the answer — "YES" (without quotes) if nn can be represented as a sum of kk distinct positive odd (not divisible by 22) integers and "NO" otherwise.
Example
Input
Copy
6 3 1 4 2 10 3 10 2 16 4 16 5
Output
Copy
YES YES NO YES YES NO
挺好一道思维水题,一开始没看到distinct直接WA到怀疑人生...
举个例子就能明白,比如k=4,组合出来的最小值为1+3+5+7=16,之后可以对某些数+2使得能够组合出例如3+3+5+7=18,所以k=4的话能组合出16 18 20 22......全为偶数,而如果k=3能组合出9 11 13......全为奇数,所以首先判断n和k的奇偶性,如果相同的话看看是否大于这个k能组合出的最小数即可。最小值可以用等差数列算,记得得开long long。
#include <bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { long long n,k; cin>>n>>k; long long mmin=(1+2*k-1)*k/2; if(k%2==0) { if(n>=mmin&&n%2==0)cout<<"YES"<<endl; else cout<<"NO"<<endl; } else { if(n>=mmin&&n%2!=0)cout<<"YES"<<endl; else cout<<"NO"<<endl; } } return 0; }