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. 43.VUE学习之--组件之使用.sync修饰符与computed计算属性超简单的实现美团购物车原理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. A1075 PAT Judge (25)(25 分)

    A1075 PAT Judge (25)(25 分) The ranklist of PAT is generated from the status list, which shows the sc ...

  3. U1

    如果 activity_main.xml没有xml代码可以对图像右键 go to mxl同时可以在design  和  text  切换 在安装了Android Studio3.3版本之后,第一个He ...

  4. 笔记-python tutorial-9.classes

    笔记-python tutorial-9.classes 1.      Classes 1.1.    scopes and namespaces namespace: A namespace is ...

  5. 13,scrapy框架的日志等级和请求传参

    今日概要 日志等级 请求传参 如何提高scrapy的爬取效率 一.Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时,在终端里打印输出的就是scrapy ...

  6. 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist

    不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句:  ./spark-shell  --master spark://ma ...

  7. Python框架之Django学习笔记(十四)

    Django站点管理(续·完) 本想昨天更新的,谁曾想昨天竟然是工作日!我就不吐槽昨天加班到十一点多了,需求增加无疑让我等蛋疼不已,忽而想起一首打油诗: 明月几时有,把酒问群友.不知这次版本,今晚能出 ...

  8. Python-S9——Day110-Git继续

    1 当日内容概要 2 内容回顾 3 Git版本控制之多人协同开发 4 Git版本控制之fork 5 版本控制之其他 6 Redis之字典基本操作 7 Django中操作Redis 8 Django缓存 ...

  9. 学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符的嵌套来完成此题:C表示。

    # -*- coding: utf8 -*- # Author:wxq #python 2.7 #题目:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用利用条件运算符 ...

  10. rpm包管理 命令

    rpm -ivh package.rpmrpm -ivh --force  package_name.rpm # ...conflict with...rpm -ivh --nodeps packag ...