G - B-number

 1 #include<stdio.h>
 2 #include<string.h>
 3 using namespace std;
 4 typedef long long ll;
 5 
 6 int a[20];
 7 int dp[20][15][3];
 8 
 9 //pos:当前位置
10 //mod:余数
11 //have :0:前一位不是1 1:前一位是1 2:有13  
12 ll dfs(int pos,int mod,int have,bool limit){
13     if(pos==-1) return mod == 0 && have == 2;
14     if(!limit && dp[pos][mod][have] != -1) 
15         return dp[pos][mod][have];//没有上限并且已被访问过
16     int up = limit?a[pos]:9;
17     ll ans = 0,i;
18     for(i = 0;i <= up;i++){
19        int modx = (mod*10+i)%13;
20        int havex = have;
21        if(have == 0 && i==1) havex=1;
22        if(have == 1 && i!=1) havex=0;
23        if(have == 1 && i == 3) havex=2;
24        ans += dfs(pos-1, modx, havex, limit && i == a[pos]);
25     }
26     if(!limit) dp[pos][mod][have] = ans;
27     return ans;
28 }
29 
30 ll solve(ll x){
31     int pos=0;
32     while(x){
33         a[pos++]=x%10;
34         x/=10;
35     }
36     return dfs(pos-1,0,0,true);
37 }
38 int main(){
39     ll n;
40     memset(dp,-1,sizeof(dp));
41     while(scanf("%lld",&n)!=EOF){
42         ll ans = solve(n);
43         printf("%lld\n",ans;
44     }
45     return 0;
46 }

 

posted @ 2018-06-02 18:16  ouyang_wsgwz  阅读(200)  评论(0编辑  收藏  举报