标签:set font padding std and ble numbers otto line
总算用记忆化搜索搞定了一个难一点的数位dp了!。
AC代码例如以下:
///记忆化搜素 500MS 272K #include<iostream> #include<cstdio> #include<cstring> #define mod 13 using namespace std; int dp[20][15][4]; int num[20]; int dfs(int pos,int mo,int status,bool limit) { int i; //cout<<pos<<"~~~~~~~"<<mo<<"~~~~~"<<status<<"~~~~~~~~"<<limit<<"~~~~~~~"<<dp[pos][status]<<endl; if(!pos) return status==2&&mo==0; if(!limit&&dp[pos][mo][status]!=0) return dp[pos][mo][status]; int end = limit ?num[pos] : 9; int sum=0; for(i=0;i<=end;i++) { int a=mo; int flag = status ; if(flag==0&&i==1) flag=1; if(flag==1&&i==3) flag=2; if(flag==1&&i!=1&&i!=3) flag=0; sum+=dfs(pos-1,(a*10+i)%mod,flag,limit&&i==end); } //cout<<"!!!!!!!!!!!"<<sum<<"!!!!!!!!"<<endl; return limit ?
sum : dp[pos][mo][status] = sum; } int _13(int n) { int pos=1; memset(dp,0,sizeof dp); while (n>0) { num[pos++]=n%10; n/=10; } //cout<<"~~~~~~~~~"<<pos-1<<"~~~~~~~~"<<endl; return dfs(pos-1,0,0,true); } int main() { int i,j; int n; while(~scanf("%d",&n)) { printf("%d\n",_13(n)); } return 0; }
标签:set font padding std and ble numbers otto line
原文地址:http://www.cnblogs.com/ljbguanli/p/6905401.html