[SCOI 2009]windy数

2021-02-12 19:15

阅读:353

Description

题库链接

找出在 \([A,B]\) 间满足相邻位差值至少为 \(2\) 的正整数个数。

\(1\leq A,B\leq 2\cdot 10^9\)

Solution

数位 \(DP\)

还是按照套路 \(f_{i,j}\)\(i\) 位数,第 \(1\) 位为 \(j\) 的满足条件的个数。

然后计算的时候要注意若前面的位数已经不满足了,就直接退出。

Code

//It is made by Awson on 2018.2.25
#include 
#define LL long long
#define dob complex
#define Abs(a) ((a) 
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) 
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
void read(LL &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x1)+(x3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(LL x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(LL x) {if (x 0) putchar('-'); print(Abs(x)); }

LL f[20][10], a, b;

void pre() {
    for (int i = 0; i 9; i++) f[1][i] = 1;
    for (int i = 2; i 10; i++) {
    for (int j = 0; j 9; j++) {
        for (int k = 0; k -2; k++) f[i][j] += f[i-1][k];
        for (int k = j+2; k 9; k++) f[i][j] += f[i-1][k];
    }
    }
}
LL get(int x) {
    int a[20], tot = 0; LL ans = 0;
    while (x) a[++tot] =x%10, x /= 10;
    for (int i = tot-1; i >= 1; i--) for (int j = 1; j 9; j++) ans += f[i][j];
    for (int i = 1; i for (int i = tot-1, last = a[tot]; i >= 1; i--) {
    for (int j = 0; j if (Abs(j-last) 2) continue;
        ans += f[i][j];
    }
    last = a[i]; if (Abs(a[i+1]-a[i]) 2) break;
    }
    return ans;
}
void work() {
    pre(); read(a), read(b); writeln(get(b+1)-get(a));
}
int main() {
    work(); return 0;
}

上一篇:JS学习第七天

下一篇:(6)C#事务处理


评论


亲,登录后才可以留言!