Description
Question Bank link
Find out the number of positive integers in \ ([a,b]\) that satisfy the adjacent difference value of at least \ (2\) .
\ (1\leq a,b\leq 2\cdot 10^9\)
Solution
Digits \ (dp\) .
Or, according to the formula \ (f_{i,j}\) is the \ (i\) bit, the number of the first \ (1\) bit is \ (j\) satisfies the condition.
Then calculate the time to pay attention to if the previous number of digits is not satisfied, the direct exit.
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 namespaceStdvoidRead (LL &x) {CharChBOOLFlag =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;}voidPrint (LL x) {if(X >9) Print (x/Ten); Putchar (%10+48); }voidWrite (LL x) {if(X <0) Putchar ('-'); Print (Abs (x)); }ll f[ -][Ten], A, b;voidPre () { for(inti =0; I <=9; i++) f[1][i] =1; for(inti =2; I <=Ten; i++) { for(intj =0; J <=9; J + +) { for(intK =0; K <= J-2; k++) f[i][j] + = F[i-1][K]; for(intK = J+2; K <=9; k++) f[i][j] + = F[i-1][K]; }}}ll Get (intx) {inta[ -], tot =0; LL ans =0; while(x) A[++tot] =x%Ten, x/=Ten; for(inti = Tot-1; I >=1; i--) for(intj =1; J <=9; J + +) ans + = f[i][j]; for(inti =1; i < A[tot]; i++) ans + = f[tot][i]; for(inti = Tot-1, last = A[tot]; I >=1; i--) { for(intj =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; }returnAns;}voidWork () {pre (); Read (a), read (b); Writeln (Get (b+1)-get (a));}intMain () {work ();return 0;}
[Scoi 2009]windy number