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 |
---|---|
5100 | 34 |
水题,按照公式打表即可
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
__int64 a[1000000];
__int64 hash[1000000];
int main()
{
int i,j,n;
__int64 maxn;
a[0] = 0;
hash[0] = 0;
a[1] = 1;
hash[1] = 1;
maxn = 1;
for(i = 2; i<=999999; i++)
{
if(i%2)
{
j = (i-1)/2;
a[i] = a[j]+a[j+1];
}
else
a[i] = a[i/2];
if(a[i]>maxn)
maxn = a[i];
hash[i] = maxn;
}
while(~scanf("%d",&n) && n)
{
printf("%I64d\n",hash[n]);
}
return 0;
}