UVA 12670 数位DP
来源:互联网 发布:日本留学融入 知乎 编辑:程序博客网 时间:2024/06/05 18:07
UVA 12670
题目链接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536
题意:
给一个数字范围[x,y],问在这个范围内所有数表示成二进制后,“1”的个数和。
思路:
简单的数位DP,用那种BIT的思想去理解就好。
源码:
#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <iostream>#include <string>using namespace std;#define LL long longconst int MAXN = 70;LL dp[MAXN];LL C[MAXN];LL cal(LL a){ string str = ""; while(a){ str += a % 2 + '0'; a /= 2; } LL ans = 0;// cout << " str = " << str<<endl;// str.reverse(); int len = str.length(); int cnt = 0; for(int i = len - 1 ; i >= 0 ; i--){ if(str[i] == '1'){// printf("i = %d, dp[i] = %lld\n", i, dp[i]); ans += dp[i] + cnt * C[i]; cnt++; } } ans += cnt; return ans;}void init(){ dp[0] = 0; C[0] = 1; for(int i = 1 ; i < MAXN ; i++){ dp[i] = dp[i - 1] * 2 + C[i - 1]; C[i] = C[i - 1] * 2; }}int main(){ init(); LL a, b; while(scanf("%lld%lld", &a, &b) != EOF){ LL ans = cal(b) - cal(a - 1);// printf("cal1 = %lld, cal2 = %lld\n", cal(b), cal(a - 1)); printf("%lld\n", ans); } return 0;}
0 0
- UVA 12670 数位DP
- uva 10401 数位DP
- uva 11361 数位DP
- uva fzu2019(数位dp)
- 数位DP UVA
- UVA 10712 - Count the Numbers (数位DP)
- uva 417 - Word Index(数位dp)
- uva 10712 - Count the Numbers(数位dp)
- UVA 12486 Space Elevator(数位DP)
- 数位DP(越大越好,uva 12105)
- UVA 12105 Bigger is Better(数位dp)
- UVa 11361 Investigating Div-Sum Property / 数位DP
- UVA 11038 How Many O's(数位dp)
- UVA 11361 - Investigating Div-Sum Property(数位DP)
- uva 11361 - Investigating Div-Sum Property(数位dp)
- uva 1489 - Math teacher's homework(数位dp)
- UVa 11361 - Investigating Div-Sum Property (数位DP)
- UVa 12683 Odd and Even Zeroes(数论+数位DP)
- 罗马数字与阿拉伯数字相互转换
- 第4章 虚拟机性能监控与故障处理工具
- [logstash-input-log4j]插件使用详解
- 欢迎使用CSDN-markdown编辑器
- WIFI基本知识整理
- UVA 12670 数位DP
- Android Studio快捷键
- 【实验】两个有序顺序表的合并
- VMware提示:此虚拟机似乎正在使用中,取得该虚拟机的所有权失败错误的解决方案
- 我的ASPxGridView控件的数据是在后台绑定的,但是实现不了筛选和分页的功能,只能显示一页的数据?
- caffe学习笔记-数据库制作及计算均值文件命令格式(windows版)
- JNDI数据源的配置
- 【jQuery应用】导航栏滑块随鼠标移动
- MessagePack介绍