FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7629 Accepted Submission(s): 3158 Problem Description FatMouse has stored some cheese in a city. The city can be
数位DP 用的是之前的模板。 之前写过的汇总:
http://blog.csdn.net/techmonster/article/details/50284375 模板: (强调一点,我的数位DP模板是结合记忆话搜索优化的,博客中之前写过的数位DP题,题意:有一个n*n的方格,每个格子有一个价值,经过这个格子就可以得到这个价值,每次可以横着或者竖着一步最多走K格,并且每次走的格子要比上一个格子的价值大,问现在从位置(1,1)走,最大可以获得的价值. 分析: 记忆化搜素,dp[i][j]表示的是从位置(i,j)开都是用这个模板)
int dfs(int top, int s, bool e)
{
if (top==0) return s==target_s;
if (!e && dp[top][s]!=-1) return dp[top][s];
int i,res = 0;
int u = e?num[top]:9;
for (i = 0; i <= u; ++i)
res += dfs(top-1, new_s(s, i), e&&i==u);
return e?res:dp[top][s]=res;
}
dp[i][j][k][l] i表示第几位数,j表示各位数总和,k表示余数,l 表示除多少
题目代码:
#include
#include
#include
#include
#include
#include
#include
#define MS(x,y) memset(x,y,sizeof(x))
using namespace std;
const int MAXN = 130;
//const int N = 5010;
const double eps = 1e-10;
void fre(){freopen("t.txt","r",stdin);}
typedef long long LL;
typedef unsigned long long ULL;
int len,dp[10][82][82][82],num[15];
void getnum(int x)
{
len = 0;
while(x)
{
num[++len] = x%10;
x/=10;
}
}
int dfs(int top, int sum, int mo, int div,bool e)
{
if (top==0) return mo==0 && sum == div;
if (!e && dp[top][sum][mo][div]!=-1) return dp[top][sum][mo][div];
int i,res = 0,nsum,ndiv,nmo;
int u = e?num[top]:9;
for (i = 0; i <= u; ++i)
{
nsum = sum+i;
res += dfs(top-1, nsum, (mo*10+i)%div, div, e&&i==u);
}
return e?res:dp[top][sum][mo][div]=res;
}
int main()
{
// fre();
int T,a,b,i,t = 0;
scanf("%d",&T);MS(dp,-1);
while(T--)
{
scanf("%d%d",&a,&b);
int tem = 0;
getnum(b);
for(i = 1; i < 82; ++i) tem +=dfs(len,0,0,i,1);
getnum(a-1);
for(i = 1; i < 82; ++i) tem -=dfs(len,0,0,i,1);
printf("Case %d: %d\n",++t,tem);
}
}