[hdu 3652]數位dp解決數的倍數問題
阿新 • • 發佈:2017-08-01
余數 spa code turn 3的倍數 printf span hdu mes
原以為很好的理解了數位dp,結果遇到一個新的問題還是不會分析,真的是要多積累啊。
解決13的倍數,可以根據當前余數來推,所以把當前余數記為一個狀態就可以了。
#include<bits/stdc++.h> using namespace std; int dp[20][13][2][10]; int b[20]; int dfs(int pos,int preok,int rem,int th,int pre) { if (pos==-1) { if (rem==0&&th==1) return 1; else return0; } if (preok&&dp[pos][rem][th][pre]!=-1) return dp[pos][rem][th][pre]; int up=preok?9:b[pos]; int ans=0; for (int i=0;i<=up;i++) { ans+=dfs(pos-1,i<b[pos]||preok,(rem*10+i)%13,pre==1&&i==3||th,i); } if (preok) dp[pos][rem][th][pre]=ans;return ans; } int solve(int n) { int cnt=0; do { b[cnt++]=n%10; n/=10; }while (n); return dfs(cnt-1,0,0,0,0); } int main() { memset(dp,-1,sizeof(dp)); int n; while (~scanf("%d",&n)) { printf("%d\n",solve(n)); } return 0; }
[hdu 3652]數位dp解決數的倍數問題