zoukankan      html  css  js  c++  java
  • HDU 4982 Goffi and Squary Partition(BestCoder Round #6)

    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
     
    题意:给出n和k的值,问能否找到一个序列满足以下条件:1.这个序列长度为k,这k个数的和是n;2.这k个数中存在任意k-1个数的和是任意一个数的平方。
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int N=1e3+10;
    const int M=50000;
    const int INF=0x3f3f3f3f;
    
    int main ()
    {
        int n, k, sum, i, flag;
        int squre, remain;
    
        while (scanf("%d%d", &n, &k) != EOF)
        {
            flag = 0;
            sum = k*(k-1)/2; ///可以先令1~k-1这些数为前k-1个数(这是最小的k-1个数的和)
    
            for (i = 1; i*i < n; i++)
            {
                squre = i*i; ///完全平方数
                remain = n-squre; ///可能的第k个数
    
                if (sum > squre) continue; ///前k-1个数的和大于完全平方数,不符合题意
                if (remain <= k-1 && sum+k > n) continue; ///如果第k个数<=k-1,那么构造这个完全平方数时用到的最小的数是k,并且此时总和>n,不符合题意
                if (squre == sum+1 && remain == k) continue; ///如果完全平方数==sum+1,说明在构造完全平方数时需要用到k,而需要的第k个数也是k,产生矛盾
    
                flag = 1;
                break;
            }
    
            if (flag) printf("YES
    ");
            else printf("NO
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    调用Win32 API netapi32.dll 实现UNC(网络共享)连接的管理(一)
    一个重写Page基类的例子
    36进制进位算法例子
    关于.net 中调用script的alert后 css失效的办法
    Javascript:keyCode键码值表
    Url地址重写,利用HttpHander手工编译页面并按需生成静态HTML文件
    在.NET程序中正确使用String类型
    Sql Server中自动序号的方法
    托管和非托管的关系和区别
    Oracle 格式化
  • 原文地址:https://www.cnblogs.com/syhandll/p/4906484.html
Copyright © 2011-2022 走看看