HDU - 4734 F(x) (2013成都网络游戏,数字DP)

意甲冠军:求0-B见面<=F[A]所有可能的

思维:数字DP,内存搜索

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

int A, B;
int dp[20][200000];
int bit[20];

int dfs(int cur, int num, int flag) {
	if (cur == -1)
		return num >= 0;
	if (num < 0)
		return 0;
	if (!flag && dp[cur][num] != -1)
		return dp[cur][num];
	int ans = 0;
	int end = flag?

bit[cur]:9;
	for (int i = 0; i <= end; i++)
		ans += dfs(cur-1, num-i*(1<<cur), flag&&i==end);
	if (!flag)
		dp[cur][num] = ans;
	return ans;
}

int F(int x) {
	int tmp = 0;
	int len = 0;
	while (x) {
		tmp += (x%10)*(1<<len);
		len++;
		x /= 10;
	}
	return tmp;
}

int cal() {
	int len = 0;
	while (B) {
		bit[len++] = B%10;
		B /= 10;
	}
	return dfs(len-1, F(A), 1);
}

int main() {
	int t;
	int cas = 1;
	scanf("%d", &t);
	memset(dp, -1, sizeof(dp));
	while (t--) {
		scanf("%d%d", &A, &B);
		printf("Case #%d: %d\n", cas++, cal());
	}
	return 0;
}
时间: 2024-10-05 11:10:01

HDU - 4734 F(x) (2013成都网络游戏,数字DP)的相关文章

HDU - 4734 F(x) (2013成都网络赛,数位DP)

题意:求0-B的满足<=F[A]的所有可能 思路:数位DP,记忆化搜索 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int A, B; int dp[20][200000]; int bit[20]; int dfs(int cur, int num, int flag) { if (cur == -

HDU 4734 F(x)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:对于一个n位的十进制数字x=(AnAn-1An-2 ... A2A1),定义 F(x)=An*2n-1+An-1*2n-2+ ...+A2*2+A1*1.给出A.B,求在[0,B]之间有多少数字满足F(x)<=F(A)? 思路:数位DP.f[dep][x]表示到达dep剩余为x的方案数. i64 n,m; i64 f[25][N]; int a[25],num; i64 Sum; i64

HDU 4734——F(x)

HDU 4734——F(x) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:数i在0~B的范围内,求F(i)小于F(A)的个数 F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1 先把F(A)算出来=tot,然后按位计算,如果num(当前F(pos)<=tot,方案数+1 返回1,如果num>tot,返回0) dp[pos][num] 的状态表示为dp[当前第几位][最大值t

hdu 4734 F(x)(数位dp+优化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~b有多少个不大于f(a)的数 显然这题可以设这样的dp dp[len][count]表示前len位权值为count的有多少,然后显然的在len==0时return count>=f(a); 但是这样

HDU 4734 F(x)(数位DP)

Description For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there betwe

HDU 4734 —— F(x)

F(x) Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A 1 * 1. Now yo

HDU 4734 F(x) (数位DP,基础)

题意:  一个非负整数的十进制位是这样的 (AnAn-1An-2 ... A2A1),定义F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1.给出A和B,问[0,B]中有几个整数x的F(x)值<=F(A)? 思路: 算一下就知道F(x)值不会超过512*9,而B仅仅有8位十进制数,那么8*512*9就可以算出所有的统计了.对于每个询问,先计算F(A)的值,然后统计小于此值有几个就行了.统计的复杂度也是很低的.若是以前缀和来统计后面的个数的

HDU 4734 F(x) (数位DP)

F(x) Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 382    Accepted Submission(s): 137 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x

HDU - 4734 F(x) (数位DP)

For a decimal number x with n digits (A nA n-1A n-2 ... A 2A 1), we define its weight as F(x) = A n * 2 n-1 + A n-1 * 2 n-2 + ... + A 2 * 2 + A 1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B,