HDU-4982-Goffi and Squary Partition【贪心】【构造】
来源:互联网 发布:桂林力港网络 编辑:程序博客网 时间:2024/05/21 01:44
HDU-4982-Goffi and Squary Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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:
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.
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
题目链接:HDU-4982
题目大意:给出n,k。问是否存在一个k个数字的序列,和为n,并且其中存在k-1个数字的和是一个完全平方数。
例如:{1, 5, 6, 10}, n为22,k为 3的时候, 1 + 5 + 6 + 10 = 22 and 1 + 5 + 10 = 16 = 4 × 4.所以符合。
(理解了好久的题解才明白这么做,orz)
以下是代码:
#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;int main(){ int n,k; while(scanf("%d%d",&n,&k) != EOF) { int flag = 0; int sum = k * (k - 1) / 2; for (int i = 1; i * i < n; i++) //遍历可能的完全平方数 { int squre = i * i; int need = n - squre; //可能的第k个数 if (squre < sum) continue; //前 k-1个数的和大于完全平方数,则不符合 if (need <= k - 1 && sum + k > n) continue; //如果第k个数 <= k-1,那么构造这个完全平方数时用到的最小的数是k,并且此时总和>n,不符合 if (squre == sum + 1 && need == k) continue; //如果完全平方数 == sum+1,说明在构造完全平方数时需要用到k,而需要的第k个数也是k,产生矛盾 flag = 1; break; } if (flag) printf("YES\n"); else printf("NO\n"); } return 0;}
- HDU-4982-Goffi and Squary Partition【贪心】【构造】
- hdu 4982 Goffi and Squary Partition(构造)
- hdu 4982 Goffi and Squary Partition
- hdu 4982 Goffi and Squary Partition (枚举)
- HDU 4982 Goffi and Squary Partition
- [BestCoder Round #6] hdu 4982 Goffi and Squary Partition(构造)
- HDU 4982 Goffi and Squary Partition(推理)
- Goffi and Squary Partition-HDU-4982 BC.6.B
- hdu 4982 Goffi and Squary Partition(BestCoder Round #6)
- hdu 4982 Goffi and Squary Partition (想法题)
- HDU 4982 Goffi and Squary Partition(枚举)
- HDU 4982/BC 6B Goffi and Squary Partition
- hdu 4982 Goffi and Squary Partition(思路)
- 【杂题】 HDOJ 4982 Goffi and Squary Partition
- HDOJ 4982 Goffi and Squary Partition
- HDU4982 Goffi and Squary Partition
- HDU 4982 Goffi and Squary Partition(BestCoder Round #6 1002)
- 【CUGBACM15级BC第六场 B】hdu 4982 Goffi and Squary Partition
- 【笔记】git常用命令
- C++ Primer Plus 编程练习ch5
- equals和equalsIgnoreCase的区别
- oracle 执行语句
- Hession 简单尝试
- HDU-4982-Goffi and Squary Partition【贪心】【构造】
- PHPCrawl爬虫库抓取酷狗歌单
- 各种经典的进度条效果
- 主机规划与磁盘分区
- 数据结构(寒假小结)——2.线性表
- android实现全屏 去标题去任务栏
- 自然场景文字检测和识别
- viewpager切换添加动画效果
- 监考风波