题目链接:http://codeforces.com/problemset/problem/768/B点击打开链接

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 nlr (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.

仔细观察可以发现 每个数被拆分成的最终序列是有规律的 这里以10举例

先将该数用二进制表示 1010

观察序列 发现二进制中的第一位的1位于1 3 5 7 9 11 13 15的位置

第二位的0位于2 6 10 14

第三位的1位于4 12

第四位的0位于8

将1010这个二进制数的每一位附上权值 每一位对应为1 2 4 8 从左到右位置为 0 1 2 3 (其实就是2的次方对应关系)

于是可以发现 对于序列中任意一个位置的值 都具有以下规律

设当前位置为position

position的值对应于 position的因子中最大的2的N次方数 即为该数二进制的第N位的值

有点难懂 还是举10的例子

对于序列的第3位 他的因子中最大的2的N次方数为1 即N=0 所以为二进制1010的第0位 也就是第一个1

对于序列的第6位 她的因子中最大的2的N次方数为2 即N=1 所以对应1010的第一个0

。。。。。。12。。。。。。。。。。。。。。。4 即N=2 所以对应第三位 也就是第二个1

找出规律之后 代码就很简单了

因为范围不大 因此枚举他需要的区间的每个位置 根据规律计算当前的数是不是1

#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <string.h>
#include <set>
using namespace std;
vector<long long int > s;
int main()
{long long int n,l,r;cin >> n >> l >> r;while(n!=0){s.push_back(n%2);n/=2;}long long int cnt=0;reverse(s.begin(),s.end());s.push_back(0);for(long long int i=l;i<=r;i++){long long int j=0;long long int mid=i;while(mid%2==0)j++,mid/=2;if(s[j]==1)cnt++;}cout << cnt;
}

CodeForces - 768B Code For 1(找规律)相关推荐

  1. CodeForces - Insertion Sort(打表找规律)

    题目链接:http://codeforces.com/gym/101955/problem/C Time limit:6.0 s Memory limit:1024 MB Problem Descri ...

  2. codeforces ECR 74 Standard Free2play(找规律)

    题目大意: 有一座山,山有h层.每一层都有平台.有些平台凸出来,有些平台是凹进去.主角一开始站在h层平台,他的目标是落到第0层.主角能够下山的方式只有一种:让高为x的平台和高为x-1平台的状态取反从而 ...

  3. Celex Update CodeForces - 1358C(打表找规律)

    During the quarantine, Sicromoft has more free time to create the new functions in "Celex-2021& ...

  4. Grid game CodeForces - 1104C 放格子|思维|找规律

    题意:4*4的格子中输入0放 2*1的图案输入1放1*2的图案 当摆满一行或一列后此行列图案清空 就想毛熊方块一样 分析:开始感觉很唬人 要搜索还是要dp啥的 后来发现原来2*1就放左下 1*2就放左 ...

  5. 【CodeForces - 735B】Urbanization (找规律,思维)

    题干: Local authorities have heard a lot about combinatorial abilities of Ostap Bender so they decided ...

  6. Codeforces 768B Code For 1 二分+区间查询

    点击打开链接 题意:给出n<=1e15 若存在n>1 则把n变为n/2,n%2,n/2,问[l,r]内1的个数? f[n]为n展开后得到的数字个数: f[n]=2*f[n/2]+1  迭代 ...

  7. Codeforces Round #715 (Div. 1) B. Almost Sorted 找规律

    传送门 文章目录 题意: 思路: 题意: 思路: 找规律yydsyydsyyds. 一看没什么想法,所以打了个表,好家伙,不打不知道,一打不得了,下面是n=6n=6n=6的符合要求的情况: 不难发现, ...

  8. codeforces:E. Add Modulo 10【状态压缩 + 找规律】

    分析 分类讨论,进行操作 [5,0]落回0 其余落到2 然后就停止 分两类 如果是第一类,必须是同一个0结尾才行 如果第二类,必须%20相同才行 ac code import sys input = ...

  9. [codeforces 1333A] Little Artem 读懂题+找规律+多举例

    Codeforces Round #632 (Div. 2)   比赛人数12810 [codeforces 1333A]   Little Artem   读懂题+找规律+多举例 总目录详见http ...

最新文章

  1. git更新代码报错,error: The following untracked working tree files would be overwritten by ch
  2. Mac环境PHP踩过的“坑” (一)函数重载
  3. 分页缓冲池如何关闭_线程池没你想的那么简单
  4. 电脑出现 远程计算机或者设备不受连接
  5. wine最小化游戏后无法恢复的问题
  6. Office文档模型深入---Outlook文档模型与开发实战(1)
  7. 收藏!目标检测优质综述论文总结!
  8. 人工智能诗歌写作平台_人工智能教作文,只写出二类文,人类语文老师稳赢
  9. Ubuntu 16.04 安装 VMware-Workstation-12
  10. testng 定时构建_10自动化测试_持续集成篇
  11. oracle:10g下载地址(转载)
  12. 脚本程序gdb 脚本
  13. python单词字典排序_python字典排序
  14. FISCO BCOS Solidity 如何import引入其他代码库 Source “Table.sol“ not found: File not found
  15. mac中 安装mysql无法启动_Mac 下安装MySQL(dmg方式),无法启动
  16. 收藏!深度学习必读10篇经典算法论文总结!
  17. linux chmod 777 r,chmod -R 777 的3种补救办法,附有linux chmod命令语法和结构详解
  18. 小学作文批改评语大全
  19. ISCC 2022 部分
  20. VB集成无标题栏Form图片按钮Activex

热门文章

  1. Vue全家桶+MongoDB+Koa2全栈开发网站
  2. 鱼是最后一个看到水的
  3. vue实现列表的无缝滚动
  4. SPDK简介(其与Ceph rbd的关系)
  5. Bootstrap 标签 label
  6. Erlang和Elixir简介
  7. 数据恢复软件:FonePaw Data Recovery mac中文版
  8. 公需科目2020快速学习_2021公需科目快速学习方法
  9. 数据挖掘学习——SOM网络聚类算法+python代码实现
  10. 华为的价值主张带给施工企业数字化转型的价值思考