Dp Digit

Правка en1, от theoneandonlytron, 2026-10-01 06:49:13

This blog is my personal reflection on what I have learned in Dp Digit

Theory: + Dp digit template is something likethis void dfs(pos, tight, [some conditions], target) if pos == target: return something limit = if tight = true — > target[pos] else 9 (may vary depending on problem) for i : 0 — > limit: update_tight() answ += do something() dp[pos][tight][some condition] = answ return answ

Trick: 1. Answ(L, R) = Answ(0,R) — Answ(0, L)

  • Ideas:
  • https://codeforces.me/gym/100886/problem/G
  • this problem mentioned max, which mean you can't use trick 1
  • so we must keep a low_tight for keeping the number >= L, and a high_tight to keep a number <= R
  • and then you just keep max number for each state simple
  • Be careful on the limit because you might be off by one
  • https://www.spoj.com/problems/PR003004/en/

  • This problem is a little bit complicated, because we must store (current sum, total number)
  • Formula : answ.sum += i * total_number + current_sum
  • answ.number += total_number
  • https://vjudge.net/problem/CodeForces-1036C/origin

  • Chill problem standard easy
  • https://vjudge.net/problem/CodeForces-628D/origin

  • Problem require 2 requirment:
  • A mutiple of M
  • appear D ONLY ON even position
  • for the mod requirement just store mod of M very simple

  • The even requirement is a bit hard but it is possible, just store the current parity of the position (stays odd until started)
  • vjudge.net/problem/CSES-2220/origin

  • standard no comment
  • https://www.codechef.com/problems/DGTCNT

  • Not really dp digit because contain a lot of combinatorics but it contain dp digit idea
  • so first we have a bunch of requiremnt, which require cnt[0] == a0 OR cnt[1] == a1 OR ... cnt[9] == a[9]
  • This is clear inclusion exclusion. So we must run through every mask (complexity 2^10)
  • the idea for this problem is to walk on tight
  • the moment tight turn off it never turns on again which mean the moment the tight turn off we use combinatorics to count (ways to shuffle the numbers in mask * number of ways to shuffle the number not in mask)
  • Precaculate Combinatoric also be careful with 0 it will mess your program up.

  • https://vjudge.net/problem/CodeForces-1073E

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский theoneandonlytron 2026-10-01 07:32:32 114
en5 Английский theoneandonlytron 2026-10-01 07:30:47 69
en4 Английский theoneandonlytron 2026-10-01 07:13:01 51
en3 Английский theoneandonlytron 2026-10-01 07:12:15 6 Tiny change: '\n```cpp\nvoid dfs(pos, ' -> '\n```cpp\nll dfs(pos, '
en2 Английский theoneandonlytron 2026-10-01 07:05:33 3046 (published)
en1 Английский theoneandonlytron 2026-10-01 06:49:13 2346 Initial revision (saved to drafts)