B. Code For 1 分治
2 seconds
256 megabytes
standard input
standard output
Jon fought bravely to rescue the wildlings who were attacked by the white-walkers at Hardhome. On his arrival, Sam tells him that he wants to go to Oldtown to train at the Citadel to become a maester, so he can return and take the deceased Aemon's place as maester of Castle Black. Jon agrees to Sam's proposal and Sam sets off his journey to the Citadel. However becoming a trainee at the Citadel is not a cakewalk and hence the maesters at the Citadel gave Sam a problem to test his eligibility.
Initially Sam has a list with a single element n. Then he has to perform certain operations on this list. In each operation Sam must remove any element x, such that x > 1, from the list and insert at the same position , , sequentially. He must continue with these operations until all the elements in the list are either 0 or 1.
Now the masters want the total number of 1s in the range l to r (1-indexed). Sam wants to become a maester but unfortunately he cannot solve this problem. Can you help Sam to pass the eligibility test?
The first line contains three integers n, l, r (0 ≤ n < 250, 0 ≤ r - l ≤ 105, r ≥ 1, l ≥ 1) – initial element and the range l to r.
It is guaranteed that r is not greater than the length of the final list.
Output the total number of 1s in the range l to r in the final sequence.
7 2 5
4
10 3 10
5
Consider first example:
这个题目做的挺漂亮嘛!哈哈!
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<iostream>
#define MAXN 200009
#define eps 1e-11 + 1e-12/2
typedef long long LL; using namespace std;
/*
显然!分治!
*/
LL n, l ,r;
LL solve(LL x, LL beg,LL end)
{
LL mid = (beg + end) / ;
if (beg == mid) return ;
if (r < mid)
return solve(x / , beg, mid - );
else if (l > mid)
return solve(x / , mid + , end);
else
{
LL ret = x % ;
if (l < mid) ret += solve(x / , beg, mid - );
if (r > mid) ret += solve(x / , mid + , end);
return ret;
}
return ;
}
int main()
{ cin >> n >> l >> r;
if (n == )
{
cout << << endl;
return ;
}
LL len = pow(, floor(log2(n)) + ) - ;
cout << solve(n, , len) << endl;
}
最新文章
- .Net 转战 Android 4.4 日常笔记(5)--新软件Android Studio 0.5.8安装与配置及问题解决
- AppBox升级进行时 - Any与All的用法(Entity Framework)
- 关于oracle中日期使用
- 如何在启用SharePoint浏览器功能的InfoPath 表单中添加托管代码以动态地加载并显示图片
- zoj Treasure Hunt IV
- iphone获取当前磁盘信息
- Android MemInfo
- 自制node.js + npm绿色版
- NAND闪存供过于求的情况今年会有所好转吗?
- Gson学习文档
- 【GIS】Mapbox-json配置
- Asp.Net WebApi核心对象解析(一)
- 信息安全-加密:RAS 加密
- POSTGRESQL 锁表的问题
- kafka demo
- 使用Topshelf 5步创建Windows 服务
- testng参数化(提供测试数据)
- Python数学运算入门把Python当作计算器
- 最新eclipse安装SVN插件
- RGB和HSL色彩的相互转换
热门文章
- $CF241D\ Numbers$
- T - Posterized(贪心思维)
- linux centos7 安装nginx并启动
- js数组的各种方法
- (1)HTML声明与基础(已入垃圾筐)
- Java反射机制实战——字段篇
- C++(存储类)经典!!
- mongo 3.4分片集群系列之七:配置数据库管理
- 【PostgreSQL-9.6.3】LOG: unrecognized configuration parameter ";dynamic_shared_memory_type";
- windows系统下的redis启动教程