分治的思想,二分法,采用BigInteger解决大数
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.

Example

Input
7 2 5
Output
4
Input
10 3 10
Output
5


import java.math.BigInteger;
import java.util.Scanner;
import java.util.regex.Matcher;/*** Created by 95112 on 10/21/2017.*/
public class TwoPoint {private static long sum = 1;private static BigInteger ans;static BigInteger two;public static void main(String[] args){Scanner scanner = new Scanner(System.in);ans = new BigInteger("0");BigInteger n = new BigInteger(scanner.next());BigInteger l = new BigInteger(scanner.next());BigInteger r = new BigInteger(scanner.next());BigInteger x = new BigInteger("1");BigInteger y = new BigInteger("1");BigInteger tmp = n.add(new BigInteger("0"));two = new BigInteger("2");while (tmp.compareTo(x) == 1){tmp = tmp.divide(two);y = y.multiply(two).add(x);}solve(x,y,l,r,n);System.out.println(ans.toString());}private static  void solve(BigInteger x,BigInteger y , BigInteger l , BigInteger r , BigInteger n){if (x.compareTo(y) == 1 || l.compareTo(r) == 1)return;BigInteger mid = x.add(y).divide(two);if ( mid.compareTo(l) == -1 )solve(mid.add(BigInteger.ONE),y,l,r,n.divide(two));else if (mid.compareTo(r) == 1)solve(x,mid.subtract(BigInteger.ONE),l,r,n.divide(two));else {ans = ans.add(n.mod(two));solve(mid.add(BigInteger.ONE),y,mid.add(BigInteger.ONE),r,n.divide(two));solve(x,mid.subtract(BigInteger.ONE),l,mid.subtract(BigInteger.ONE),n.divide(two));}}
}

分治问题CodeForces768B大数相关推荐

  1. python递归算法 电影院票价问题_算法课堂实验报告(二)——python递归和分治(第k小的数,大数乘法问题)...

    python实现递归和分治 一.开发环境 开发工具:jupyter notebook 并使用vscode,cmd命令行工具协助编程测试算法,并使用codeblocks辅助编写C++程序 编程语言:py ...

  2. 大数相乘(C语言,分治算法)

    问题: 由于编程语言提供的基本数值数据类型表示的数值范围有限,不能满足较大规模的高精度数值计算,因此需要利用其他方法实现高精度数值的计算,于是产生了大数运算.大数运算主要有加.减.乘三种方法. 下面就 ...

  3. python入门算法_Python 算法入门教程

    分治算法介绍 今天我们聊一聊计算机中非常重要和常用的一种算法:分治算法.它在计算机领域应用广泛,几乎无处不在.不仅计算机领域,在信号处理领域,分而治之也是十分常见的一种信号处理方法.著名快速傅里叶变换 ...

  4. 最全最详细数据结构与算法视频-【附课件和源码】

    源码和课件下载方式在文末 什么是数据结构与算法 算法用来设计并实现一种用计算机来解决问题的方法.它满足下列性质: 输入:有零个或多个输入量 输出:产生至少一个输出量 确定性:算法的指令清晰.无歧义 有 ...

  5. 详解分治法(divide-and-conquer)及其典型应用

    什么是分治法 在昨天的文章<漫谈数据库中的join>的最后,提到Grace hash join和Sort-merge join都是基于分治思想的.分治法(divide-and-conque ...

  6. 斐波那契数列取模(大数)分治算法

    斐波那契数列取模(大数)分治算法 这是算法课程上完分之策略后老师留的一道题目: 菲波那契数列如下:1,1,2,3,5,8,13,21,34......其中a[1] = 1, a[2] = 1, a[n ...

  7. 【恋上数据结构】贪心(最优装载、零钱兑换、0-1背包)、分治(最大连续子序列和、大数乘法)

    贪心.分治 贪心(Greedy) 问题1:最优装载(加勒比海盗) 问题2:零钱兑换 零钱兑换的另一个例子 贪心注意点 问题3:0-1背包 0-1 背包 - 实例 一些习题 分治(Divide And ...

  8. 第十章分治算法(大数相乘)

    分治算法:分治算法由两部分组成,分和治,分是使问题规模变小,递归解决较小的问题,治是从子问题的解中构建原问题的解. 传统上,在正文中至少含有两个递归调用的例程叫做分治算法,而正文中只含有一个递归调用的 ...

  9. POJ1001 求高精度幂 (分治高精度大数相乘)

    总体思路: 高精度大数乘法 需要记录小数点位置 需要使用大数相乘 只是考验能否使用大数相乘 大整数乘法可以模拟乘法运算写 也可以使用分治写法 分治可以优化XY=AC2^N [(A-B)(D-C)+AC ...

最新文章

  1. 【数字信号处理】序列傅里叶变换 ( 傅里叶变换物理意义 | 反应信号在整个数字角频率上的能量分布 )
  2. 何时开学?教育部最新回应:满足三个条件可开学
  3. hdu 2047(递推)
  4. 计算机应用技术 平面设计,全国信息化计算机应用技术水平教育考试试卷 平面设计师...
  5. Oracle学习笔记:blank_trimming的含义
  6. Qt工作笔记-让界面飞一会(让界面旋转出来)
  7. 笔记本电脑内网、外网一起使用
  8. [转]Delphi 12种大小写转换的方法
  9. 学习Windows Mobile开发系列笔记(win32基本程序框架)
  10. 深度学习入门:用MNIST完成Autoencoder(续)
  11. 循环体(for/while)循环变量的设置
  12. matlab做拉普拉斯反演,拉普拉斯变换反演
  13. 手机技巧之扩展内存的检测以及修复。
  14. 基于ttcrpy的跨孔CT高斯牛顿算法及python代码分享(2)
  15. 计算机视觉教程2-6:八大图像特效算法制作你的专属滤镜(附Python代码)
  16. 解决安装ubuntu时,出现安装程序向硬盘复制文件时遇到的错误
  17. 动视暴雪宣布裁员约8%  780多名员工将被裁-千氪
  18. 这3款免费的Word转PDF转换器软件,建议收藏使用
  19. Unity 粒子 基础
  20. 《云云众声》第90期:久久不见 新年第90期有新精彩!

热门文章

  1. 零成本学arduino教程——74hc165扩展寄存器教程
  2. 烤仔DeFi课堂 | 从雅典到去中心化金融
  3. 盘点2022初级Java笔试题,选择题,简答题(右滑查看答案)
  4. MBA-day6数学-应用题-工程问题-习题
  5. 【限时干货】数据圈火爆的数据产品文章全集
  6. TPM零知识学习十一 —— tpm全安装流程复盘(下)
  7. 栈的应用之中缀表达式转后缀
  8. 《容忍与自由》读后感
  9. 你给文字描述,AI艺术作画,精美无比!附源码,快来试试!
  10. 《第一行代码》第三版之通知、多媒体(十)