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): 902    Accepted Submission(s): 315


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]
  • the sum of  k  positive integers is equal to  n
  • one of the subsets of  X  containing  k1  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  ( 2n200000,2k30 ).
 

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
 


题目大意:给出一组n和k,问是否存在k个不同的正整数的和为n,而在这k个数中(k-1)个数的和是个平方数。


解题思路:对于n和k,应该是构造出(k-1)个数的和是一个小于n的平方数,同时注意到是k个不同的数,要注意
第k个数不能再构造的(k-1)个数里面。


代码如下:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int n,k;
int Judge(int m)
{
    int i,j;
    //平方数x_2
    int x_2=m*m;
    //第k个数n_k
    int n_k=n-x_2;

    if(n_k==0)
        return 0;

    int sum=0;
    int num=0;
    //用连续的自然数构造(k-2)个数,判断第k-1个数
    for(j=0;j<k-2;j++)
    {
        num++;
        //如果第k个数出现,跳过
        if(num==n_k)
            num++;
        sum+=num;
    }
    //
    if(sum+n_k>n)
        return 0;
    //
    int n_k_1=n-sum-n_k;//第k-1个数的最小值
    if(n_k_1<=num)
        return 0;
    //判断第k-1个数
    num++;
    if(n_k==num||n_k==num+1)
    {
        if(n_k_1==n_k)
            return 0;
    }
    return 1;
}
int main()
{
    while(~scanf("%d%d",&n,&k))
    {
        int m=sqrt(n*1.0);
        int flag=0;
        for(int i=m;i>=1;i--)
        {
            if(Judge(i))
            {
                printf("YES\n");
                flag=1;
                break;
            }
        }
        if(!flag)
            printf("NO\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值