首页 > 代码库 > HDU 4352 XHXJ's LIS 数位DP + 状压
HDU 4352 XHXJ's LIS 数位DP + 状压
由LIS的nlogn解法 可以得出最后统计数组中数的个数即为LIS的长度 这样就可以状压了
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;typedef long long LL;const int maxn = 20;LL a,b,k;int len = 0,lim[maxn];LL f[maxn][1 << 10][11];void getlim(LL n) { len = 0; memset(lim,0,sizeof(lim)); while(n) { lim[len++] = n % 10; n /= 10; }}int check(int state) { int cnt = 0; while(state) { cnt += state & 1; state >>= 1; } return cnt;}int gao(int state,int num) { for(int i = num;i <= 9;i++) if(state & (1 << i)) { state &= ~(1 << i); break; } state |= (1 << num); return state;}LL dfs(int now,int state,int first,int bound) { if(now == 0) { if(check(state) == k) { return 1; } return 0; } LL ¬e = f[now][state][k]; int m = bound ? lim[now - 1] : 9; if(!bound && first && note != -1) return note; LL ret = 0; for(int i = 0;i <= m;i++) { int ns = gao(state,i); if(i || first) ret += dfs(now - 1,ns,1,bound && i == m); else ret += dfs(now - 1,state,0,bound && i == m); } if(!bound && first) note = ret; return ret;}LL solve(LL num) { getlim(num); return dfs(len,0,0,1);}int main() { memset(f,-1,sizeof(f)); int T,kase = 1; cin >> T; while(T--) { cout << "Case #" << kase++ << ":" << " "; cin >> a >> b >> k; cout << solve(b) - solve(a - 1) << endl; } return 0;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。