Skip to content

Commit 2c2fbe7

Browse files
feat: add c solution to lc problem: No.3753 (#5249)
1 parent 53880cb commit 2c2fbe7

3 files changed

Lines changed: 226 additions & 0 deletions

File tree

solution/3700-3799/3753.Total Waviness of Numbers in Range II/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,83 @@ tags:
130130

131131
```
132132

133+
#### C
134+
135+
```c
136+
static int len, digits[20];
137+
static long long memoCnt[20][11][11][2];
138+
static long long memoSum[20][11][11][2];
139+
static char vis[20][11][11][2];
140+
static long long cnt, sum;
141+
142+
static void dfs(int pos, int pp, int pr, int st, int ti) {
143+
if (pos == len) {
144+
cnt = 1;
145+
sum = 0;
146+
return;
147+
}
148+
if (!ti && vis[pos][pp][pr][st]) {
149+
cnt = memoCnt[pos][pp][pr][st];
150+
sum = memoSum[pos][pp][pr][st];
151+
return;
152+
}
153+
int h = ti ? digits[pos] : 9;
154+
long long c = 0, s = 0;
155+
for (int d = 0; d <= h; d++) {
156+
int ns = st || d;
157+
long long a = 0;
158+
int npp, np;
159+
if (!ns) {
160+
npp = 10;
161+
np = 10;
162+
} else if (!st) {
163+
npp = 10;
164+
np = d;
165+
} else {
166+
if (pp != 10 && pr != 10 && ((pr > pp && pr > d) || (pr < pp && pr < d)))
167+
a = 1;
168+
npp = pr;
169+
np = d;
170+
}
171+
dfs(pos + 1, npp, np, ns, ti && d == h);
172+
c += cnt;
173+
s += sum + a * cnt;
174+
}
175+
if (!ti) {
176+
vis[pos][pp][pr][st] = 1;
177+
memoCnt[pos][pp][pr][st] = c;
178+
memoSum[pos][pp][pr][st] = s;
179+
}
180+
cnt = c;
181+
sum = s;
182+
}
183+
184+
static long long calc(long long N) {
185+
if (N < 0) return 0;
186+
len = 0;
187+
long long x = N;
188+
if (!x) {
189+
digits[len++] = 0;
190+
} else {
191+
char buf[20];
192+
int l = 0;
193+
while (x) {
194+
buf[l++] = x % 10;
195+
x /= 10;
196+
}
197+
for (int i = l - 1; i >= 0; i--)
198+
digits[len++] = buf[i];
199+
}
200+
memset(vis, 0, sizeof(vis));
201+
dfs(0, 10, 10, 0, 1);
202+
return sum;
203+
}
204+
205+
long long totalWaviness(long long a, long long b) {
206+
return calc(b) - calc(a - 1);
207+
}
208+
```
209+
133210
<!-- tabs:end -->
134211
135212
<!-- solution:end -->

solution/3700-3799/3753.Total Waviness of Numbers in Range II/README_EN.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,83 @@ Return the total sum of waviness for all numbers in the range <code>[num1, num2]
126126

127127
```
128128

129+
#### C
130+
131+
```c
132+
static int len, digits[20];
133+
static long long memoCnt[20][11][11][2];
134+
static long long memoSum[20][11][11][2];
135+
static char vis[20][11][11][2];
136+
static long long cnt, sum;
137+
138+
static void dfs(int pos, int pp, int pr, int st, int ti) {
139+
if (pos == len) {
140+
cnt = 1;
141+
sum = 0;
142+
return;
143+
}
144+
if (!ti && vis[pos][pp][pr][st]) {
145+
cnt = memoCnt[pos][pp][pr][st];
146+
sum = memoSum[pos][pp][pr][st];
147+
return;
148+
}
149+
int h = ti ? digits[pos] : 9;
150+
long long c = 0, s = 0;
151+
for (int d = 0; d <= h; d++) {
152+
int ns = st || d;
153+
long long a = 0;
154+
int npp, np;
155+
if (!ns) {
156+
npp = 10;
157+
np = 10;
158+
} else if (!st) {
159+
npp = 10;
160+
np = d;
161+
} else {
162+
if (pp != 10 && pr != 10 && ((pr > pp && pr > d) || (pr < pp && pr < d)))
163+
a = 1;
164+
npp = pr;
165+
np = d;
166+
}
167+
dfs(pos + 1, npp, np, ns, ti && d == h);
168+
c += cnt;
169+
s += sum + a * cnt;
170+
}
171+
if (!ti) {
172+
vis[pos][pp][pr][st] = 1;
173+
memoCnt[pos][pp][pr][st] = c;
174+
memoSum[pos][pp][pr][st] = s;
175+
}
176+
cnt = c;
177+
sum = s;
178+
}
179+
180+
static long long calc(long long N) {
181+
if (N < 0) return 0;
182+
len = 0;
183+
long long x = N;
184+
if (!x) {
185+
digits[len++] = 0;
186+
} else {
187+
char buf[20];
188+
int l = 0;
189+
while (x) {
190+
buf[l++] = x % 10;
191+
x /= 10;
192+
}
193+
for (int i = l - 1; i >= 0; i--)
194+
digits[len++] = buf[i];
195+
}
196+
memset(vis, 0, sizeof(vis));
197+
dfs(0, 10, 10, 0, 1);
198+
return sum;
199+
}
200+
201+
long long totalWaviness(long long a, long long b) {
202+
return calc(b) - calc(a - 1);
203+
}
204+
```
205+
129206
<!-- tabs:end -->
130207
131208
<!-- solution:end -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
static int len, digits[20];
2+
static long long memoCnt[20][11][11][2];
3+
static long long memoSum[20][11][11][2];
4+
static char vis[20][11][11][2];
5+
static long long cnt, sum;
6+
7+
static void dfs(int pos, int pp, int pr, int st, int ti) {
8+
if (pos == len) {
9+
cnt = 1;
10+
sum = 0;
11+
return;
12+
}
13+
if (!ti && vis[pos][pp][pr][st]) {
14+
cnt = memoCnt[pos][pp][pr][st];
15+
sum = memoSum[pos][pp][pr][st];
16+
return;
17+
}
18+
int h = ti ? digits[pos] : 9;
19+
long long c = 0, s = 0;
20+
for (int d = 0; d <= h; d++) {
21+
int ns = st || d;
22+
long long a = 0;
23+
int npp, np;
24+
if (!ns) {
25+
npp = 10;
26+
np = 10;
27+
} else if (!st) {
28+
npp = 10;
29+
np = d;
30+
} else {
31+
if (pp != 10 && pr != 10 && ((pr > pp && pr > d) || (pr < pp && pr < d)))
32+
a = 1;
33+
npp = pr;
34+
np = d;
35+
}
36+
dfs(pos + 1, npp, np, ns, ti && d == h);
37+
c += cnt;
38+
s += sum + a * cnt;
39+
}
40+
if (!ti) {
41+
vis[pos][pp][pr][st] = 1;
42+
memoCnt[pos][pp][pr][st] = c;
43+
memoSum[pos][pp][pr][st] = s;
44+
}
45+
cnt = c;
46+
sum = s;
47+
}
48+
49+
static long long calc(long long N) {
50+
if (N < 0) return 0;
51+
len = 0;
52+
long long x = N;
53+
if (!x) {
54+
digits[len++] = 0;
55+
} else {
56+
char buf[20];
57+
int l = 0;
58+
while (x) {
59+
buf[l++] = x % 10;
60+
x /= 10;
61+
}
62+
for (int i = l - 1; i >= 0; i--)
63+
digits[len++] = buf[i];
64+
}
65+
memset(vis, 0, sizeof(vis));
66+
dfs(0, 10, 10, 0, 1);
67+
return sum;
68+
}
69+
70+
long long totalWaviness(long long a, long long b) {
71+
return calc(b) - calc(a - 1);
72+
}

0 commit comments

Comments
 (0)