1079. Maximum
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Consider the sequence of numbers
ai,
i = 0, 1, 2, …, which satisfies the following requirements:
- a0 = 0
- a1 = 1
- a2i = ai
- a2i+1 = ai + ai+1
Write a program which for a given value of
n finds the largest number among the numbers
a
0,
a
1, …,
an.
Input
You are given several test cases (not more than 10). Each test case is a line containing an integer
n(1 ≤
n ≤ 99 999). The last line of input contains 0.
Output
For every
n in the input write the corresponding maximum value found.
Sample
input | output |
---|---|
5 10 0 | 3 4 |
Problem Author: Emil Kelevedzhiev
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
Problem Source: Winter Mathematical Festival Varna '2001 Informatics Tournament
Difficulty: 112
Printable version
Submit solution
Discussion (82)
My submissions All submissions (32638) All accepted submissions (12992) Solutions rating (10216)
直接打一个表,在计算第i个数的大小的时候更新出来0~i的最大值即可。
My submissions All submissions (32638) All accepted submissions (12992) Solutions rating (10216)
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 100500
int dp[MAXN];
int a[MAXN];
void init()
{
a[0]=0;
a[1]=1;
dp[0]=0;
dp[1]=1;
int maxx=0;
for(int i=2;i<=100050;i++)
{
if(i&1)
{
a[i]=a[i/2]+a[i/2+1];
}
else
{
a[i]=a[i/2];
}
if(a[i]>maxx)
{
dp[i]=a[i];
maxx=a[i];
}
else
{
dp[i]=maxx;
}
}
}
int main()
{
init();
int t,n;
while(cin>>n&&n)
{
cout<<dp[n]<<endl;
}
return 0;
}