Codeforces Round #247 (D. Random Task)
2014-06-06 00:00
176 查看
摘要: Codeforces Round #247 (D. Random Task)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer n, that among numbers n + 1, n + 2, ..., 2·n there are exactly m numbers which binary representation contains exactly kdigits one".
The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.
Input
The first line contains two space-separated integers, m and k (0 ≤ m ≤ 1018; 1 ≤ k ≤ 64).
Output
Print the required number n (1 ≤ n ≤ 1018). If there are multiple answers, print any of them.
Sample test(s)
input
output
input
output
2.设范围为n+1~2n的数据中有k个1的数为m,利用归纳法可证得当n = n + 1时,有k个1的数据个数将会>=m, 呈现单调递增性。因此可选用二分查找来加快速度。
3.n+1~2n的数据具有连续性可以利用数位dp进一步缩短求解时间
题目
D. Random Tasktime limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: "Find such positive integer n, that among numbers n + 1, n + 2, ..., 2·n there are exactly m numbers which binary representation contains exactly kdigits one".
The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.
Input
The first line contains two space-separated integers, m and k (0 ≤ m ≤ 1018; 1 ≤ k ≤ 64).
Output
Print the required number n (1 ≤ n ≤ 1018). If there are multiple answers, print any of them.
Sample test(s)
input
1 1
output
1
input
3 2
output
5
解析
1.该题目数据量很大,因此需要注意数据的存储类型防止越界,且完全的暴力求解将会超时.2.设范围为n+1~2n的数据中有k个1的数为m,利用归纳法可证得当n = n + 1时,有k个1的数据个数将会>=m, 呈现单调递增性。因此可选用二分查找来加快速度。
3.n+1~2n的数据具有连续性可以利用数位dp进一步缩短求解时间
解答
#include <iostream> #include <cstring> using namespace std; #define CLR(a, val) memset(a, val, sizeof(a)) typedef long long int ll; #define INF 1000000000000000000LL #define MAXN 60LL static void dp_init(void); static ll dp_cnt(ll num); static ll binary_search(ll start, ll end, ll needed); ll m, k; ll dp[70][70]; void dp_init(void){ CLR(dp, 0); for(ll i = 0; i <= MAXN; i++) dp[i][0] = 1; for(ll i = 1; i <= MAXN; i++){ for(ll j = 1; j <= i; j++) dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1]; } } ll dp_cnt(ll num){ ll bit_found = 0; ll bit_cnt = 0; for(ll i = MAXN; i >= 0; i--){ if(num & ( 1LL << i)){ ll j = k - bit_found; if(j < 0){ /*we can make sure that no more nums could found after j < 0*/ break; } bit_cnt += dp[i][j]; bit_found++; } } return bit_cnt; } ll binary_search(ll start, ll end, ll needed){ ll bit_cnt, mid; dp_init(); while(start <= end){ mid = (start + end)/2; bit_cnt = dp_cnt(mid*2) - dp_cnt(mid); if(bit_cnt < needed){ start = mid + 1; }else if(bit_cnt > needed){ end = mid - 1; }else{ return mid; } } return -1; } int main() { ll mid = 1; cin >> m >> k; cout << binary_search(1, INF, m) << endl; return 0; }
相关文章推荐
- Codeforces Round #247 (Div. 2) D Random Task
- CodeForces 431 D.Random Task(组合数学)
- Codeforces 431 D. Random Task 数位dp单调性
- Codeforces Round #247(Div. 2) D. Random Task 二分+前缀和
- 窥探 kernel,just for fun --- task_struct
- 编写高质量代码改善C#程序的157个建议——建议82:Parallel简化但不等同于Task默认行为
- 百度前端学院_小薇课堂_task_4
- android:clearTaskOnLaunch="true"的作用
- A Simple Task
- java线程创建之FutureTask
- 网上购物系统(Task010)——FormView编辑更新商品详细信息
- Java RandomAccessFile用法
- linux进程管理与调度:task_struct结构体
- objective-c 中随机数的用法 arc4random() 、random()、CCRANDOM_0_1()
- 简述RandomAccessFile和FileInputStream及FileOutputStream在使用中的区别
- ListenableFutureTask的异步回调
- AsyncTask, RejectedExecutionException and Task Limit
- hdu 3974 Assign the task (线段树)
- SpringTaskExecutor介绍
- random 随机位移所选择的物体Y轴位移.txt