zoukankan      html  css  js  c++  java
  • POJ3252 Round Numbers 【数位dp】

    题目链接

    POJ3252

    题解

    为什么每次写出数位dp都如此兴奋?
    因为数位dp太苟了
    因为我太弱了

    (f[i][0|1][cnt1][cnt0])表示到二进制第(i)位,之前是否达到上界,前面已经有(cnt1)(1)(cnt0)(0)时的方案数
    显然当(cnt1 = 0)时就不存在任何前导数字了

    然后就记忆化搜索 分类讨论各种转移
    【为什么我写得好麻烦QAQ是不是我姿势不对】

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<map>
    #include<cstring>
    #include<algorithm>
    #define LL long long int
    #define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
    #define REP(i,n) for (int i = 1; i <= (n); i++)
    #define cls(s) memset(s,0,sizeof(s))
    #define mp(a,b) make_pair<int,int>(a,b)
    #define cp pair<int,int>
    using namespace std;
    const int maxn = 100005,maxm = 100005,INF = 1000000000;
    inline int read(){
    	int out = 0,flag = 1; char c = getchar();
    	while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
    	while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
    	return out * flag;
    }
    int bit[maxn],vis[33][2][33][33];
    LL f[33][2][33][33];
    LL cal(int n,int lim,int cnt1,int cnt0){
    	if (!n) return cnt0 >= cnt1;
    	if (vis[n][lim][cnt1][cnt0]) return f[n][lim][cnt1][cnt0];
    	vis[n][lim][cnt1][cnt0] = true;
    	LL& re = f[n][lim][cnt1][cnt0];
    	if (!lim && !cnt1) return re = cal(n - 1,0,1,0) + cal(n - 1,0,0,0);
    	else if (!lim){
    		int tot = cnt1 + cnt0 + n,least = (tot & 1) ? (tot >> 1) + 1 : (tot >> 1);
    		least = least - cnt0;
    		if (least <= 0) return re = (1 << n);
    		LL C = 1;
    		for (int i = 1; i <= n; i++){
    			C = C * (n - i + 1) / i;
    			if (i >= least) re += C;
    		}
    		return re;
    	}
    	else if (!cnt1){
    		if (!bit[n]) return re = cal(n - 1,1,0,0);
    		else return re = cal(n - 1,1,1,0) + cal(n - 1,0,0,0);
    	}
    	else {
    		if (!bit[n]) return re = cal(n - 1,1,cnt1,cnt0 + 1);
    		else return re = cal(n - 1,1,cnt1 + 1,cnt0) + cal(n - 1,0,cnt1,cnt0 + 1);
    	}
    }
    LL solve(int x){
    	cls(f); cls(vis);
    	int n = 0,tmp = x;
    	while (tmp) bit[++n] = (tmp & 1),tmp >>= 1;
    	return cal(n,1,0,0);
    }
    int main(){
    	int a = read(),b = read();
    	if (a > b) swap(a,b);
    	printf("%lld
    ",solve(b) - solve(a - 1));
    	return 0;
    }
    
    
  • 相关阅读:
    poj 2763 Housewife Wind
    hdu 3966 Aragorn's Story
    poj 1655 Balancing Act 求树的重心
    有上下界的网络流问题
    URAL 1277 Cops and Thieves 最小割 无向图点带权点连通度
    ZOJ 2532 Internship 网络流求关键边
    ZOJ 2760 How Many Shortest Path 最大流+floyd求最短路
    SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流
    怎么样仿写已知网址的网页?
    5-10 公路村村通 (30分)
  • 原文地址:https://www.cnblogs.com/Mychael/p/9022863.html
Copyright © 2011-2022 走看看