CodeForces-893B(Beautiful Divisors)

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值