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 ...
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 }

 

目录
相关文章
|
11月前
|
前端开发 机器人 API
前端大模型入门(一):用 js+langchain 构建基于 LLM 的应用
本文介绍了大语言模型(LLM)的HTTP API流式调用机制及其在前端的实现方法。通过流式调用,服务器可以逐步发送生成的文本内容,前端则实时处理并展示这些数据块,从而提升用户体验和实时性。文章详细讲解了如何使用`fetch`发起流式请求、处理响应流数据、逐步更新界面、处理中断和错误,以及优化用户交互。流式调用特别适用于聊天机器人、搜索建议等应用场景,能够显著减少用户的等待时间,增强交互性。
2882 2
|
SQL Java Spring
spring 防止SQL注入的拦截器
spring 防止SQL注入的拦截器
743 0
Vue3头像(Avatar)
这是一个基于 Vue3 的头像组件库,提供了圆形和方形两种头像形状,并支持自定义大小、图片、图标及字符展示。
363 0
Vue3头像(Avatar)
|
安全 Java 开发者
Java一分钟之-Spring Cloud Netflix Eureka:服务注册与发现
【6月更文挑战第8天】Spring Cloud Eureka是微服务架构的关键,提供服务注册与发现功能。本文讲解Eureka工作原理、配置、常见问题及解决方案。Eureka包含Server(管理服务状态)和Client(注册服务实例并发现服务)。快速入门包括启动Eureka Server和创建Eureka Client。常见问题涉及服务注册不上、服务下线和客户端注册信息不准确,可通过检查网络、理解自我保护机制和配置元数据解决。此外,文中还提及健康检查、安全配置和集群部署等高级实践,以增强系统健壮性和扩展性。
414 8
|
Ubuntu Linux API
一文详解Docker镜像
一文详解Docker镜像
|
安全 Shell Linux
【CentOS7操作系统安全加固系列】第(1)篇
【CentOS7操作系统安全加固系列】第(1)篇
740 0
【CentOS7操作系统安全加固系列】第(1)篇
|
Java fastjson 数据安全/隐私保护
Springboot 自定义注解+AOP简单实例介绍
Springboot 自定义注解+AOP简单实例介绍
385 0
Springboot 自定义注解+AOP简单实例介绍
|
算法 Python
疫情期间佩戴口罩检测之训练检测口罩模型算法实现口罩检测步骤以及报错解决
疫情期间佩戴口罩检测之训练检测口罩模型算法实现口罩检测步骤以及报错解决
686 1
疫情期间佩戴口罩检测之训练检测口罩模型算法实现口罩检测步骤以及报错解决
|
消息中间件 存储 负载均衡
SIGMOD 2021《Kafka 流处理对一致性和完整性的设计》解读
Kafka 以消息存储系统在业界闻名,近几年来 Confluent 公司对 on Kafka 流式计算场景又先后推出了 Kafka Streams(流计算)、ksqlDB(基于 Kafka Streams 的类分析型 DB 系统)。笔者对发表在 SIGMOD 2021 上的论文《Consistency and Completeness: Rethinking Distributed Stream Processing in Apache Kafka》做一些总结,梳理 Kafka Streams 在流处理场景上的设计思路。
693 0