题目链接:http://acm.tju.edu.cn/toj/showp2226.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 244 Accepted Runs: 115 Multiple test files
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 contains a single integer N (5 ≤ N ≤ 1000).
Output
Write to the output 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 #1
7
Output for the sample input #1
3 4
Sample input #2
31
Output for the sample input #2
2 3 5 6 7 8
Source: Northeastern European 1998
给你一个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) );
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
int n,ans[1000];
while(~scanf("%d",&n)){
memset(ans,0,sizeof(ans));
int sum=5;
ans[0]=2;
ans[1]=3;
int i=2;
for(;sum+ans[i-1]+1<=n;i++){
ans[i]=ans[i-1]+1;
sum+=ans[i];
}
int tmp=n-sum;
while(tmp>0){
for(int j=i-1;tmp>0&&j>=0;j--){
ans[j]++;
tmp--;
}
}
printf("%d",ans[0]);
for(int j=1;j<i;j++)
printf(" %d",ans[j]);
printf("\n");
}
}