标签:mamicode
Sample Output
Case 1: 10
Case 2: 3
题意:求区间[l,r] 满足x%f[x]==0的数的个数!!
比较经典的数位动规!
纯枚举肯定会超时!!!
所以这里是通过枚举位数和求的!!!
AC代码如下:
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int num[22]; int dp[11][82][82][82]; int mo; int dfs(int pos,int mo,int sum1,int sum2,bool limit) { int i; if(pos==-1) return sum1==mo&&sum2==0; if(!limit&&dp[pos][mo][sum1][sum2]!=-1) return dp[pos][mo][sum1][sum2]; int end = limit ? num[pos] : 9; int sum = 0; for(i = 0; i <= end; i++) { sum+=dfs(pos-1,mo,sum1+i,(sum2*10+i)%mo,limit&&i==end); } return limit ? sum : dp[pos][mo][sum1][sum2] = sum; } int solve (int n) { int pos=0; int ans=0; int i; while(n>0) { num[pos++]=n%10; n/=10; } for(i=1;i<=81;i++) { ans+=dfs(pos-1,i,0,0,true); } return ans; } int main() { int t; int l,r; int cas=1; scanf("%d",&t); memset(dp,-1,sizeof dp); while(t--) { scanf("%d%d",&l,&r); printf("Case %d: %d\n",cas++,solve(r)-solve(l-1)); } return 0; }
标签:mamicode
原文地址:http://blog.csdn.net/hanhai768/article/details/38081425