Educational Codeforces Round 33 (Rated for Div. 2) B题. Beautiful Divisors

B. Beautiful Divisors
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

 题意:100000 以内 有几个 美丽的数字 , 分别是 :

  1(2)->1(10)    110(2)->6(10)   11100(2)->28(10)   1111000(2) -> 120(10)   111110000(2)->496(10)   11111100000(2) -> 2016(10)

       1111111000000(2) -> 8128(10)     111111110000000(2) -> 32640(10)    11111111100000000(2) -> 130816(10)

        注意看题 , 题目要的是  :“满足 是 n 的 除数 同时 是美丽数字 两个条件的最大数字” ,

 

/*  预处理  */ 
#include<stdio.h>
#include<string.h>
#include <iostream>
#include<algorithm>

using namespace std;
#define maxn 200
int num[maxn] ; 

void init(){
    num[1] = 1 ; 
    num[2] = 6 ; 
    num[3] = 28 ; 
    num[4] = 120 ; 
    num[5] = 496 ;
    num[6] = 2016 ; 
    num[7] = 8128 ; 
    num[8] = 32640 ; 
    num[9] = 130816 ; 
    
}

int main(){
    int n ; 
    init() ; 
    
    while(~scanf("%d" , &n)){
        for(int i=9 ; i>0 ; i--){
            if(num[i]<=n&&(n%num[i] == 0 )){
                printf("%d\n" , num[i]) ; 
                break ; 
            }
        }
    }
    
    return 0 ; 
}
View Code
/* 快速幂判断美丽数字  */ 
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std ;

#define LL long long
// 快速幂  
LL pow_x(int k ) {
    LL result = 1 ;
    LL a = 2 ;

    while(k) {
        if(k&1) {
            result = result * a ;
        }
        a = a*a ;
        k=k/2 ;
    }
    return result ;
}

int main() {

    int n ;
    while(~scanf("%d" , &n)) {
        bool flag = false ;
        int result ;
        for(int i=n ; i>0 ; i--) {
            for(int k = 1 ; ; k++ ) {
                if((pow_x(k)-1)*pow_x(k-1) == i ) {
                    if(n%i==0) {
                        result = i ;
                        flag = true ;
                        break ;
                    }

                }
                
                if((pow_x(k)-1)*pow_x(k-1) > i ) {
                    break ;
                }
            }
            if(flag) {
                break;
            }
        }
        printf("%d\n" , result) ;
    }
    return 0 ;

}
View Code

 

转载于:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7889935.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值