lightoj - 1032 - Fast Bit Calculations - 数位dp
来源:互联网 发布:eia数据今天数据 编辑:程序博客网 时间:2024/06/06 08:35
题意:将一个十进制数转化为二进制后,统计这一位是1,且下一位也是1的位数为这个数的权值,例如,6的二进制为110,第一1的下一位也是1,所以6的权值为1。给一个十进制数n,求0到n所有的十进制数的权值和。
题解:数位dp,可以参考算法合集之《浅谈数位类统计问题》,用树形图来理解就容易多了。
#include <bits/stdc++.h>//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define ll long long#define SZ(x) ((int)(x).size()) #define ALL(v) (v).begin(), (v).end()#define foreach(i, v) for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++ i)#define reveach(i, v) for (__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++ i) #define REP(i,a,n) for ( int i=a; i<int(n); i++ )#define FOR(i,a,n) for ( int i=n-1; i>= int(a);i-- )#define lson rt<<1, L, m#define rson rt<<1|1, m, Rtypedef pair<int, int> pii;typedef pair<ll, ll> pll;#define mp(x, y) make_pair(x, y)#define pb(x) push_back(x)#define fi first#define se second#define CLR(a, b) memset(a, b, sizeof(a))#define Min(a, b) a = min(a, b)#define Max(a, b) a = max(a, b)const int maxn = 50;int T;int kase;int n;int a[maxn];ll dp[maxn][2];ll cnt[maxn];void ini(){ REP(i, 1, maxn){ dp[i][1] = dp[i-1][0] + dp[i-1][1] + (1 << (i - 1)); dp[i][0] = dp[i-1][0] + dp[i-1][1]; }}ll solve(){ CLR(a, 0); CLR(cnt, 0); ll res = 0; n ++; int m = 0; while(n){ a[m ++] = n % 2; n /= 2; } REP(i, 0, m){ cnt[i+1] = cnt[i]; if(a[i]) res += dp[i][0], cnt[i+1] += (1 << i); if(a[i] && a[i+1]) res += cnt[i]; } return res;}int main(){#ifdef ac freopen("in.txt","r",stdin);#endif //freopen("out.txt","w",stdout); scanf("%d", &T); ini(); while(T--){ scanf("%d", &n); printf("Case %d: %lld\n", ++ kase, solve()); } return 0;}
0 0
- lightoj 1032 Fast Bit Calculations 数位dp
- LightOJ 1032 - Fast Bit Calculations (数位dp)
- LightOJ - 1032 Fast Bit Calculations(数位DP)
- LightOJ 1032 Fast Bit Calculations(数位DP)
- lightoj 1032 - Fast Bit Calculations 【数位dp】
- lightoj - 1032 - Fast Bit Calculations - 数位dp
- lightoj - 1032 Fast Bit Calculations (数位dp)总结
- LightOJ 1032 Fast Bit Calculations(数位DP)
- lightoj 1032 - Fast Bit Calculations (数位DP)
- LightOJ 1032 - Fast Bit Calculations (数位dp)
- lightoj 1032 - Fast Bit Calculations 二进制数位dp
- lightoj 1032 Fast Bit Calculations(数位dp)
- LightOJ 1032 - Fast Bit Calculations(dp)
- 1032 - Fast Bit Calculations 数位dp
- LightOJ 1032 Fast Bit Calculations
- lightoj 1032 - Fast Bit Calculations
- 【Lightoj 1032 Fast Bit Calculations 】
- [light oj 1032]Fast Bit Calculations[数位DP]
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) B. Bear and Compressing
- 单片机设计经验设计技巧集锦
- 【HPU】[1737]老王特警队
- 如何在Mac OS X上安装 Ruby运行环境
- linux下测试磁盘的读写IO速度
- lightoj - 1032 - Fast Bit Calculations - 数位dp
- form_for的关系
- MY GOAL
- java.堆栈浅层小知识
- windows客户端开发--客户端国际化中特殊处理(日期等)
- ffmpeg 编码示例
- Exception:在spring上用mybatis时报错
- 手写服务器httpserver_xml配置文件_sax解析基础应用JAVA205-206
- 《leetCode》:Largest Number