【DP】 HDOJ 4734 F(x)

1941 단어 디지털 dp
디지털 DP.dp[i][j]는 길이가 i이고 크기가 j의 개수를 초과하지 않습니다...
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 500005
#define maxm 300005
#define eps 1e-3
#define mod 9999677
#define INF 0x3f3f3f3f
#define PI (acos(-1.0))
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid 
#define rson o<<1 | 1, mid+1, R
#define pii pair<int, int>
#pragma comment(linker, "/STACK:16777216")
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
//head

int dp[12][10005];
int digit[20];
int two[maxn];

int dfs(int pos, int sum, int limit)
{
	if(dp[pos][sum] != -1 && !limit) return dp[pos][sum];
	if(!pos) return true;
	int a = 0, b = limit ? digit[pos] : 9, ans = 0;
	for(int i = a; i <= b; i++) if(sum-i*two[pos] >= 0) ans += dfs(pos-1, sum-i*two[pos], limit & i == b);
	if(!limit) dp[pos][sum] = ans;
	return ans;
}

void work()
{
	int a, b;
	scanf("%d%d", &a, &b);
	int cnt = 0;
	int t = 0;
	while(a) t += two[++cnt] * (a % 10), a /= 10;
	a = t;
	cnt = 0;
	while(b) digit[++cnt] = b % 10, b /= 10;
	printf("%d
", dfs(cnt, a, 1)); } int main() { int _, __ = 0; memset(dp, -1, sizeof dp); two[1] = 1; for(int i = 2; i <= 12; i++) two[i] = two[i-1] * 2; while(scanf("%d", &_)!=EOF) { while(_--) printf("Case #%d: ", ++__), work(); } return 0; }

좋은 웹페이지 즐겨찾기