码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4982 Goffi and Squary Partition(推理)

时间:2014-08-24 23:54:13      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   io   for   ar   art   代码   amp   

HDU 4982 Goffi and Squary Partition

思路:直接从完全平方数往下找,然后判断能否构造出该完全平方数,如果可以就是yes,如果都不行就是no,注意构造时候的判断,由于枚举一个完全平方数,剩下数字为kk,构造的时候要保证数字不重复

代码:

#include <cstdio>
#include <cstring>
#include <cmath>

int n, k;

bool judge(int num) {
    int yu = num * num;
    int kk = n - yu;
    if (kk == 0) return false;
    int sum = 0;
    int cnt = 0;
    for (int i = 0; i < k - 2; i++) {
	cnt++;
	if (cnt == kk) cnt++;
	sum += cnt;
    }
    if (sum + kk >= n) return false;
    int need = n - sum - kk;
    if (need <= cnt) return false;
    cnt++;
    if (kk == cnt || kk == cnt + 1) {
	if (need == kk) return false;
    }
    return true;
}

bool solve() {
    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 (solve()) printf("YES\n");
	else printf("NO\n");
    }
    return 0;
}


HDU 4982 Goffi and Squary Partition(推理)

标签:style   color   os   io   for   ar   art   代码   amp   

原文地址:http://blog.csdn.net/accelerator_/article/details/38804139

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!