BestCoder Round #6(1002)hdu4982(贪心)

Goffi and Squary Partition


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 18    Accepted Submission(s): 12


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 k1 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 ( 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

题意:构造k个数,使得总和为n,且存在k-1个数的总和为一个完全平方数,能构造输出YES,否则输出NO

思路:贪心,先找到小于n且最接近n的完全平方数x,然后构造前k-1个数使得总和为x,那么第k个数固定为y=n-x

          做到这里就可以分情况讨论了

         1.  y不等于1~k-1里的数,如果 y 不等于 k 则有解,如果 y=k ,如果x-k*(k-1)/2=1,则无解,否则有解

         2.  y等于1~k-1里的数,显然y不可能等于k了,则将1~k-1里等于y的数替换为k即可,只要前k-1个数的总和<=x则有解,否则无解

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include 
      
      
       
       
#include 
       
       
         using namespace std; int a[460]; int main() { int n,k; int i; int d=0; for(i=1;i*i<=200000;i++)a[d++]=i*i; while(scanf("%d%d",&n,&k)!=EOF) { int y; int ok=0; for(i=0;i 
        
          =n)break; i--; int total=a[i]; y=n-total; if(y<1)printf("NO\n"); else{ int sum=0; for(i=1;i<=k-1;i++){ if(i!=y)sum+=i; else sum+=k; } if(sum>total)printf("NO\n"); else { if(k!=y)printf("YES\n"); else { if(total-sum==1)printf("NO\n"); else printf("YES\n"); } } } } } 
         
       
      
      
     
     
    
    
   
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值