13 100 200 1000
1 1 2 2
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <cmath> #include <algorithm> using namespace std; const double eps = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; #define ll long long #define CL(a) memset(a,0,sizeof(a)) int dp[15][15][3],s[15];//dp[i][j][k],i表示位数,j表示余数,k表示末尾是1、末尾不是1、含有13. int dfs(int pos, int mod, int have, int lim)//前三个数对应数组dp,lim表示上限 { int num,ans,mod_x,have_x; if (pos <= 0) return mod==0&&have==2; if (!lim && dp[pos][mod][have]!=-1) //没有上限且被访问过 return dp[pos][mod][have]; ans = 0; num = lim?s[pos]:9;//如果有上限,只能取到当前位数,如果没上限,可取到9 //假设该位是2,下一位是3,如果现在算到该位为1,那么下一位是能取到9的,如果该位为2,下一位只能取到3 for (int i=0; i<=num; i++) { mod_x = (mod*10+i)%13;//该位的每种情况对13取模 have_x = have; if (have==0 && i==1)//末尾加1 have_x=1; if (have==1 && i!=1)//末尾已经为1了 have_x=0; if (have==1 && i==3)//末尾是1,现在加3 have_x=2; ans+=dfs(pos-1, mod_x, have_x, lim&&i==num);//如果i==num,下一位能取的最大数就为s[pos-1],i!=num,下一位能取到9 } if (!lim) dp[pos][mod][have] = ans; return ans; } int main () { int n,t; while (scanf ("%d",&n)==1) { CL(s); memset(dp, -1, sizeof(dp)); t = 0; while (n) { s[++t]=n%10; n/=10; } cout<<dfs(t, 0, 0, 1)<<endl; } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/d_x_d/article/details/48312945