【HDOJ】4982 Goffi and Squary Partition

题意就是整数划分,选出和为n的K个整数,其中K-1个数的和为完全平方数S。
选择整数时需要从1,2,3..连续选择,当选择整数与n-S相等时,需要跳过n-S,即选择n-S+1。
如此选择K-2个数,从而可确定第K-1个数,若该数已经出现(小于或等于K-2),则划分失败;
若第K-1个数不等于n-S,则肯定划分成功,否则K-1个数若等于n-S。
即需要通过将第K-2个数+1,同时第K-1个数-1得到正确的划分,并且需要保证调整后第K-2个数仍小于第K-1个数,因此,两数之间的距离至少大于2。

 1 #include <cstdio>
 2 #include <cmath>
 3
 4 int n, k;
 5
 6 bool judge(int s) {
 7     int p, q;
 8     int i, j = 0, sum = 0, tmp;
 9
10     p = s*s;
11     if (n == p)
12         return false;
13     q = n-p;
14
15     for (i=1; i<=k-2; ++i) {
16         ++j;
17         if (j == q)
18             ++j;
19         sum += j;
20     }
21     if (sum >= p)
22         return false;
23     tmp = p - sum;
24     if (tmp <= j)
25         return false;
26     if (tmp == q) {
27         if (q <= j+2)
28             return false;
29     }
30     return true;
31 }
32
33 int main() {
34     int m;
35
36     while (scanf("%d %d", &n, &k) != EOF) {
37         m = sqrt((n-1)*1.0);
38
39         if (judge(m))
40             printf("YES\n");
41         else
42             printf("NO\n");
43     }
44
45     return 0;
46 }
时间: 2024-10-27 05:27:47

【HDOJ】4982 Goffi and Squary Partition的相关文章

HDU 4982 Goffi and Squary Partition(推理)

HDU 4982 Goffi and Squary Partition 思路:直接从完全平方数往下找,然后判断能否构造出该完全平方数,如果可以就是yes,如果都不行就是no,注意构造时候的判断,由于枚举一个完全平方数,剩下数字为kk,构造的时候要保证数字不重复 代码: #include <cstdio> #include <cstring> #include <cmath> int n, k; bool judge(int num) { int yu = num * n

HDU 4982 Goffi and Squary Partition(BestCoder Round #6 1002)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4982 解析: 题意: 给你N和K,问能否将N拆分成K个互不相同的正整数,并且其中K-1个数的和为完全平方数     实际上,一拿到题目,我先想了下这里会不会存在负数或者0,后来经过几次华丽丽的WA之后发现,这题确实不用考虑到非正数.但是还是想吐槽一下,这个题目的意思也有点儿太不明晰了...     再说回题目来,我们枚举所有的完全平方数i*i,然后看能否将他拆成k-1个数,并且用不到n-i*i.所以

HDOJ 4982 Goffi and Squary Partition

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 374    Accepted Submission(s): 145 Problem Description Recently, Goffi is interested in squary partition of integers.

hdu 4982 Goffi and Squary Partition

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1442 Accepted Submission(s): 479 Problem Description Recently, Goffi is interested in squary partition of integers. A set

[BestCoder Round #6] hdu 4982 Goffi and Squary Partition(构造)

Goffi and Squary Partition 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 p

【HDOJ】4983 Goffi and GCD

题意说的非常清楚,即求满足gcd(n-a, n)*gcd(n-b, n) = n^k的(a, b)的不同对数.显然gcd(n-a, n)<=n, gcd(n-b, n)<=n.因此当n不为1时,当k>2时,不存在满足条件的(a,b).而当k=2时,仅存在(n, n)满足条件.因此仅剩n=1以及k=1需要单独讨论:当n = 1时,无论k为何值,均有且仅有(1,1)满足条件,此时结果为1:当k = 1时,即gcd(n-a, n)*gcd(n-b, n) = n,则令gcd(n-a, n) =

hdu 4982 Goffi and Squary Partition (枚举)

//给出n和k,求k个不同的正整数,使其中k-1个数能组成平方数,k个数的和为n.有解输出YES,无解输出NO. # include <stdio.h> # include <string.h> # include <algorithm> # include <math.h> using namespace std; int n,k; bool judge(int num) { int yy=num*num;//k-1个数相加 int kk=n-yy; if

hdu4982 Goffi and Squary Partition (DFS解法)

BestCoder Round #6 B http://acm.hdu.edu.cn/showproblem.php?pid=4982 Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 308    Accepted Submission(s): 106 Problem Descrip

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1