Goffi and Squary Partition

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

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 k−1 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 (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
 
Source
 
构造数组出来,真的难想到,无比佩服能够想到的大牛。。分析都在代码里。。
/**
题意:给你k个数(各不相同的正整数),它们的和是n,问是否存在k-1个数的和是某个数的平方?
分析:我们假设其中k-1个数个数的平方为 m , m 应该不小于 k*(k-1)/2,不然就会有重复的 。
我们可以通过构造一个序列来判断是否满足条件。
*/
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL; int n,k;
bool judge(int m){
int t = n-m, sum = (k-)*k/;
if(sum>m) return false; ///k-1项最小都要 sum
int count=,x=;
for(int i=;i<k-;i++){ ///构造出一个序列包含剩下的序列中的 k - 2 项
x++;
if(t==x) x++;
count+=x;
}
int lastnum = n-count-t; ///剩下的序列中的第 k-1 个数
if(lastnum<=x) return false; ///如果最后一个数小于 x ,那么之前肯定存在过了
x++;
if(t==x||t==x+){ ///如果 t == x 或者 t==x+1 那么当 第k-1个数等于t 的时候我们无法通过改变前k-2
///项的值令 lastnum != t 了,如果t更大一些,我们是可以通过改变 k-1这个序列的值
///来让 lastnum != t 的.
if(lastnum==t) return false;
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF){
bool flag = false;
for(int i=;i*i<n;i++){
int m = i*i;
if(judge(m)){
flag = true;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}

hdu 4859(思路题)的更多相关文章

  1. hdu 4908(思路题)

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  3. hdu 5191(思路题)

    Building Blocks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  4. hdu 5101(思路题)

    Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. hdu 5063(思路题-反向操作数组)

    Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. hdu 4956(思路题)

    Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. hdu 5400(思路题)

    Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  8. HDU 1173 思路题

    题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...

  9. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

随机推荐

  1. Ubuntu samba 安装与配置 实现windows和虚拟机中的Ubuntu共享文件

    2.    安装sumba服务 sudo apt-get install samba samba-common 这里出现了小问题, Ubuntu上安装samba不能安装的问题,“下列的软件包有不能满足 ...

  2. 学习pytho第五天 循环

    age_of_Bob = 56#定义年龄 count = 0#头部计数 while True:#while循环 if count ==3:#如果次数==3: break#退出 guess_age = ...

  3. music21 关联 MuseScore 和 Lilypond

    在python安装 music21后,需要关联 musescore 或 lilypond 才能可以用图形化的形式看到 乐谱. 因此 在安装 music21后,需要配置环境变量,yvivid 在 mus ...

  4. B1019 数字黑洞 (20分)

    B1019 数字黑洞 (20分) 给定任一个各位数字不完全相同的 4 位正整数,如果我们先把 4 个数字按非递增排序,再按非递减排序,然后用第 1 个数字减第 2 个数字,将得到一个新的数字.一直重复 ...

  5. A1025 PAT Ranking (25)(25 分)

    A1025 PAT Ranking (25)(25 分) Programming Ability Test (PAT) is organized by the College of Computer ...

  6. HOJ_14001 Just Terraffic!

    题意相对来说比较扭曲..所以来说下模型,具体做法有兴趣的孩纸去问度娘或者波塞冬吧~~ 给出一个序列长度,并且输入该序列,该序列的含义是横坐标: 任何两个相邻坐标绝对值小于等于1000的必然为一个整体, ...

  7. 使用Spark Streaming + Kudu + Impala构建一个预测引擎

    随着用户使用天数的增加,不管你的业务是扩大还是缩减了,为什么你的大数据中心架构保持线性增长的趋势?很明显需要一个稳定的基本架构来保障你的业务线.当你的客户处在休眠期,或者你的业务处在淡季,你增加的计算 ...

  8. codeforce GYM 100741 A Queries

    A. Queries time limit per test:0.25 s memory limit per test:64 MB input:standard input output:standa ...

  9. Unable to execute dex: Multiple dex files define Lcom/myapp/R$array;

    Unable to execute dex: Multiple dex files define Lcom/myapp/R$array; 我这个问题最后解决方式是,吧工程里面用同一个v4包. 很明显, ...

  10. alert(1) to win部分解题

    XSS在线习题分析 (https://alf.nu/alert1) 1. Warmup function escape(s) { return '<script>console.log(& ...