CodeForces 893B Beautiful Divisors
来源:互联网 发布:土库曼斯坦 知乎 编辑:程序博客网 时间:2024/06/06 02:32
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 ofk + 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!
The only line of input contains one number n (1 ≤ n ≤ 105) — the number Luba has got.
Output one number — the greatest beautiful divisor of Luba's number. It is obvious that the answer always exists.
3
1
992
496
题目大意:
给定一个n,求小于等于n的最大的漂亮数字。漂亮数字的定义是二进制下前k个数字都为1, 后k + 1个数字都为0的十进制数字。
解题思路:
因为要求最大的, 我们只要从小于等于n的漂亮数字中从后往前枚举看是否能被整除即可。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*
*ios::sync_with_stdio(false);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = (int)1e9 + 7;
const int Max = (int) 1e5 + 7;
int ans, cur;
int main() {
int n;
ans = 1, cur = 1;
scanf("%d", &n);
while (ans <= n) {
cur++;
int x = 0;
for (int i = cur - 1; i <= cur + cur - 2; ++i) {
x += (1 << i);
}
ans = x;
}
for (int i = cur; i >= 1; --i) {
int temp = 0;
for (int j = i - 1; j <= i + i - 2; ++j) {
temp += (1 << j);
}
if (n >= temp && n % temp == 0) {
printf("%d\n", temp);
return 0;
}
}
}
- CodeForces 893B Beautiful Divisors
- CodeForces 893B Beautiful Divisors
- Codeforces Educational Codeforces Round 33 B 893B Beautiful Divisors
- CodeForces 893B Beautiful Divisors (打表)
- 【Educational Codeforces Round 33 B】Beautiful Divisors
- 171127 #codeforces# Beautiful Divisors
- CF-B.Beautiful Divisors
- Educational Codeforces Round 33 (Rated for Div. 2) 893B. Beautiful Divisors
- 【Educational Codeforces Round 33】 B. Beautiful Divisors (枚举)
- codeforces--651B Beautiful Paintings
- codeforces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- codeforces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- 【CodeForces】651B - Beautiful Paintings
- 【CodeForces】[651B]Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- Win10_64位下搭建TensorFlow-GPU版本的环境
- java中的多线程
- bzoj2209 [Jsoi2011]括号序列(splay)
- 图文详解MyEclipse中新建Maven webapp项目的步骤(很详细)
- 理解zookeeper选举机制
- CodeForces 893B Beautiful Divisors
- <4>C++ Primer 变量
- ubuntu下ping www.baidu.com 出错误
- 深度优先遍历 广度优先遍历JAVA
- Zookeeper安装过程中的坑
- Laravel-admin之修改操作日志
- 服务器环境搭建
- 项目中最近遇到的一些bug
- KMP算法的实现及优化