[二分] cf 768B Code For 1
来源:互联网 发布:个人博客整站源码 编辑:程序博客网 时间:2024/05/22 14:23
[二分] cf 768B Code For 1
@(ACM题目)[二分图]
比赛时各种zz,搞了2h才搞出来(ノಠ益ಠ)ノ彡┻━┻,最后用的是递归求解。之后看了别人的才发现很简洁的规律(二进制无敌!进制无敌!制无敌!无敌!敌!)。
以后需要注意:与复制有关的要考虑二进制
题意
将一个数n拆成数列
题意转换
拆的操作即为n>>1,n&1,n>>1,其中n&1即n在二进制下的末位,而去掉这个末位得到的就是n>>1。也就是要将n的二进制表示中的最低位放在中间,剩余的二进制位在这个最低位左右各放一份,继续进行拆操作。
设数字n对应的二进制表示为abcde,其中a、b、c、d、e为0或1(如22表示为10110),则对应“拆”的过程为:
- 以e为中心:abcd, e, abcd
- 以d为中心:abc, d, abc, e, abc, d, abc
- 以c为中心:ab, c, ab, d, ab, c, ab, e, ab, c, ab, d, ab, c, ab
- 以b为中心:a, b, a, c, a, b, a, d, a, b, a, c, a, b, a, e, a, b, a, c, a, b, a, d, a, b, a, c, a, b, a
规律
设n的二进制表示为
#include<iostream>#include<cstdio>#include <cstring>#include<cmath>#include<cstdlib>#include<climits>#include<algorithm>#include<vector>#include<map>#include<set>#include<list>#include<stack>using namespace std;typedef long long LL;const int maxn = 55;int bn[maxn];int main(){ LL n, l, r; cin>>n>>l>>r; for(int i = log2(n); i >= 0; i--) { bn[i] = n&1; n >>= 1; } int res = 0; for(LL i = l; i <= r; i++) res += bn[(int)log2(i&-i)]; cout<<res<<endl; return 0;}
递归程序
f(i)代表第1到第i位有多少个1,递归地算。
#include<iostream>#include<cstdio>#include <cstring>#include<cmath>#include<cstdlib>#include<climits>#include<algorithm>#include<vector>#include<map>#include<set>#include<list>#include<stack>using namespace std;typedef long long LL;const int maxn = 55;bool ck[maxn];map<LL, int> mp;LL f(LL n){ LL lb = n&(-n); if(n==0) return 0; if(n==lb) return ck[mp[n]+1]+f(n-1); else if(((n+1)&(-n-1))==n+1) return ck[mp[n+1]]+2*f(n>>1); return f(n-lb)+f(lb);}int main(){ LL cur = 1; for(int i = 0; i <= 51; i++) { mp[cur] = i; cur<<=1; } LL n, l, r, res = 0; cin>>n>>l>>r; LL nn = n; int cnt = 0; while(nn) { cnt++; nn>>=1; } nn=n; while(nn) { ck[cnt--] = nn&1; nn>>=1; } cout<<f(r)-f(l-1)<<endl; return 0;}
0 0
- [二分] cf 768B Code For 1
- cf B. Code For 1
- 768B Code For 1 [二分]
- Codeforces 768B Code For 1 二分+区间查询
- B. Code For 1
- B. Code For 1
- cf:B-QR code
- codeforces 768 B Code For 1 (数学)
- Codeforces 768B Code For 1
- 【codeforces 768B】Code For 1
- Codeforces 768B Code For 1[DFS]
- codeforece 768 B. Code For 1
- Codeforces 768B Code for 1 【分治】
- Codeforces 768B Code For 1
- CodeForces 768B Code For 1
- codeforces-768B Code for 1
- CF 397 B. Code obfuscation
- CF 287B(Pipeline-二分)
- 移动APP测试之安全性测试策略分析
- 车牌识别-模板匹配-BP神经网络-卷积神经网络[深度学习]
- Flask, Angularjs共存之道
- 算法训练 最短路 Bellman-Ford
- jQuery选择器 过滤选择器(六.表单对象属性过滤选择器)
- [二分] cf 768B Code For 1
- Fragment 出栈的方法popBackStack需要特别注意的一点
- datagridview 限制某列只能输入数字(方法一)
- iOS之WKWebview中cookie问题的解决办法
- mysql创建组合索引
- 微服务架构基础——解读六边形架构
- 工作用到的基础知识
- Java servlet 实现时钟效果
- Spring MVC中使用Thymeleaf模板引擎