HDU 4982 Goffi and Squary Partition(枚举)
来源:互联网 发布:校园网络暴力案例 编辑:程序博客网 时间:2024/05/29 14:31
Goffi and Squary Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 902 Accepted Submission(s): 315
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
Source
BestCoder Round #6
解题思路:对于n和k,应该是构造出(k-1)个数的和是一个小于n的平方数,同时注意到是k个不同的数,要注意
第k个数不能再构造的(k-1)个数里面。
代码如下:
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#include <limits.h>#define debug "output for debug\n"#define pi (acos(-1.0))#define eps (1e-6)#define inf (1<<28)#define sqr(x) (x) * (x)#define mod 1000000007using namespace std;typedef long long ll;typedef unsigned long long ULL;int n,k;int Judge(int m){ int i,j; //平方数x_2 int x_2=m*m; //第k个数n_k int n_k=n-x_2; if(n_k==0) return 0; int sum=0; int num=0; //用连续的自然数构造(k-2)个数,判断第k-1个数 for(j=0;j<k-2;j++) { num++; //如果第k个数出现,跳过 if(num==n_k) num++; sum+=num; } // if(sum+n_k>n) return 0; // int n_k_1=n-sum-n_k;//第k-1个数的最小值 if(n_k_1<=num) return 0; //判断第k-1个数 num++; if(n_k==num||n_k==num+1) { if(n_k_1==n_k) return 0; } return 1;}int main(){ while(~scanf("%d%d",&n,&k)) { int m=sqrt(n*1.0); int flag=0; for(int i=m;i>=1;i--) { if(Judge(i)) { printf("YES\n"); flag=1; break; } } if(!flag) printf("NO\n"); } return 0;}
0 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
- HDU 4982 Goffi and Squary Partition(推理)
- hdu 4982 Goffi and Squary Partition(BestCoder Round #6)
- hdu 4982 Goffi and Squary Partition(思路)
- Goffi and Squary Partition-HDU-4982 BC.6.B
- 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
- [BestCoder Round #6] hdu 4982 Goffi and Squary Partition(构造)
- HDU 4982 Goffi and Squary Partition(BestCoder Round #6 1002)
- HDU4982 Goffi and Squary Partition
- 【CUGBACM15级BC第六场 B】hdu 4982 Goffi and Squary Partition
- Android线程间通信的3中方式
- 【机房收费系统】第一次机房收费系统之er图、实体联系图小试
- STM8L系列产品分类
- Android开发模仿微信发表朋友圈图片多选及图片浏览实现-Assur
- win10初体会
- HDU 4982 Goffi and Squary Partition(枚举)
- STM32两个hex文件合并问题
- 关于SOC中的总线AXI/AHB/APB
- Maximal Square
- 2. Browser 对象 - Window 对象(2)
- bc第六场Goffi and Squary Partition
- hdu 5317 RGCDQ
- 排序算法--希尔排序
- AS3和JS通信并调用qq空间分享界面