您的位置:首页 > 其它

lightoj 1032 - Fast Bit Calculations 【数位dp】

2016-03-16 16:26 309 查看
题目链接:lightoj 1032 - Fast Bit Calculations

定义二进制中相邻的两个1价值为1。如(11011)2价值为2,(1111)2价值为3。

题意:定义v(i)为i的价值,问∑Ni=1v(i)。

事实证明我又一次把简单问题复杂化。。。TMD,一开始直接设dp[i][j][cnt]搞,看结果少了,果断放弃了。用SB思路AC后才发现dp数组没有初始化。我是SBaaaaa.

SB思路(已AC):用sum[i]表示i的二进制中相邻1的个数。如(111)2 = 3。用num[i]表示i的二进制是否存在相邻位的1,存在为1,反之为0。

那么ans=∑Ni=1sum[i]−∑Ni=1num[i]。如果你做过hdoj4507 的话下面就很好想了。考虑单个位的1来维护sum和num就好了。但有个trick,如(1011)2,这时第一个1是不能计算的,因为它不是连续的。

设dp[i][j][flag][cnt]表示处理到第i位、前一位填j、共有cnt个连续的1,flag表示是否连续,连续就把该位的1加上,反之不加。

SB思路AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e7 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
struct Node {
LL num, sum;
};
Node dp[100][2][2][100];
void init() {
for(int i = 0; i < 100; i++) {
for(int j = 0; j < 2; j++) {
for(int k = 0; k < 2; k++) {
for(int p = 0; p < 100; p++)
dp[i][j][k][p].num = dp[i][j][k][p].sum = -1;
}
}
}
}
int bit[100]; int len;
bool get(int pre, int now, bool flag) {
if(pre == 1 && now == 1) return true;
if(pre == 1 && now == 0) return false;
if(pre == 0 && now == 1) return true;
if(pre == 0 && now == 0) return flag;
}
Node DFS(int pos, int pre, int cnt, bool flag, bool yes)
{
if(pos == -1) {
Node ans; ans.num = cnt > 0; ans.sum = 0;
return ans;
}
if(!yes && dp[pos][pre][flag][cnt].num != -1) return dp[pos][pre][flag][cnt];
int End = yes ? bit[pos] : 1;
Node ans, temp; ans.num = ans.sum = 0;
for(int i = 0; i <= End; i++) {
if(i == 1 && pre == 1) {
temp = DFS(pos-1, i, cnt+1, get(pre, i, flag), yes&&i==End);
ans.num += temp.num; ans.sum += temp.num + temp.sum;
}
else {
temp = DFS(pos-1, i, cnt, get(pre, i, flag), yes&&i==End);
ans.num += temp.num; ans.sum += temp.num * i * flag + temp.sum;
//if(pos == 3) cout << i << ' ' << ans.sum << endl;
}
}
if(!yes) dp[pos][pre][flag][cnt] = ans;
//cout << pos << ' ' << pre << ' ' << cnt << ' ' << yes << ' ' << zero << ' ' << ans.num << ' ' << ans.sum << endl;
return ans;
}
LL Count(int n)
{
len = 0;
while(n) {
bit[len++] = n & 1;
n >>= 1;
}
return DFS(len-1, 0, 0, 1, 1).sum - DFS(len-1, 0, 0, 1, 1).num;
}
int main()
{
int t, kcase = 1; scanf("%d", &t);
while(t--)
{
int n; scanf("%d", &n); init();
printf("Case %d: %lld\n", kcase++, Count(n));
}
return 0;
}


AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define PI acos(-1.0)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e7 + 1;
const int pN = 1e6;// <= 10^7
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
void getmax(int &a, int b) {a = max(a, b); }
void getmin(int &a, int b) {a = min(a, b); }
void add(LL &x, LL y) { x += y; x %= MOD; }
LL dp[100][2][100];
int bit[100];
LL DFS(int pos, int pre, int cnt, bool yes)
{
if(pos == -1) return cnt;
if(!yes && dp[pos][pre][cnt] != -1) return dp[pos][pre][cnt];
int End = yes ? bit[pos] : 1;
LL ans = 0;
for(int i = 0; i <= End; i++) {
if(pre == 1 && i == 1) {
ans += DFS(pos-1, i, cnt+1, yes&&i==End);
}
else {
ans += DFS(pos-1, i, cnt, yes&&i==End);
}
}
if(!yes) dp[pos][pre][cnt] = ans;
return ans;
}
LL Count(int n)
{
int len = 0;
while(n) {
bit[len++] = n & 1;
n >>= 1;
}
return DFS(len-1, 0, 0, 1);
}
int main()
{
int t, kcase = 1; scanf("%d", &t);
while(t--)
{
int n; scanf("%d", &n); CLR(dp, -1);
printf("Case %d: %lld\n", kcase++, Count(n));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: