hdu 4859(思路题)
Goffi and Squary Partition
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1171 Accepted Submission(s): 402
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.
4 2
22 4
YES
YES
/**
题意:给你k个数(各不相同的正整数),它们的和是n,问是否存在k-1个数的和是某个数的平方?
分析:我们假设其中k-1个数个数的平方为 m , m 应该不小于 k*(k-1)/2,不然就会有重复的 。
我们可以通过构造一个序列来判断是否满足条件。
*/
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef long long LL; int n,k;
bool judge(int m){
int t = n-m, sum = (k-)*k/;
if(sum>m) return false; ///k-1项最小都要 sum
int count=,x=;
for(int i=;i<k-;i++){ ///构造出一个序列包含剩下的序列中的 k - 2 项
x++;
if(t==x) x++;
count+=x;
}
int lastnum = n-count-t; ///剩下的序列中的第 k-1 个数
if(lastnum<=x) return false; ///如果最后一个数小于 x ,那么之前肯定存在过了
x++;
if(t==x||t==x+){ ///如果 t == x 或者 t==x+1 那么当 第k-1个数等于t 的时候我们无法通过改变前k-2
///项的值令 lastnum != t 了,如果t更大一些,我们是可以通过改变 k-1这个序列的值
///来让 lastnum != t 的.
if(lastnum==t) return false;
}
return true;
}
int main()
{
while(scanf("%d%d",&n,&k)!=EOF){
bool flag = false;
for(int i=;i*i<n;i++){
int m = i*i;
if(judge(m)){
flag = true;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
hdu 4859(思路题)的更多相关文章
- hdu 4908(思路题)
BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Proud Merchants HDU - 3466 (思路题--有排序的01背包)
Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...
- hdu 5191(思路题)
Building Blocks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5101(思路题)
Select Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- hdu 5063(思路题-反向操作数组)
Operation the Sequence Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 4956(思路题)
Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 5400(思路题)
Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 1173 思路题
题目大意 有n个地点(坐标为实数)需要挖矿,让选择一个地点,使得在这个地方建造基地,到n个地点的距离和最短,输出基地的坐标. 题解+代码: 1 /* 2 把这个二维分开看(即把所有点投影到x轴上,再把 ...
- 51nod P1305 Pairwise Sum and Divide ——思路题
久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...
随机推荐
- JZOJ 1264. 乱头发节
1264. 乱头发节(badhair.pas/c/cpp) (File IO): input:badhair.in output:badhair.out Time Limits: 1000 ms M ...
- [BSOJ2684]锯木厂选址(斜率优化)
Description 从山顶上到山底下沿着一条直线种植了n棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂.木材只能按照一个方向运输:朝山下运.山脚下有一个锯木厂 ...
- (WPF&Silverlight)silverlight自定义控件
2个半小时弄懂了自定义控件是怎么回事儿. 在silverlight中创建一个UserControl,把上面sliderbar的外观和功能都封装在里面. 以自定义控件mapslider控件为例: 1.首 ...
- luogu3338 [ZJOI2014]力
我发现我的构造方法好像不太一样而且比较显然?--先读入 \(q\) 数组(下表从零开始). 记 \(i < j\) 时,\(a_{i-j}=-1/i^2\):\(i > j\) 时,\(a ...
- 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站
现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- 【POJ 3764】The Xor-longest Path
题目 给定一个\(n\)个点的带权无根树,求树上异或和最大的一条路径. \(n\le 10^5\) 分析 一个简单的例子 相信大家都做过这题: 给定一个\(n\)个点的带权无根树,有\(m\)个询问, ...
- ArrayList以及Map小练
ArrayList常用方法 public static void main(String[] args) { List list = new ArrayList(); List list1 = new ...
- Java和C#中神奇的String
Java String: String 类适用于描述字符串事物.该类是不可以被继承的.我们主要学习: 1字符串特性.字符串最大的特性:一旦被初始化就不可以被改变.重赋值只是改变了引用. 2字符串操作. ...
- Python脚本获取Linux系统信息
# -*- coding:utf-8 -*- import os import subprocess import re import hashlib #对字典取子集 def sub_dict(for ...