[BestCoder Round #6] hdu 4982 Goffi and Squary Partition(构造)

Goffi and Squary Partition

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:
  1. the sum of k positive integers is equal to n
  2. one of the subsets of X containing k?1 numbers sums up to a square of integer.
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

题意为:

给出n,k,问能不能把n拆成k个不同的正整数相加,且其中k-1个数的和为完全平方数。

比如 n=22 ,k =4, 22可以拆成 1+5+6+10,且 1+5+10=4*4

BESTCODER主页上的题解:

PS:这道题目原来是要求输出一种可行方案的,所以下面题解是按照输出方案的思想搞的。
分析:
我们尝试枚举那个完全平方数 S,然后看能否将他拆分为 K-1 个数,并且不用到N-S
这一步可以用贪心+一次调整来搞定。为了保证 K-1 个数都不同,我们尝试尽量用 1,2,3...这些连续自然数来构造,如果 N-S 出现在这些数中,那么将 N-S 移除,再新加一个数。如果这样都不能拆分成 K-1 个数,那么这个 S 肯定不行。
现在考虑已经用上述方法拆分了,我们需要判断这个拆分是否可行。会产生问题的只有最后一个数,这个数可能和 N-S 一样,也可能出现在之前的序列。如果是出现在之前的序列,那么这个拆分也是不靠谱的。如果和 N-S 一样,那么分两种情况
1.	N-S 曾出现在之前的序列,那么显然这个拆分也是不靠谱的
2.	N-S 比倒数第二个数大,对于这种我们可以通过调整最后一个数和倒数第二个数的大小,来使得这个拆分成立,假设最后一个数为 a,倒数第二个为 b,只要 a-1,b+1 就好了。当然如果 a-1 = b+1 这个拆分也是不靠谱的这道题目就这样搞定了,其实没必要找所有的完全平方数,只要找小于 N 与 N 最接近的完全平方数就好了。

枚举出一个完全平方数,那么剩下的数就确定了,下面就开始判断剩下的那个数是否符合题意。

构造出完全平方数的k-1个数,从小到大,贪心,1,2,3.....

#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
int n,k;

bool judge(int s)//s为枚举出来的完全平方数
{
    int f=n-s;//剩下的
    int t=1;//t为构造出来的数
    for(int i=1;i<=k-2;i++)//开始构造k-1个数中的前k-2个,剩下的那个数为s
    {
        if(t==f)
            t++;
        s-=t;
        if(s<=t)
            return false;
        t++;
    }
    t--;
    //到此为止,t为构造出的k-1个数中的倒数第二个,s为倒数第一个
    while(1)
    {
        if(s<=t)//最后一个数出现在前面的序列中
            return false;
        if(s!=f)
            return true;
        //s==f时,调整s,t
        s--;
        t++;
        //s+t和不变。
    }
}

bool ok()
{
    int m=(int)sqrt(n);
    if(m*m==n)//枚举时要求完全平方数<n才可以
        m--;
    while(m)
    {
        if(judge(m*m))
            return true;
        m--;
    }
    return false;
}

int main()
{
    while(scanf("%d%d",&n,&k)==2)
    {
        if(ok())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
时间: 2024-10-12 10:48:53

[BestCoder Round #6] hdu 4982 Goffi and Squary Partition(构造)的相关文章

HDU 4982 Goffi and Squary Partition(推理)

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 * n

HDU 4982 Goffi and Squary Partition(BestCoder Round #6 1002)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4982 解析: 题意: 给你N和K,问能否将N拆分成K个互不相同的正整数,并且其中K-1个数的和为完全平方数     实际上,一拿到题目,我先想了下这里会不会存在负数或者0,后来经过几次华丽丽的WA之后发现,这题确实不用考虑到非正数.但是还是想吐槽一下,这个题目的意思也有点儿太不明晰了...     再说回题目来,我们枚举所有的完全平方数i*i,然后看能否将他拆成k-1个数,并且用不到n-i*i.所以

hdu 4982 Goffi and Squary Partition

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1442 Accepted Submission(s): 479 Problem Description Recently, Goffi is interested in squary partition of integers. A set

hdu 4982 Goffi and Squary Partition (枚举)

//给出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

HDOJ 4982 Goffi and Squary Partition

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 374    Accepted Submission(s): 145 Problem Description Recently, Goffi is interested in squary partition of integers.

[BestCoder Round #6] hdu 4981 Goffi and Median (水题)

Goffi and Median Problem Description A median in a sequence with the length of n is an element which occupies position number ?n+12? after we sort the elements in the non-decreasing order (the elements are numbered starting with 1). A median of an ar

【HDOJ】4982 Goffi and Squary Partition

题意就是整数划分,选出和为n的K个整数,其中K-1个数的和为完全平方数S.选择整数时需要从1,2,3..连续选择,当选择整数与n-S相等时,需要跳过n-S,即选择n-S+1.如此选择K-2个数,从而可确定第K-1个数,若该数已经出现(小于或等于K-2),则划分失败:若第K-1个数不等于n-S,则肯定划分成功,否则K-1个数若等于n-S.即需要通过将第K-2个数+1,同时第K-1个数-1得到正确的划分,并且需要保证调整后第K-2个数仍小于第K-1个数,因此,两数之间的距离至少大于2. 1 #inc

hdu4982 Goffi and Squary Partition (DFS解法)

BestCoder Round #6 B http://acm.hdu.edu.cn/showproblem.php?pid=4982 Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 308    Accepted Submission(s): 106 Problem Descrip

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat