bzoj 1026: [SCOI2009]windy数

D(不)P(会)类

 1 #include<bits/stdc++.h>
 2 #define N 200005
 3 #define LL long long
 4 #define inf 0x3f3f3f3f
 5 #define ls tr[x][0]
 6 #define rs tr[x][1]
 7 using namespace std;
 8 inline int ra()
 9 {
10     int x=0,f=1; char ch=getchar();
11     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
12     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
13     return x*f;
14 }
15 int f[12][10];
16 int dp[12];
17 void init()
18 {
19     memset(f,0,sizeof(f));
20     for (int i=0; i<10; i++) f[1][i]=1;
21     for (int i=2; i<=10; i++)
22     {
23         for (int j=0; j<10; j++)
24         {
25             for (int k=0; k<10; k++)
26             {
27                 if (abs(j-k)>=2) 
28                     f[i][j]+=f[i-1][k];
29             }
30         }
31     }
32 }
33 int len;
34 int solve(int a)
35 {
36     len=0;
37     while (a)
38     {
39         dp[len++]=a%10;
40         a/=10;
41     } 
42     int ans=0;
43     for (int i=1; i<len; i++)
44         for (int j=1; j<10; j++)
45             ans+=f[i][j];
46     for (int i=1; i<dp[len-1]; i++)
47         ans+=f[len][i];
48     for (int i=len-1; i>=1; i--)
49     {
50         for (int j=0; j<dp[i-1]; j++)
51             if (abs(j-dp[i])>=2)
52                 ans+=f[i][j];
53         if (abs(dp[i]-dp[i-1])<2) break;
54     }
55     return ans;
56 }
57 int main()
58 {
59     init(); int a,b;
60     while (~scanf("%d%d",&a,&b))
61     {
62         cout<<solve(b+1)-solve(a);
63     }
64     return 0;
65 }

 

posted @ 2017-03-01 08:06  ws_ccd  阅读(...)  评论(... 编辑 收藏