HDU 4982 Goffi and Squary Partition(BestCoder Round #6)
2015-10-24 11:27
323 查看
[align=left]Problem Description:[/align]
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.
[align=left]Input:[/align]
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).
[align=left]Output:[/align]
For each case, if there exists a squary partition of n to k distinct positive integers, output "YES" in a line. Otherwise, output "NO".
[align=left]Sample Input:[/align]
2 2
4 2
22 4
[align=left]Sample Output:[/align]
NO
YES
YES
题意:给出n和k的值,问能否找到一个序列满足以下条件:1.这个序列长度为k,这k个数的和是n;2.这k个数中存在任意k-1个数的和是任意一个数的平方。
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.
[align=left]Input:[/align]
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).
[align=left]Output:[/align]
For each case, if there exists a squary partition of n to k distinct positive integers, output "YES" in a line. Otherwise, output "NO".
[align=left]Sample Input:[/align]
2 2
4 2
22 4
[align=left]Sample Output:[/align]
NO
YES
YES
题意:给出n和k的值,问能否找到一个序列满足以下条件:1.这个序列长度为k,这k个数的和是n;2.这k个数中存在任意k-1个数的和是任意一个数的平方。
#include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<queue> #include<algorithm> 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; }
相关文章推荐
- Run Django on Tornado
- Go语言学习:Channel是什么?
- .NET平台BigO算法复杂度备忘
- 批量替换和转移目录的东东
- Java、Scala和Go语言多线程并发对比测试结果和结论
- GoAhead 嵌入式web
- scheduling algorithm
- Mac OS上设置Django开发环境
- 跟着猫哥学Golang[1] - Hello World!
- hdu1850 Being a good boy(尼姆博弈)
- Why The Golden Age Of Machine Learning is Just Beginning
- Scrambled Polygon - POJ 2007(求凸包)
- Go写的计费回调Server
- hdoj3966Aragorn's Story【树链刨分】
- EGOCache缓存框架详细讲解,egocache缓存框架
- 安卓系统学习--获取Google安卓系统源码
- 基于 Golang 的 xls 读取类库:xls
- GO项目目录
- POJ 2262 Goldbach's Conjecture
- Mongoose的用法笔记