POJ1032 Parliament(数论)

New convocation of The Fool Land's Parliament consists of N delegates. According to the present regulation delegates should be divided into disjoint groups of different sizes and every day each group has to send one delegate to the conciliatory committee. The composition of the conciliatory committee should be different each day. The Parliament works only while this can be accomplished. 
You are to write a program that will determine how many delegates should contain each group in order for Parliament to work as long as possible. 

Input

The input file contains a single integer N (5<=N<=1000 ).

Output

Write to the output file the sizes of groups that allow the Parliament to work for the maximal possible time. These sizes should be printed on a single line in ascending order and should be separated by spaces.

Sample Input

7

Sample Output

3 4


这道题就是将n分成若干个不同的正整数的和,使其相乘最大,求分成哪几个数。

题解:就是将n分成2,3,......,直到不能分为止,然后怎么办呢,剩下的就倒着分配回去,这样乘积最大。


转一下:http://www.cnblogs.com/Missa/archive/2012/10/11/2719943.html

给你一个n问求使得 a1+a2+..ak==n时 a1*a2*..ak最大。。a1 a2.....不相等。(没看懂题目意思。。)

以下转自http://blog.himdd.com/?p=1918

思路:将一个数分成2份,如何分,使得这两个数乘积最大。答案是将这个数平分,证明是求x*(n-x)的最大值。基于这种思路,将N分成乘积最大的不相等的多份,应使得其中每份的数相差尽量少,即差值为1的等差数列为最理想状态。构造了一个等差数列以后,再根据剩余值对整个数列的值进行调整。使得相邻元素差值达到最小。这里注意,等差数列的构造应以2为首项,1为首项的话,对乘积没影响。。。
(以下证明是从网上得来的)
由题意知,这种分解的数目是有限的,因此,最大积存在;
假设最大积的分解为:
N=a1+a2+a3+…+a[t-2]+a[t-1]+a[t] (t是分解的数目,a1<a2<a3<...<a[t-2]<a[t-1]<a[t])
 下面是该数列的性质及其证明:
1)a1>1;
如果a1=1,则a1和a[t]可以由a[t]+a1=a[t]+1来替代,从而得到更大的积;

2)对于所有的i,有a[i+1]-a[i]<= 2;
如果存在i使得a[i+1]-a[i]>=3,则a[i]和a[i+1]可以替换为a[i]+1,a[i+1]-1,从而使乘积更大;

3)最多只存在一个i使得a[i+1]-a[i]=2;
如果i< j且a[i+1]-a[i]=2、a[j+1]-a[j]=2,则a[i],a[j+1]可以替换为a[i]+1,a[j+1]-1,从而使得乘积更大;

4)a1<=3;
如果a1>=4,则a1和a2可以替换为2,a1-1,a2-1,从而使得乘积更大;

5)如果a1=3且存在i满足a[i+1]-a[i]=2,则i一定等于t-1;
如果i<t-1,则a[i+2]可以替换为2,a[i+2]-2,从而使得乘积更大;< p="">

将上面5条性质综合一下,得到该数列满足:
1)1< a1< 4
2)a[i+1]-a[i] <=2(该序列按升序排序)
3)a[i+1]-a[i]=2的情况最多只有一个

因此,我们得到最大的乘积的做法就是求出从2开始的最大连续(由上面总结的性质2和3可知)自然数列之和A,使得A的值不超过N,具体分析如下:
对输入的N,找到k满足:
A=2+3+4+...+(k-1)+k <= N < A+(k+1) = B
假设N=A+p(0<=p< k+1),即A+p是最大积的数列
1)p=0,则最大积是A;
2)1<=p<=k-1,则最大积是B-{k+1-p},即从数列的最大项i开始,从大到小依次每项加1,知道p=0为止;
3)p=k,则最大积是A+p=A+k=A-{2}+{k+2};( =3+4+...+k+( k+2) );

 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int n;
 9 int a[100007],top=0;
10 
11 int main()
12 {
13     scanf("%d",&n);
14     int st=2;
15     while (n>=st)
16     {
17         a[++top]=st;
18         n-=st;
19         st++;
20     }
21     for (int i=top;i>=top-n+1;i--)
22         a[i]++;
23     if (n>top) a[top]++;        
24     for (int i=1;i<=top-1;i++)    
25         printf("%d ",a[i]);
26     printf("%d
",a[top]);    
27 }
原文地址:https://www.cnblogs.com/fengzhiyuan/p/7598844.html