[zoj 3416/hdu 3709]数位DP

题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等。0<=L<=R<=1e18

思路:由于任何非0正数最多只有1个位置作为中心轴使得它是平衡数。于是可以按中心轴的位置分类统计答案。令dp[p][i][j]表示中心轴在p位(p>=0)前i位且左边比右边的加权和已经多j的方案数,枚举当前第i位放的数k,那么dp[p][i][j]=∑dp[p][i-1][j+(p-i+1)*k]。

求出dp值后,只需从高位向低位统计,统计时也是按中心轴分类,即枚举中心轴,然后根据前多少位相同,维护一下已确定的数到中心轴的加权和,然后加上对应dp值。由于0这个数无论以什么作为中心轴都会使答案加1,所以最后需减去重复的。

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 29 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 30 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 31 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 32 #define all(a) (a).begin(), (a).end()
 33 #define lowbit(x) ((x) & (-(x)))
 34 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 35 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 36 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 37 #define pchr(a) putchar(a)
 38 #define pstr(a) printf("%s", a)
 39 #define sstr(a) scanf("%s", a)
 40 #define sint(a) scanf("%d", &a)
 41 #define sint2(a, b) scanf("%d%d", &a, &b)
 42 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 43 #define pint(a) printf("%d\n", a)
 44 #define test_print1(a) cout << "var1 = " << a << endl
 45 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 46 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 47 #define mp(a, b) make_pair(a, b)
 48 #define pb(a) push_back(a)
 49
 50 typedef unsigned int uint;
 51 typedef long long LL;
 52 typedef pair<int, int> pii;
 53 typedef vector<int> vi;
 54
 55 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 56 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 57 const int maxn = 1e5 + 7;
 58 const int md = 100000007;
 59 const int inf = 1e9 + 7;
 60 const LL inf_L = (LL)1e18 + 7;
 61 const double pi = acos(-1.0);
 62 const double eps = 1e-6;
 63
 64 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 65 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 66 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 67 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 68 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 69 int make_id(int x, int y, int n) { return x * n + y; }
 70
 71 struct Node {
 72     LL a[808];
 73     LL &operator [] (const int x) {
 74         return a[x + 401];
 75     }
 76 } dp[20][20];
 77
 78 void div_digit(LL x, int a[], int &n) {
 79     int p = 0;
 80     a[p ++] = x % 10;
 81     x /= 10;
 82     while (x) {
 83         a[p ++] = x % 10;
 84         x /= 10;
 85     }
 86     n = p;
 87 }
 88
 89 void init() {
 90     rep_up0(p, 18) dp[p][0][0] = 1;
 91     rep_up0(p, 18) {
 92         rep_up1(i, 17) {
 93             for (int j = -400; j <= 400; j ++) {
 94                 rep_up0(k, 10) {
 95                     int val = j + (p - i + 1) * k;
 96                     if (val < -400 || val > 400) continue;
 97                     dp[p][i][j] += dp[p][i - 1][val];
 98                 }
 99             }
100         }
101     }
102 }
103
104 LL calc(LL n) {
105     if (n == -1) return 0;
106     if (n == (LL)1e18) return (LL)12644920956811384;
107     LL ans = 0;
108     int a[20], len;
109     div_digit(n, a, len);
110     rep_up0(p, 18) {
111         int sum = 0;
112         rep_down0(i, len) {
113             rep_up0(j, a[i]) {
114                 int val = sum + (p - i) * j;
115                 if (val < -400 || val > 400) continue;
116                 ans += dp[p][i][val];
117             }
118             sum += a[i] * (p - i);
119         }
120     }
121     rep_up0(p, 18) {
122         int sum = 0;
123         rep_down0(i, len) {
124             sum += a[i] * (p - i);
125         }
126         if (sum == 0) ans ++;
127     }
128     return ans - 17;
129 }
130
131 int main() {
132     //freopen("in.txt", "r", stdin);
133     init();
134     int T;
135     cin >> T;
136     LL n, m;
137     while (T --) {
138         cin >> n >> m;
139         cout << calc(m) - calc(n - 1) << endl;
140     }
141     return 0;
142 }

另外,灵机一动想出来一种写法(如有雷同,纯属巧合),用起来也还不错哦!

时间: 2024-10-06 23:05:13

[zoj 3416/hdu 3709]数位DP的相关文章

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

HDU 3709 数位dp

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5276    Accepted Submission(s): 2523 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

Balanced Number HDU - 3709 数位dp

题意: 给出范围 算出 满足  选取一个数中任一一个 树作为支点  两边的数分别乘以到中心的距离和 左和等于右和   的数有多少个 数位DP题 状态转移方程为dp[pos][x][state]=dp[pos-1][x][state-(pos-x)*i]  表示为pos位上的数字为 i    以x为支点  则  以中心点左为负右为正   pos左右的数乘以权值的 和为state pos-1位就是 把pos位的 i乘以权值减去   即 state-(pos-x)*i 如果枚举到最后一位的时候 sta

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了.dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数. #include <bits/stdc++.h> using namespace std; typedef long

hdu 5179 数位dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1