Codeforces 768B Code For 1

B. Code For 1

time limit per test:2 seconds

memory limit per test:256 megabytes

input:standard input

output: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?

Input

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

Output the total number of 1s in the range l to r in the final sequence.

Examples

Input

7 2 5

Output

4

Input

10 3 10

Output

5

Note

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

分析:二分的模版题!来围观看看!

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll n, l, r, s = 1, ans;
 5 void solve(ll a, ll b, ll l, ll r, ll d)//二分的思想
 6 {
 7     if ( a > b || l > r )    return;
 8     else
 9         {
10         ll mid = (a+b)/2;
11         if ( r < mid )solve(a,mid-1,l,r,d/2);
12         else if ( mid < l )solve(mid+1,b,l,r,d/2);
13         else {
14             ans += d%2;
15         solve(a,mid-1,l,mid-1,d/2);
16         solve(mid+1,b,mid+1,r,d/2);
17         }
18     }
19 }
20 int main()
21 {
22     cin >> n >> l >> r;
23     long long p = n;
24     while ( p >= 2 )
25     {
26         p /= 2;
27         s = s*2+1;
28     }
29     solve(1,s,l,r,n);
30     cout << ans << endl;
31     return 0;
32 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Java架构师历程

jsoup解析的常见用法

1、解析attribute中值,如下面所示的serviceID和serviceName:

2613
来自专栏JadePeng的技术博客

Java LinqCollection 仿Linq的list常用函数

目前支持find,findAll,sort,select,remove等,java不支持lamda函数,因此用接口代替 public interface Fun...

1K4
来自专栏码匠的流水账

聊聊storm的IEventLogger

storm-2.0.0/storm-client/src/jvm/org/apache/storm/metric/IEventLogger.java

882
来自专栏Java学习123

转 java里poi操作excel的工具类(兼容各版本)

4046
来自专栏码匠的流水账

聊聊flink的RichParallelSourceFunction

flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

2681
来自专栏智能大石头

C++返回值优化RVO

返回值优化,是一种属于编译器的技术,它通过转换源代码和对象的创建来加快源代码的执行速度。RVO = return value optimization。 测试...

3139
来自专栏Hongten

Get the image file(s) some informations,Including the Make,Model,Date/Time,etc

This is a blog about how to get the image file(s) some informations.Including th...

992
来自专栏后端沉思录

自定义钉钉机器人报警

按照钉钉的文档来开发,创建机器人后,即可获取Webhook地址,整个过程还是很简单的,以上只是提供了一个思路.

3592
来自专栏曾大稳的博客

Android ClassLoader流程解读并简单方式实现热更新

ClassLoader在启动Activity的时候会调用loadClass方法,我们就从这里入手:

2962
来自专栏Android知识点总结

看得见的数据结构Android版之双链表篇

691

扫码关注云+社区

领取腾讯云代金券