博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4982 Goffi and Squary Partition(BestCoder Round #6)
阅读量:7071 次
发布时间:2019-06-28

本文共 1914 字,大约阅读时间需要 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
#include
#include
#include
#include
#include
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\n"); else printf("NO\n"); } return 0;}

转载于:https://www.cnblogs.com/syhandll/p/4906484.html

你可能感兴趣的文章
XML Schema的基本语法(转)
查看>>
DEDECMS删除 提取第一个图片为缩略图 自动勾选默认选择设置方法
查看>>
堆 C语言实现
查看>>
PHP解决中文乱码
查看>>
springMVC <mvc:interceptors>拦截器的使用
查看>>
the Root Of AVL
查看>>
Java基础篇-File IO编程
查看>>
服务器环境部署
查看>>
2019春第十周作业
查看>>
(十六)Activitivi5之内置用户组(角色)设计表以及IdentityService
查看>>
python曲线拟合
查看>>
BUAA 2014级数据结构第五次上机 二叉树之数组转换广义表
查看>>
EF执行出错~NotSupportedException
查看>>
Linux & Vim Command Wallpaper
查看>>
Linux常用命令备忘
查看>>
小程序右上角转发分享web-view页面(备份前端网)
查看>>
virtualbox linux虚拟机相关
查看>>
关于.net和java我的见解
查看>>
【Android】设置Dialog点击屏幕不消失
查看>>
ConcurrentDictionary与Dictionary
查看>>