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
Tags:
)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAXN 100000
int a[MAXN];
int sum[MAXN];
int main()
{
int i;
int ans=3;
a[0]=0;
a[1]=1;
a[2]=1;
a[3]=2;
a[4]=1;
a[5]=3;
sum[0]=0;
sum[1]=1;
sum[2]=1;
sum[3]=2;
sum[4]=2;
sum[5]=3;
for(i=6; i<100000; i++)
{
if(i&1)
a[i]=a[i/2]+a[i/2+1];
else
a[i]=a[i/2];
ans=max(ans,a[i]);
sum[i]=ans;
}
int n;
while(cin>>n,n)
cout<<sum[n]<<endl;
return 0;
}