题目来源: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. Cannot find PHPUnit in include path (.;C:\php5\pear)

    --pear channel-discover pear.phpunit.de --pear install phpunit/PHPUnit 此时会显示: No valid packages foun ...

  2. bzoj 3221: Obserbing the tree树上询问 树链剖分+线段树

    题目大意: http://www.lydsy.com/JudgeOnline/problem.php?id=3221 题解 啊呀...这是昨天的考试题啊...直接就粘了.. 与4515: [Sdoi2 ...

  3. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  4. Bootstrap CSS教程

    Bootstrap 教程 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...

  5. centos下升级mysql5.5.47到5.7.14操作过程

    一. 查看已安装Mysql基础信息通过mysql –V 查看下mysql版本可以通过命令find / -name mysql 得到下面信息 1.安装目录[root@jjxnhd-192-10 mysq ...

  6. JVM插码之三:javaagent介绍及javassist介绍

    本文介绍一下,当下比较基础但是使用场景却很多的一种技术,稍微偏底层点,就是字节码插庄技术了...,如果之前大家熟悉了asm,cglib以及javassit等技术,那么下面说的就很简单了...,因为下面 ...

  7. influxdb api

    https://influxdb-python.readthedocs.io/en/latest/examples.html

  8. 面向对象(Java中普通代码块,构造代码块,静态代码块区别及代码示例)

    //执行顺序:(优先级从高到低.)静态代码块>mian方法>构造代码块>构造方法. 其中静态代码块只执行一次.构造代码块在每次创建对象是都会执行. 1 普通代码块 //普通代码块:在 ...

  9. Centos开启telnet/ssh/ftp/sftp服务

    Telnet 开启telnet服务步骤: 1.   查看CentOS/Telnet_server版本:#cat /etc/issue,   #rpm -qa | grep telnet 2.   安装 ...

  10. asp弹出层

    asp弹出层 <style type="text/css"> html, body { height: %; width: %; } .white_content { ...