Goffi and Squary Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1135 Accepted Submission(s): 385
Problem Description
Recently, Goffi is interested in squary partition of integers.
A set X of k distinct positive integers is called squary partition of n if and only if it satisfies the following conditions:
[ol]
A set X of k distinct positive integers is called squary partition of n if and only if it satisfies the following conditions:
[ol]
- the sum of
k
positive integers is equal to
n
- one of the subsets of
X
containing
k−1
numbers sums up to a square of integer.[/ol]
For example, a set {1, 5, 6, 10} is a squary partition of 22 because 1 + 5 + 6 + 10 = 22 and 1 + 5 + 10 = 16 = 4 × 4.
Goffi wants to know, for some integers n and k , whether there exists a squary partition of n to k distinct positive integers.
Input
Input contains multiple test cases (less than 10000). For each test case, there's one line containing two integers
n
and
k
(
2≤n≤200000,2≤k≤30
).
Output
For each case, if there exists a squary partition of
n
to
k
distinct positive integers, output "YES" in a line. Otherwise, output "NO".
Sample Input
2 2 4 2 22 4
Sample Output
NO YES YES
Source
思路:枚举每一个完全平方数判断是否能有k-1个数组成
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<vector>
using namespace std;
const int maxn=10010;
int n,k;
bool judge(int num){
int cnt=k-2,mod=n-num*num;
if(mod==0)return false;
int minn=cnt*(cnt+1)/2;
if(mod<=cnt){
minn=minn+cnt-mod+1;
cnt++;
}
if(minn+mod>=n)return false;
int num2=n-minn-mod;
if(num2<=cnt)return false;
cnt++;
if(mod==cnt||mod==cnt+1){
if(num2==mod)return false;
}
return true;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&k)!=EOF){
int num=sqrt(n);
bool flag=false;
for(i=num;i>=1;--i){
if(judge(i)){
flag=true;
break;
}
}
if(flag){
printf("YES\n");
}
else {
printf("NO\n");
}
}
return 0;
}