B. Beautiful Divisors
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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 ≤ 10^5) — 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
题意:有一种数字,它的二进制数是由K+1个1和K个0组成,问给出的n能否整除这种数字,并选出最大的。
思路:把10^5以内的所有这种数字列出出来,然后一个一个的判断,并找出最大值
#include<bits/stdc++.h>
using namespace std ;
int num[10] ;
int main()
{
for(int i = 1 ; i <= 9 ; i ++)
{
num[i] = (pow(2 , i) - 1) * pow(2 , i - 1) ;
}
int n ;
cin >> n ;
int ans ;
for(int i = 1 ; i <= 9 ; i ++)
{
if(n % num[i] == 0)
{
ans = num[i];
}
}
cout << ans << endl ;
return 0 ;
}
我这里用的是pow()函数,但是,如果,n的上限再大几个次方的话,最好用一下快速幂,以免tle