盒子
盒子
文章目录
  1. 题目
  2. 题意
  3. 思路
  4. 代码

HDU 4933 Miaomiao's Function(数位dp+大数)

题目

源地址:http://acm.hdu.edu.cn/showproblem.php?pid=4933

题意

一道数学题,题意不好描述,见题面吧。

思路

题目中的f(x)比较好求,主要是求g(x)。
g(x)定义为x的各个位数交错和,现在要求一个区间[l, r]的g(x)和。显然是用数位dp做。
不过数据比较大,需要用到大数。
dp[pos][one]表示到pos位,前面是否全为零的交错和

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#define pb push_back
#define INF 1 << 30
#define fi first
#define se second
#define debug puts("=====================");
using namespace std;
typedef long long ll;
struct bigint { //flag表示正负,a数组从低位到高位, 从1开始
int flag;
int a[210];
bigint() {
flag=1;
memset(a,0,sizeof a);
}
void clear() {
for (int i = 200; i >= 0; i--) a[i] = 0;
}
void out() {
for (int i = 10; i >= 1; i--) printf("%d", a[i]);
cout<<endl;
}
};
bigint operator +(bigint a, bigint b);
bigint operator -(bigint a, bigint b);
bigint operator +(bigint a, bigint b) {
if (a.flag==b.flag) {
bigint c;
int g=0;
for(int i=1; i<=200; i++) {
c.a[i]=a.a[i]+b.a[i]+g;
g=c.a[i]/10;
c.a[i]%=10;
}
c.flag=a.flag;
return c;
} else {
if (a.flag==1) {
b.flag=1;
return a-b;
} else {
a.flag=1;
return b-a;
}
}
}
int cmp(const bigint &a, const bigint &b) {
for(int i=200; i>=1; i--) if (a.a[i]!=b.a[i]) if (a.a[i]<b.a[i]) return -1;
else return 1;
return 0;
}
bigint operator -(bigint a, bigint b) {
if (a.flag==-1 || b.flag==-1) {
if (a.flag==-1 && b.flag==-1) {
a.flag=1;
b.flag=1;
return b-a;
}
if (a.flag==-1) {
a.flag=1;
a=a+b;
a.flag=-1;
return a;
}
if (b.flag==-1) {
b.flag=1;
return a+b;
}
}
if (cmp(a,b)==-1) {
a=b-a;
a.flag=-1;
return a;
}
bigint c;
int g=0;
for(int i=1; i<=200; i++) {
c.a[i]=a.a[i]-b.a[i]-g;
if (c.a[i]<0) c.a[i]+=10, g=1;
else g=0;
}
return c;
}
bigint operator *(bigint a, int t) {
if (t==0) {
bigint c;
return c;
}
if (t<0) {
a.flag*=-1;
t*=-1;
}
int g=0;
bigint c;
c.flag=a.flag;
for(int i=1; i<=200; i++) {
c.a[i]=a.a[i]*t+g;
g=c.a[i]/10;
c.a[i]%=10;
}
return c;
}
int iszero(bigint z) {
for(int i=1; i<=200; i++) if (z.a[i]!=0) return 0;
return 1;
}
bigint toBigint(char *s) {
int len=strlen(s+1);
bigint c;
for(int i=len;i>=1;i--) c.a[len-i+1]=s[i]-'0';
return c;
}
bigint toBigint(int a) {
bool flag=true;
if(a<0) {
a=-a;
flag=false;
}
char s[10];
int cnt=1;
while(a) {
s[cnt++]=(a%10+'0');
a/=10;
}
for(int i=1,j=cnt-1; i<j; i++,j--)
swap(s[i],s[j]);
s[cnt]='\0';
bigint q=toBigint(s);
if(!flag)
q.flag=-1;
return q;
}
int modd(bigint c, int z) {
int g=0;
for(int i=200; i>=1; i--) g=(g*10+c.a[i])%z;
if (c.flag==-1) g*=-1;
return g;
}
char l[105], r[105];
bigint base[105], g[105], dp[105][2];
bool vis[105][2] = {0};
int bit[105];
bigint dfs(int pos, bool one, bool flag) {
if (pos == 0) {
if (!flag) return toBigint(45);
else return toBigint(bit[0] * (bit[0] + 1) / 2);
}
if (!flag && vis[pos][one]) return dp[pos][one];
int u = flag ? bit[pos] : 9;
bigint ans;
for (int i = 0; i <= u; i++) {
int nxt;
if (flag && i == u) nxt = 1, ans = ans + g[pos - 1] * i;
else nxt = 0, ans = ans + base[pos] * i;
if (!i && one) ans = ans + dfs(pos - 1, 1, nxt);
else ans = ans - dfs(pos - 1, 0, nxt);
}
if (!flag) {
vis[pos][one] = 1;
dp[pos][one] = ans;
}
return ans;
}
bigint calc(char *s) {
int pos = strlen(s);
for (int i = 0; i < pos; i++) bit[pos - i - 1] = s[i] - '0';
bigint tmp;
tmp.a[1] = 1;
g[0].clear();
g[0].a[1] = bit[0];
g[0] = g[0] + tmp;
for (int i = 1; i < pos; i++) g[i] = g[i - 1] + base[i] * bit[i];
return dfs(pos - 1, 1, 1);
}
void solve() {
bigint R = calc(r);
bigint L = calc(l);
int pos = strlen(l);
int res = 0, k = 0;
for (int i = pos - 1; i >= 0; i--) {
if (!k) res += bit[i];
else res -= bit[i];
k ^= 1;
}
bigint tmp = toBigint(res);
tmp = R - L + tmp;
if (iszero(tmp)) {
puts("Error!");
return ;
}
res = modd(tmp, 9);
if (res <= 0) res += 9;
int s = modd(tmp, res);
if (s < 0) s += res;
printf("%d\n", s);
}
int main () {
base[0].a[1] = 1;
for (int i = 1; i <= 100; i++) base[i] = base[i - 1] * 10;
int _;
cin>>_;
while(_--) {
scanf("%s%s", l, r);
solve();
}
return 0;
}