Goffi and Squary Partition

Time Limit: / MS (Java/Others)    Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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:
[ol]
the sum of k positive integers is equal to n
one of the subsets of X containing k− numbers sums up to a square of integer.
[/ol]
For example, a set {, , , } is a squary partition of because + + + = and + + = = × . 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 ). 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 Sample Output
NO
YES
YES

题意:输入整数n和k,要求把n分成k个数之和的形式,其中存在k-1个数之和为一个完全平方数,而且这k个数各不相同。
分析: 我们尝试枚举那个完全平方数 S,然后看能否将他拆分为 K-1 个数,并且不用到N-S 这一步可以用贪心+一次调整来搞定。为了保证 K-1 个数都不同,我们尝试尽量

用 1,2,3...这些连续自然数来构造,如果 N-S 出现在这些数中,那么将 N-S 移除,再新加一个数。最后一个数由S-sum(1~k-2)(包括调整过的)来得到。

  • 1.如果sum值大于S值,可以分成两种情况来看

1.1 前k-2个数中不存在N-S,那么原数列为1,2,3,....,k-2,其中的和大于等于S值,且最小的数为1,没有剩余的空间减少这k-2个数的和

1.2 前k-2个数中存在N-S,设x等于N-S那么原数列为1,2,....x-1,x+1,.....,k-1,其中多出来的空间为避免N-S,同样不存在剩余空间减少和

  • 2.如果倒数最后一个数在前面k-2个数中出现,由上面结论可知,必定存在冲突,且无法调整
  • 3.如果倒数最后一个数与N-S相等,那么可以使得倒数第一个数-1和倒数第二个数+1,这样的调整代价是最小的,如果这样的处理方式仍存在冲突,就为错
#include <cstdio>
using namespace std;
int pnt[],top;
int n,k;
bool check(int x)
{
int sum=,top=;
int r=n-x,cc=,cnt=;
pnt[top++]=;
for(int i=; i<k-; i++)
{
cc++;
if(cc==r) cc++;
pnt[top++]=cc;
sum+=cc;
}
if(sum>=x) return false;
pnt[top]=x-sum;
if(pnt[top]<=pnt[top-]) return false;
if(pnt[top]==r)
{
if(pnt[top-]+>=pnt[top]-) return false;
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
int flag=;
for(int i=; i<=; i++)
{
if(i*i>=n) break;
if(check(i*i))
{
printf("%d\n",i*i);
printf("YES\n");
flag=;
break;
}
}
if(flag) continue;
printf("NO\n");
}
return ;
}

hdu 4982 Goffi and Squary Partition的更多相关文章

  1. HDU 4982 Goffi and Squary Partition(推理)

    HDU 4982 Goffi and Squary Partition 思路:直接从全然平方数往下找,然后推断是否能构造出该全然平方数,假设能够就是yes,假设都不行就是no.注意构造时候的推断,因为 ...

  2. 【HDOJ】4982 Goffi and Squary Partition

    题意就是整数划分,选出和为n的K个整数,其中K-1个数的和为完全平方数S.选择整数时需要从1,2,3..连续选择,当选择整数与n-S相等时,需要跳过n-S,即选择n-S+1.如此选择K-2个数,从而可 ...

  3. hdu4982 Goffi and Squary Partition (DFS解法)

    BestCoder Round #6 B http://acm.hdu.edu.cn/showproblem.php?pid=4982 Goffi and Squary Partition Time ...

  4. BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告

    题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...

  5. Goffi and Squary Partition

    题意: 给你N和K,问能否将N拆分成K个互不相同的正整数,并且其中K-1个数的和为完全平方数. PS:这道题目原来是要求输出一种可行方案的,所以下面题解是按照输出方案的思想搞的. 分析: 我们尝试枚举 ...

  6. HDU 4981 Goffi and Median(水)

    HDU 4981 Goffi and Median 思路:排序就能够得到中间数.然后总和和中间数*n比較一下就可以 代码: #include <cstdio> #include <c ...

  7. hdu 4983 Goffi and GCD(数论)

    题目链接:hdu 4983 Goffi and GCD 题目大意:求有多少对元组满足题目中的公式. 解题思路: n = 1或者k=2时:答案为1 k > 2时:答案为0(n≠1) k = 1时: ...

  8. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...

  9. hdu 4982 贪心构造序列

    http://acm.hdu.edu.cn/showproblem.php?pid=4982 给定n和k,求一个包含k个不相同正整数的集合,要求元素之和为n,并且其中k-1的元素的和为完全平方数 枚举 ...

随机推荐

  1. iOS常用网络库之AFNetWorking

    简介 ​     `AFNetworking`是iOS开发网络API中最常用的第三方库,`github`中的`star`数充分说明了它在iOS开发中第三方库中的江湖地位  github地址:[AFNe ...

  2. 修复 Firefox 下本地使用 Bootstrap 3 时 glyphicon 不显示问题

    本地开发使用 Firefox 调试,遇到了 glyphicon 图标不显示的问题,期初以为是路径问题,搜索一大圈后找到了答案,原来这是一个安全性的问题,于是问题就好办了,解决方案如下: 1. 在Fir ...

  3. 你不知道的函数节流,提高你的JS性能!

    浏览器的DOM计算处理非常耗费CPU时间,霸占内存,这对我们的开发来说是非常不友好的,,比如IE浏览器的onresize事件就可能在用户稍微拖动一下窗口时计算上千次,甚至更高频率直接让浏览器崩溃... ...

  4. Xcode导航栏不显示模拟器选择框ToolBar

    不显示ToolBar的小伙伴可能就是下面的样子: 全屏后就可以看到ToolBar,像下面这样: 刚开始还以为是模拟器没装,还傻不拉几的去下载模拟器,后来才发现,只要下面的操作即可显示 点击" ...

  5. python学习之——pip的安装与使用

    安装步骤: 前提:已经安装python 1.下载安装包并解压: easy_install 地址:https://pypi.python.org/pypi/setuptools#downloads pi ...

  6. iOS.ReactNative-5-make-react-native-to-support-dynamically-update

    基于React Native来实现App的热更新 热更新: 类似Hot-Patching,在程序不发布版本的前提下,对部分功能或者全部功能进行修改或者升级. JSX 1. JSX简介 1.1 http ...

  7. IIS发布WCF服务项目之本地

    最近由于项目需求,要做一个上传文件附件的功能,由于是多用户访问,所以这就用到了WCF服务,程序编写完成就需要发布了, 下面记录下发布到IIS的过程: 1,安装IIS 第一步:检查Windows7中II ...

  8. 获取当前运行dll文件的路径

    char moduledir[MAX_PATH];  GetModuleFileNameA(GetModuleHandleA("ppdl_BE081_BIW_seal_library.dll ...

  9. exp

    想将远程机器上的数据库备份,搞了一上午,终于解决了: exp 用户名/密码@ip/service名 file=地址+文件名

  10. ros下多机器人系统(1)

    multi-robot system 经过两个多月的ros学习,对ros的认识有了比较深入的了解,本篇博客主要记录在ros下开发多机器人系统以及对ros更深入的开发.本篇博客是假定读者已经学习完了全部 ...