lightoj 1042 - Secret Origins 【数学】
2016-05-01 12:19
429 查看
题目链接:lightoj 1042 - Secret Origins
1042 - Secret Origins
PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB
This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr’s existence know very little about her. For example, no one has any clue as to which time period she is originally from.
But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - “Dokhina Batash”. She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.
The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.
You are now given a similar task. Find the first number after N which has the same Onoroy value as N.
Input
Input starts with an integer T (≤ 65), denoting the number of test cases.
Each case begins with an integer N (1 ≤ N ≤ 109).
Output
For each case of input you have to print the case number and the desired result.
Sample Input
Output for Sample Input
5
23
14232
391
7
8
Case 1: 27
Case 2: 14241
Case 3: 395
Case 4: 11
Case 5: 16
题意:找到离n最近的数m满足n和m的二进制中1的个数相同。
思路:[x, x + lowbit(x)]区间里面 1的个数是非升序的。
AC代码:
1042 - Secret Origins
PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB
This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr’s existence know very little about her. For example, no one has any clue as to which time period she is originally from.
But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - “Dokhina Batash”. She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.
The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.
You are now given a similar task. Find the first number after N which has the same Onoroy value as N.
Input
Input starts with an integer T (≤ 65), denoting the number of test cases.
Each case begins with an integer N (1 ≤ N ≤ 109).
Output
For each case of input you have to print the case number and the desired result.
Sample Input
Output for Sample Input
5
23
14232
391
7
8
Case 1: 27
Case 2: 14241
Case 3: 395
Case 4: 11
Case 5: 16
题意:找到离n最近的数m满足n和m的二进制中1的个数相同。
思路:[x, x + lowbit(x)]区间里面 1的个数是非升序的。
AC代码:
#include <iostream> #include <cstdio> #include<math.h> #include <algorithm> using namespace std; typedef long long LL; const int MAXN = 1e6 +10; const int INF = 0x3f3f3f3f; int bit[40]; int lowbit(int x) { return x & (-x); } void getbit(int n) { for(int i = 30; i >= 0; i--) { if(n & (1<<i)) bit[i] = 1; else bit[i] = 0; } } int Count(int n) { int cnt = 0; while(n) { cnt += (n & 1); n >>= 1; } return cnt; } int main() { int t, kcase = 1; scanf("%d", &t); while(t--) { int n; scanf("%d", &n); int num = Count(n); n += lowbit(n); getbit(n); int now = Count(n); while(now != num) { for(int i = 0; i <= 30; i++) { if(bit[i] == 0) { now++; bit[i] = 1; n |= (1<<i); break; } } //cout << now << ' ' << n << endl; } printf("Case %d: %d\n", kcase++, n); } return 0; }
相关文章推荐
- hibernate 创建数据中一张表的方法
- Field 'id' doesn't have a default value
- WLAN and WiFI
- hdoj 1078 FatMouse and Cheese 【dp】
- hdoj 5677 ztr loves substring 【Manacher + 多重背包】
- LaTeX关于包缺失的问题
- 一个简单的时间类的定制
- hdoj 5676 ztr loves lucky numbers 【二分】
- hdoj 5675 ztr loves math 【数学】
- hdoj 1025 Constructing Roads In JGShining's Kingdom 【LIS】
- 基础篇_包(package)
- HTML的本地数据库报错
- C#开发的进化史
- std::ref 有什么用
- PowerShell调用WMI
- redis安装
- 20145120 《Java程序设计》第9周学习总结
- Android Studio 2.+使用分享——关于未签名Apk第一次运行黑屏or白屏
- Python lambda表达式
- 2016年4月14日--4月30日(12小时,剩3009小时)