UVA 12670 数位DP
2015-09-23 09:03
453 查看
UVA 12670
题目链接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536
题意:
给一个数字范围[x,y],问在这个范围内所有数表示成二进制后,“1”的个数和。
思路:
简单的数位DP,用那种BIT的思想去理解就好。
源码:
题目链接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536
题意:
给一个数字范围[x,y],问在这个范围内所有数表示成二进制后,“1”的个数和。
思路:
简单的数位DP,用那种BIT的思想去理解就好。
源码:
[code]#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <iostream> #include <string> using namespace std; #define LL long long const 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; }
相关文章推荐
- Shell 文字 顶替tomcat星团war包裹
- Activity的task
- 团队博客作业
- [logstash-input-log4j]插件使用详解
- 罗马数字与阿拉伯数字相互转换
- 欢迎使用CSDN-markdown编辑器
- 源码推荐(9.23):YT_ShopTypeView商品分类菜单,双向滚动slider
- MySQL 设计与开发规范
- ios xcode开发工具快捷键
- Android中通信协议大全
- PHP面试总结
- c++统计文件中字符个数代码汇总
- iOS 代码实践总结
- iOS9适配
- WaitForSingleObject函数的使用
- Linux使用expect脚本实现远程机器自动登录
- 数据结构之AVL树
- Java对象的序列化和反序列化
- 数据结构与算法分析2.19题
- LabVIEW的主界面如何隐藏