HDU 5787 K-wolf Number
思路:
数位dp
#include<bits/stdc++.h> using namespace std; #define LL long long #define pb push_back #define mem(a, b) memset(a, b, sizeof(a)) LL dp[25][12][12][12][12]; int a[25]; int cnt = 0, k; LL dfs(int pos, int a1, int a2, int a3, int a4, bool limit, bool zero) { if(pos == -1) { return 1; } if(!limit) { if(k == 2) { if(~dp[pos][a4][0][0][0]) return dp[pos][a4][0][0][0]; } else if(k == 3) { if(~dp[pos][a3][a4][0][0]) return dp[pos][a3][a4][0][0]; } else if(k == 4) { if(~dp[pos][a2][a3][a4][0]) return dp[pos][a2][a3][a4][0]; } else if(k == 5) { if(~dp[pos][a1][a2][a3][a4]) return dp[pos][a1][a2][a3][a4]; } } int up = 9; if(limit) up = a[pos]; LL ans = 0; for (int i = 0; i <= up; i++) { if(k == 2) { if(i != a4) { if(zero&&i==0)ans += dfs(pos-1, a2, a3, a4, 11, limit&&i==up, zero&&i==0); else ans += dfs(pos-1, a2, a3, a4, i, limit&&i==up, zero&&i==0); } } else if(k == 3) { if(i != a3 && i != a4) { if(zero&&i==0)ans += dfs(pos-1, a2, a3, a4, 11, limit&&i==up, zero&&i==0); else ans += dfs(pos-1, a2, a3, a4, i, limit&&i==up, zero&&i==0); } } else if(k == 4) { if(i != a2 && i != a3 && i != a4) { if(zero&&i==0)ans += dfs(pos-1, a2, a3, a4, 11, limit&&i==up, zero&&i==0); else ans += dfs(pos-1, a2, a3, a4, i, limit&&i==up, zero&&i==0); } } else if(k == 5) { if(i != a1 && i != a2 && i != a3 && i != a4) { if(zero&&i==0)ans += dfs(pos-1, a2, a3, a4, 11, limit&&i==up, zero&&i==0); else ans += dfs(pos-1, a2, a3, a4, i, limit&&i==up, zero&&i==0); } } } if(!limit) { if(k == 2) { dp[pos][a4][0][0][0] = ans; } else if(k == 3) { dp[pos][a3][a4][0][0] = ans; } else if(k == 4) { dp[pos][a2][a3][a4][0] = ans; } else if(k == 5) { dp[pos][a1][a2][a3][a4] = ans; } } return ans; } LL solve(LL n) { if(n == 0) return 1; cnt = 0; mem(dp, -1); while(n) a[cnt++] = n%10, n /= 10; return dfs(cnt-1, 11, 11, 11, 11, 1, 1); } int main(){ LL L, R; while(~ scanf("%lld %lld %d", &L, &R, &k)){ printf("%lld\n", solve(R) - solve(L-1)); } return 0; }