题目来源:http://poj.org/problem?id=1032

题目大意:给定一个正整数N(5<=N<=1000),将N拆为若干个不同的数使得它们的乘积最大(找到一组互不相等,和为N,乘积最大的正整数)。

输入:N

输出:选择的数,升序输出。


Sample Input

7

Sample Output

3 4

假设不考虑拆成不等的数这个条件,那么依我们的经验应该是拆成相等的数乘积最大。加上这个条件之后,就应该是相邻的数乘积最大。那么给定一个N时,我们希望能将它拆为尽可能小的连续的数。而选择的数一定不能小至1,因为乘1不改变目标值,起不到作用纯属浪费。所以最好拆到2.当从2开始的序列累加和到某个数即将超过N时,停下,将剩下的数由高位向低位每个数的值增加1.剩余的数最多会把所有的数都加了1,还剩1个,(如果剩的比这还多,在开始从2往上累加时是不会停下来的,由数列求和公式可知。)若还剩1则再加到最的数上去。

 //////////////////////////////////////////////////////////////////////////
// POJ1032 Parliament
// Memory: 280K Time: 0MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream> using namespace std; int main() {
int N;
cin >> N;
int cnt;
int sum = ;
for (cnt = ; sum + + cnt<= N; ++cnt) {
sum += ( + cnt);
}
int left = N - sum;
int p = + cnt;
while (left > ) {
--p;
--left;
}
if (p == ) {
for (int i =; i < + cnt; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
} else if (p == ) {
for (int i =; i < + cnt; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
} else if (p == cnt + ) {
for (int i =; i < + cnt; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
} else {
for (int i =; i <= p; ++i) {
cout << i << " ";
}
for (int i = p + ; i < cnt + ; ++i) {
cout << i << " ";
}
cout << + cnt << endl;
}
system("pause");
return ;
}

POJ1032 Parliament的更多相关文章

  1. POJ1032 Parliament(数论)

    New convocation of The Fool Land's Parliament consists of N delegates. According to the present regu ...

  2. timus 1136 Parliament(二叉树)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  3. timus 1136 Parliament(e)

    Parliament Time limit: 1.0 secondMemory limit: 64 MB A new parliament is elected in the state of MMM ...

  4. codeforces 644A Parliament of Berland

    A. Parliament of Berland time limit per test 1 second memory limit per test 256 megabytes input stan ...

  5. Graphical Analysis of German Parliament Voting Pattern

    We use network visualizations to look into the voting patterns in the current German parliament. I d ...

  6. Poj 1032 Parliament

    Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19103   Accepted: 8101 Descr ...

  7. 译《The Part-Time Parliament》——终于读懂了Paxos协议!

    最近的考古发现表明,在Paxos小岛上,尽管兼职议会成员都有逍遥癖,但议会模式仍然起作用.他们依旧保持了一致的会议记录,尽管他们频繁的进出会议室并且他们的信使还很健忘.Paxon议会协议提供了一种新方 ...

  8. 北大poj- 1032

    Parliament Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18707   Accepted: 7941 Descr ...

  9. Code Forces 644A Parliament of Berland

    A. Parliament of Berland time limit per test1 second memory limit per test256 megabytes inputstandar ...

随机推荐

  1. 【LeetCode】010. Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  2. Windows 任务管理器中的几个内存概念

    我们使用的大部分 PC 是基于 Intel 微处理器的 x86 和 x64 架构计算机. 因此, 我们面对的 windows 避免不了和 Intel 架构有些设计上的契合. 比如接下来要说到的内存管理 ...

  3. POJ1442:Black Box

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 题目传送门:http://poj.org/problem?id=1442 用对顶堆维护第\(k\)小 ...

  4. ZOJ1610(经典线段树涂色问题)

    Description Painting some colored segments on a line, some previously painted segments may be covere ...

  5. java笔试(2)

  6. windows修改远程桌面RDP连接数

    windows 2003在默认情况下最多只允许两个用户进行远程终端连接,当达到两个远程桌面连接的到时候,再有人尝试连接,就会提示已经达到最大终端数,无法连上了. 一.windows2003终端连接数修 ...

  7. BO 与 VO 的属性拷贝 copyProperties(bo,vo)

    NewClass extends OldClass   : newClass继承自oldClass,属性大多数相同,但是也有新的扩展. beanUtils.copyProperties(oldClas ...

  8. 使用struts2进行文件下载以及下载权限控制的例子

    本测试有两个模块,一个是文件上上传,一个是文件下载,文件下载的时候会检查是否足有权限,如果没有,就会转发到登录页面,如果有权限,就会直接启动下载程序,给浏览器一个输出流. 下面直接上我的代码: 登录表 ...

  9. IPSec方案部署(多业务场景)

    技术点详解—IPSec方案部署 通过前面几期的介绍可以发现IPSec所涉及的参数很多,在具体方案部署过程中有许多灵活选择的地方,本期专栏就专门对IPSec在几种典型环境中的方案部署进行介绍. 一.   ...

  10. Web项目的导出和部署

    -----------------siwuxie095                                 Web 项目的导出     工程结构目录如下:                 ...