DP [Digital] [P2657] [SCOI2009] windy Number

Description

windy defines a number windy. Without leading zeros and adjacent to at least the difference between two numbers \ (2 \) is the number of positive integers is called windy. windy want to know,

In \ (A \) and \ (B \) between, including \ (A \) and \ (B \) , total number of windy number?

Limitation

\ (1 \ leq A \ leq B \ leq 2000000000 \)

Solution

The day before yesterday to rewrite the title, for a better way to write the DP, write it down here.

Due consideration leading \ (0 \) and the top of the circles are most likely to have \ (1 \) in the program, and therefore directly record to a bool variable.

Set \ (f_ {i, j} \) before considering \ (i \) position, the \ (i \) bit is \ (j \) and not on top of the circles of the number of programs, such transfer is very good write a. ,

Code

#include <cmath>
#include <cstdio>
#include <cstring>

const int maxn = 100;

int x, y;
int A[maxn], B[maxn];
ll frog[maxn][10];

int ReadNum(int *p);
ll calc(const int *const num, const int n);

int main() {
  freopen("1.in", "r", stdin);
  int x = ReadNum(A); y = ReadNum(B);
  for (int i = x - 1; ~i; --i) {
    if ((--A[i]) >= 0) {
      break;
    } else {
      A[i] = 9;
    }
  }
  if (A[x] == 0) { --x; }
  qw(calc(B, y) - calc(A, x), '\n', true);
  return 0;
}

int ReadNum(int *p) {
  auto beg = p;
  do *p = IPT::GetChar() - '0'; while ((*p < 0) || (*p > 9));
  do *(++p) = IPT::GetChar() - '0'; while ((*p >= 0) && (*p <= 9));
  return p - beg;
}

ll calc(const int *const num, const int n) {
  if (n <= 1) {
    return num[0];
  }
  memset(frog, 0, sizeof frog);
  bool upc = true;
  for (int i = 1; i < num[0]; ++i) {
    frog[0][i] = 1;
  }
  for (int i = 1; i < n; ++i) {
    int di = i - 1;
    for (int j = 0; j < 10; ++j) {
      for (int k = 0; k < 10; ++k) if (abs(j - k) >= 2) {
        frog[i][j] += frog[di][k];
      }
      ++frog[i][j];
    }
    --frog[i][0];
    if (upc) {
      for (int k = 0; k < num[i]; ++k) if (abs(num[di] - k) >= 2) {
        ++frog[i][k];
      }
      if (abs(num[di] - num[i]) < 2) {
        upc = false;
      }
    }
  }
  ll _ret = 0;
  for (int i = 0, dn = n - 1; i < 10; ++i) {
    _ret += frog[dn][i];
  }
  return _ret + upc;
}

Guess you like

Origin www.cnblogs.com/yifusuyi/p/11389000.html