lightoj 1032 - Fast Bit Calculations 【数位dp】

题目链接: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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值