HDU4982 Goffi and Squary Partition
来源:互联网 发布:用户管理系统java 编辑:程序博客网 时间:2024/05/17 22:56
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 setX 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 integersn and k , whether there exists a squary partition of n to k distinct positive integers.
A set
[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
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 24 222 4
Sample Output
NOYESYES
题意:将一个数n分成k个不同的数字,其中存在k-1个数字的和为平方数。如果存在输出YES,不存在输出NO。
题解:从1开始遍历平方数即可,然后判断该平方数是否符合情况。
如何判断:sum为前k-1个数的和,square为要判断的平方数,need为第k个数。
如果sum > square,说明最小的k-1个数的组合都比这个平方数大,不满足;
如果need <= k - 1 && sum + k > n,即第k个数在前k-1个数中,那么这k个数的最小和大于n时,不满足;
如果need == k && square - sum == 1,即第k个数是k,并且前k-1个数的和比平方数小1,此时必然会用到k这个数,所以重复,不满足。
代码:
#include <stdio.h>#include <string.h>int a[1000], tol;bool judge(int square, int n, int k){int sum = k * (k - 1) / 2;int need = n - square;if(sum > square)return false;if(need <= k - 1 && sum + k > n)return false;if(need == k && square - sum == 1)return false;return true;}bool solve(int n, int k){for(int i = 0; i < tol && a[i] < n; i++){if(judge(a[i], n, k))return true;}return false;}int main(){int n, k;tol = 0;for(int i = 1; i*i < 200005; i++)a[tol++] = i * i;while(~scanf("%d%d", &n, &k)){bool flag = solve(n, k);if(flag)puts("YES");elseputs("NO");}return 0;}
阅读全文
0 0
- HDU4982 Goffi and Squary Partition
- 【杂题】 HDOJ 4982 Goffi and Squary Partition
- hdu 4982 Goffi and Squary Partition(构造)
- hdu 4982 Goffi and Squary Partition
- HDOJ 4982 Goffi and Squary Partition
- hdu 4982 Goffi and Squary Partition (枚举)
- HDU 4982 Goffi and Squary Partition
- bc第六场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【贪心】【构造】
- hdu 4982 Goffi and Squary Partition(思路)
- [BestCoder Round #6] hdu 4982 Goffi and Squary Partition(构造)
- HDU 4982 Goffi and Squary Partition(BestCoder Round #6 1002)
- JAVA-20-数据约束、数据库设计三大范式、多表查询、存储过程、触发器、数据库权限
- 1-1 Linux系统安装
- 侦察兵
- BlockManager初始化和注册解密、BlockManagerMaster工作解密、BlockTransferService解密、本地数据读写解密、远程数据读写解密
- Android7.0中文API -- ListView
- HDU4982 Goffi and Squary Partition
- 常用的windows注册表大全
- Java对日期Date类型进行运算
- SSH 公钥免密码登陆
- POJ 1786 Bridge Hands 笔记
- JAVA-21-JDBC讲解,Driver、Connection、Statement、PreparedStatement、CallableStatement、ResultSet
- 513. Find Bottom Left Tree Value
- C++算法学习——经典的抽象设计——buffer(1)
- 结构化程序简介