码迷,mamicode.com
首页 > Windows程序 > 详细

[SCOI 2009]windy数

时间:2018-02-25 17:20:11      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:math   tin   +=   sdi   des   work   putc   www.   amp   

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 <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#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 = (x<<1)+(x<<3)+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 <= j-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 < a[tot]; i++) ans += f[tot][i];
    for (int i = tot-1, last = a[tot]; i >= 1; i--) {
    for (int j = 0; j < a[i]; 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;
}

[SCOI 2009]windy数

标签:math   tin   +=   sdi   des   work   putc   www.   amp   

原文地址:https://www.cnblogs.com/NaVi-Awson/p/8469409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!