1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include<bits/stdc++.h> using namespace std; #define ll long long ll dp[20][20][3]; int a[20]; ll dfs(int pos,bool is1,int remain,bool limit,int is13) { if(pos==-1) { return !remain&&is13==2; } if(!limit&&dp[pos][remain][is13]!=-1)return dp[pos][remain][is13]; int up = limit?a[pos]:9; ll temp = 0; for(int i=0;i<=up;i++) { int ok13 = is13; if(is13==0&&i==1)ok13=1; if(is13==1&&i!=1)ok13=0; if(is13==1&&i==3)ok13=2; temp+=dfs(pos-1,i==1,(remain*10+i)%13,limit&&i==up,ok13); } return limit?temp:dp[pos][remain][is13]=temp; } ll solve(ll x) { int p = 0; ll ans = 0; while(x) { a[p++]=x%10; x/=10; } ans+=dfs(p-1,0,0,1,0); return ans; } int main() { ll y; memset(dp,-1,sizeof(dp)); while(cin>>y) { printf("%lld\n",solve(y)); } }
|