HDU 4982 Goffi and Squary Partition(枚举)

来源:互联网 发布:校园网络暴力案例 编辑:程序博客网 时间:2024/05/29 14:31

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 24 222 4
     

    Sample Output
    NOYESYES
     

    Source
    BestCoder Round #6
     


    题目大意:给出一组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 1000000007using 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