Problem Description
Recently Luba learned about a special kind of numbers that she calls beautiful numbers. The number is called beautiful iff its binary representation consists of k + 1 consecutive ones, and then k consecutive zeroes.Some examples of beautiful numbers: 12 (110); 1102 (610); 11110002 (12010); 1111100002 (49610). More formally, the number is beautiful iff there exists some positive integer k such that the number is equal to (2k - 1) * (2k - 1).Luba has got an integer number n, and she wants to find its greatest beautiful divisor. Help her to find it!
Input
The only line of input contains one number n (1 ≤ n ≤ 105) — the number Luba has got.
Output
Output one number — the greatest beautiful divisor of Luba’s number. It is obvious that the answer always exists.
Examples
Input
3
Output
1
Input
992
Output
496
题目大意
beautiful numbers 是由连续的1和连续的0组成。1在前面且1总比0多一个
有多个测试样例,输出小于他且是其因子的最大 beautiful numbers
AC代码(打表)
由于数字增长比较快,所以数据不会太多,可以先打表
#include<algorithm>
#include<iostream>
using namespace std;
int form[55];
int main()
{
for(int i=0; i<15; ++i)
{
int num=1,mm=0;
for(int j=0; j<=2*i; ++j)
{
if(j>=i)
mm+=num;
num*=2;
}
form[i]=mm;
}
int n;
//cout<<form[14]<<endl;
while(cin>>n)
{
for(int i=14;i>=0;--i)
{
if(form[i]<=n&&n%form[i]==0)
{
cout<<form[i]<<endl;
break;
}
}
}
}
AC代码(用等比数列公式简化计算)
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
int num=1,ans=1;
for(int i=1;;++i)
{
num=pow(2.0,i)*(pow(2.0,i+1)-1);
if(num<=n&&n%num==0)
ans=num;
else if(num>n)
break;
}
cout<<ans<<endl;
}
}