windy数
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
windy定义了一种windy数。
不含前导零且相邻两个数字之差至少为
windy想知道,在
Input
包含两个整数,
满足
Output
Sample input and output
Sample Input | Sample Output |
---|---|
1 10 |
9 |
Source
Windy
简单数位dp,注意前导0的情况即可。
# include <stdio.h> # include <stdlib.h> # include <string.h> int a[11], dp[11][10]; int dfs(int pos, int pre, bool sta, int limit) { if(pos == -1) return 1; if(!limit && !sta&& dp[pos][pre] != -1) return dp[pos][pre]; int up = limit?a[pos]:9; int ans = 0; for(int i=0; i<=up; ++i) { if(sta) ans += dfs(pos-1, i, i==0, limit&&i==a[pos]); else if(abs(pre-i)>=2) ans += dfs(pos-1, i, false, limit&&i==a[pos]); } if(!limit && !sta) dp[pos][pre] = ans; return ans; } int solve(int num) { int pos = 0; while(num) { a[pos++] = num%10; num /= 10; } return dfs(pos-1, 0, true, true); } int main() { memset(dp, -1, sizeof(dp)); int n, m; while(~scanf("%d%d",&n,&m)) printf("%d\n",solve(m)-solve(n-1)); return 0; }