Codeforces 768B Code For 1
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:
Elements on positions from 2-nd to 5-th in list is [1, 1, 1, 1]. The number of ones is 4.
For the second example:
Elements on positions from 3-rd to 10-th in list is [1, 1, 1, 0, 1, 0, 1, 0]. The number of ones is 5.
题目链接:http://codeforces.com/contest/768/problem/B
分析:二分的模版题!来围观看看!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, l, r, s = , ans;
void solve(ll a, ll b, ll l, ll r, ll d)//二分的思想
{
if ( a > b || l > r ) return;
else
{
ll mid = (a+b)/;
if ( r < mid )solve(a,mid-,l,r,d/);
else if ( mid < l )solve(mid+,b,l,r,d/);
else {
ans += d%;
solve(a,mid-,l,mid-,d/);
solve(mid+,b,mid+,r,d/);
}
}
}
int main()
{
cin >> n >> l >> r;
long long p = n;
while ( p >= )
{
p /= ;
s = s*+;
}
solve(,s,l,r,n);
cout << ans << endl;
return ;
}
Codeforces 768B Code For 1的更多相关文章
- Codeforces 768B - Code For 1(分治思想)
768B - Code For 1 思路:类似于线段树的区间查询. 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- Codeforces Avito Code Challenge 2018 D. Bookshelves
Codeforces Avito Code Challenge 2018 D. Bookshelves 题目连接: http://codeforces.com/contest/981/problem/ ...
- Codeforces 768B B. Code For 1
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6423483.html B. Code For 1 time limit per test:2 se ...
- 【codeforces 768B】Code For 1
[题目链接]:http://codeforces.com/contest/768/problem/B [题意] 一开始给你一个数字n; 让你用这个数字n根据一定的规则生成序列; (如果新生成的序列里面 ...
- Codeforces Zepto Code Rush 2014 -C - Dungeons and Candies
这题给的一个教训:Codeforces没有超时这个概念.本来以为1000*(1000+1)/2*10*10要超时的.结果我想多了. 这题由于k层都可能有关系,所以建一个图,每两个点之间连边,边权为n* ...
- Codeforces - Avito Code Challenge 2018
Portal A. Antipalindrome 暴力. B. Businessmen Problems 暴力. C. Useful Decomposition 居然不是C打头的?! 将一棵树划分成若 ...
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) A. Checking the Calendar(水题)
传送门 Description You are given names of two days of the week. Please, determine whether it is possibl ...
- Codeforces Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B. Batch Sort(暴力)
传送门 Description You are given a table consisting of n rows and m columns. Numbers in each row form a ...
- CodeForces ZeptoLab Code Rush 2015
拖了好久的题解,想想还是补一下吧. A. King of Thieves 直接枚举起点和5个点之间的间距,进行判断即可. #include <bits/stdc++.h> using na ...
随机推荐
- MIPI-1
未来的产品都将朝着移动的方向发展,例如智能手机.数码相机.摄像机.平板电脑.媒体播放器.游戏机等,这些产品需要能执行多任务,包括处理多个不同的传感器如麦克风.图像传感器.磁罗盘.三轴加速度计和精细的触 ...
- iOS通过代码关闭程序
//-------------------------------- 退出程序 -----------------------------------------// - (void)exitAppl ...
- cf Round 594
A.Warrior and Archer(思维) 战士一定会ban掉当前边缘的位置.而战士和射手就会选择剩下的最远的两点.我们让剩下的最远的两点最近就达到了均衡.于是我们枚举战士ban掉的边缘,ban ...
- UVa 900 - Brick Wall Patterns
题目大意:用1*2的砖头建n*2的墙,问有多少种不同的砖头排列方式?与斐波那契序列相似. #include <cstdio> #define MAXN 60 #define N 50 un ...
- 大数据竞赛平台——Kaggle 入门
Reference: http://blog.csdn.net/witnessai1/article/details/52612012 Kaggle是一个数据分析的竞赛平台,网址:https://ww ...
- WPF中ContextMenu通过CommandParameter传参
场景:ListBox中有个ContextMenu,希望点击其中一个菜单项的时候把ListBox当做CommandParameter传递给Command,但是发现无论是通过ElementName还是Re ...
- UVa 10382 - Watering Grass
题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
- zepto js 源码 解读
/* Zepto v1.0-1-ga3cab6c - polyfill zepto detect event ajax form fx - zeptojs.com/license */ ;(funct ...
- HTML5学习笔记一:与html4的区别(整合)
一 语法的改变 1.1 HTML5中标记方法 1.内容类型(ContentType):扩展符仍为“.html”或".htm",内容类型仍是“text/html”. 2.DOCTYP ...
- 05 Linux字符驱动---静态注册
1. mycdev.c #include <linux/init.h> #include <linux/module.h> #include <linux/cdev.h& ...