Time Limit:1000MS | Memory Limit:32768KB | 64bit IO Format:%I64d & %I64u |
Description
A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
Input
Process till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
Output
Print each answer in a single line.
Sample Input
13 100 200 1000
求a到b区间各位和能整除10的个数,简单数位dp取mod==0的部分。
代码:
/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-7 13:48:09
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef __int64 ll;
ll dp[22][12],num[22];
ll dfs(ll pos,ll mod,bool flag){
if(pos==0)return mod==0;
if(flag&&dp[pos][mod]!=-1)return dp[pos][mod];
ll u=flag?9:num[pos];
ll ans=0;
for(ll d=0;d<=u;d++)
ans+=dfs(pos-1,(mod+d)%10,flag||d<u);
if(flag)dp[pos][mod]=ans;
return ans;
}
ll solve(ll x){
ll len=0;
while(x){
num[++len]=x%10;
x/=10;
}
return dfs(len,0,0);
}
int main()
{
ll i,j,k,m,a,b,T,t;
memset(dp,-1,sizeof(dp));
scanf("%I64d",&T);
for(t=1;t<=T;t++)
{
scanf("%I64d%I64d",&a,&b);
printf("Case #%I64d: ",t);
if(a==0)printf("%I64d\n",solve(b));
else printf("%I64d\n",solve(b)-solve(a-1));
}
return 0;
}