标签:hdu
//给出n和k,求k个不同的正整数,使其中k-1个数能组成平方数,k个数的和为n。有解输出YES,无解输出NO。 # include <stdio.h> # include <string.h> # include <algorithm> # include <math.h> using namespace std; int n,k; bool judge(int num) { int yy=num*num;//k-1个数相加 int kk=n-yy; if(kk==0) return false; int sum=0; int cot=0; for(int i=0; i<k-2; i++) { cot++; if(cot==kk) cot++; sum+=cot; } if(sum+kk>=n) return false; int need=n-sum-kk;//第k-1个数 if(need<=cot) return false; cot++; if(kk==cot||kk==cot+1) { if(need==kk) return false; } return true; } bool slove() { int m=sqrt(n*1.0); for(int i=m; i>=1; i--) { if(judge(i)) return true; } return false; } int main() { while(~scanf("%d%d",&n,&k)) { if(slove()) printf("YES\n"); else printf("NO\n"); } return 0; }
hdu 4982 Goffi and Squary Partition (枚举)
标签:hdu
原文地址:http://blog.csdn.net/lp_opai/article/details/38980451