zoukankan      html  css  js  c++  java
  • LightOJ 1032 按位dp

    /********************
    
    LightOJ 1032
    
    Author:Cdegree
    
    ********************/
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <cctype>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <map>
    #include <algorithm>
    #include <iostream>
    #include <string>
    #include <set>
    #define X first
    #define Y second
    #define sqr(x) (x)*(x)
    #pragma comment(linker,"/STACK:102400000,102400000")
    using namespace std;
    const double PI = acos(-1.0);
    map<int, int>::iterator it;
    typedef long long LL ;
    template<typename T> void checkmin(T &x, T y) {x = min(x, y);}
    template<typename T> void checkmax(T &x, T y) {x = max(x, y);}
    
    LL dp[33][2][2][33];
    int d[33];
    void init(LL n) {
        for(int i = 0; i < 33; ++i) {
            d[i] = (n >> i) & 1;
        }
        //reverse(d, d + 33);
        for(int i = 0; i < 33; ++i) {
            //printf("%d", d[i]);
        } //puts("");
    }
    LL gao(int pos, int pre, int pred, LL cnt) {
    
        if(pos == -1) {
            return cnt;
        }
        else {
            LL &ret = dp[pos][pre][pred][cnt];
            if(~ret)return ret;
            ret = 0;
            int up = pre ? 1 : d[pos];
            //printf("pos = %d pre = %d pred = %d cnt = %lld
    ", pos, pre, pred, cnt);
            for(int i = 0; i <= up; ++i) {
                //printf("i=%d ", i);
                int add = pred && i;
                ret += gao(pos - 1, pre || i < d[pos], i, cnt + add);
            } //puts("");
            //printf("ret = %lld
    ", ret);
            return ret;
        }
    }
    
    LL get_res(LL n) {
        init(n);
        memset(dp, -1, sizeof(dp));
        LL ret = gao(32, 0, 0, 0);
        return ret;
    }
    int main() {
        int T;
        LL n;
        scanf("%d", &T);
        for(int t = 1; t <= T; ++t) {
            scanf("%I64d", &n);
            LL res = get_res(n);
            printf("Case %d: %lld
    ", t, res);
        }
        return 0;
    }
  • 相关阅读:
    ios7 苹果原生二维码扫描(和微信类似)
    ios7之后 根据UILabel的文字计算frame的方法
    [Luogu1944] 最长括号匹配
    [bzoj3916] friends
    [NOIp2006] 能量项链
    [NOIp2003] 加分二叉树
    [Luogu1353] 跑步Running
    [Luogu2214] Mooo Moo S
    [POJ2452] Sticks Problem
    [POJ2406] Power Strings
  • 原文地址:https://www.cnblogs.com/cxw199204/p/3348728.html
Copyright © 2011-2022 走看看