题目意思 1到N 不包含 13 作为字串并且 是13的倍数
#include<iostream> #include<stdio.h> #include<cstring> #include<algorithm> using namespace std; long long N,arr[32],dp[32][3][13]; long long DFS( int pos,int flag,int t,int limit ) { if( pos == -1 ) { if( flag == 2 && t == 0 )return 1; return 0; } if( !limit && dp[pos][flag][t] != -1 ) return dp[pos][flag][t]; int end = limit?arr[pos]:9;long long ans = 0; for( int i = 0; i <= end; i++ ) { int tab = flag; if( tab == 0 && i == 1 )tab = 1; if( tab == 1 && i == 3 )tab = 2; if( tab == 1 && i == 1 )tab = 1; if( tab == 1 && i != 1 && i != 3 )tab = 0; int num = (t*10+i)%13; ans += DFS( pos-1,tab,num,limit&&(i==arr[pos]) ); } if( !limit )dp[pos][flag][t] = ans; return ans; } long long work( long long num ) { int k = 0; while( num ) { arr[k++] = num%10; num /= 10; } memset( dp,-1,sizeof(dp) ); return DFS( k-1,0,0,1 ); } int main( ) { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); while( scanf("%I64d",&N) != EOF ) printf("%I64d\n",work(N)); return 0; }