Good Numbers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4449 Accepted Submission(s): 1445
Problem Description
If we sum up every digit of a number and the result can be exactly divided by 10, we say this number is a good number.
You are required to count the number of good numbers in the range from A to B, inclusive.
You are required to count the number of good numbers in the range from A to B, inclusive.
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
Each test case comes with a single line with two numbers A and B (0 <= A <= B <= 10 18).
Output
For test case X, output "Case #X: " first, then output the number of good numbers in a single line.
Sample Input
2 1 10 1 20
Sample Output
Case #1: 0 Case #2: 1
题意很简单,就是输入两个数a,b,输出a与b之间各位数之和能够整除10的数字的个数。(然而自己看了一遍题解...
显然,数据范围这么大,不能暴力找,只能用数位dp搞搞
想清楚开数位dp所需要的维度,我们需要一个维度来存位数,一个维度来存%10的数(本人YY了一下...如何稍微变形一下倒可以考虑状压来搞)
OK,我们开个64位的二维dp数组,dp[i][j],i存第几位,j存%10的数
首先,想清楚,怎么推出各个阶段最优解。
我们考虑,如果第i位,%10为j的数的个数为dp[i][j],那么我们要求i+1位时,必须考虑两种情况:①i+1位前面的数(0~i位组成的数)小于所要求的某个数的前i位的数(0~i位组成的数),则这个i+1位可以在0~9之间任取,则第i+1位的情况可由第i位的情况推出,即dp[i+1][j+k]+=dp[i][j](0<=k<=9);②i+1位前面的数(0~i位组成的数)等于所要求的某个数的前i位的数(0~i位组成的数),则这个i+1位可以在0~lowbit[i]-1之间任取(这里的lowbit数组存的是该数字的第i位),而且这种情况是前面没有被考虑的,所以dp[i][(j+res)%10]++(0<=j<=lowbit[i]-1,这里的res是该数前面的位数对10取模,至于为什么,因为我们这里考虑的情况是前面i位和该数的前i位重合的情况,读者应该很容易想清楚)。OK,这样就能搞定这题(然而,自己想了一晚上....这智商吃枣药丸。。。)
最好考虑一下res==0,则dp[0][0]++;(写代码时YY了一下,若这题稍微加点难度,搞搞高精度,应该也是可以的= =)
AC代码:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#define eps 1e-8
#define zero(x) (((x>0?(x):-(x))-eps)
#define mem(a,b) memset(a,b,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a))
#define pfn printf("\n")
#define ll __int64
#define ull unsigned long long
#define sf(a) scanf("%d",&a)
#define sf64(a) scanf("%I64d",&a)
#define sf264(a,b) scanf("%I64d%I64d",&a,&b)
#define sf364(a,b,c) scanf("%I64d%I64d%I64d",&a,&b,&c)
#define sf2(a,b) scanf("%d%d",&a,&b)
#define sf3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf4(a,b,c,d) scanf("%d%d%d%d",&a,&b,&c,&d)
#define sff(a) scanf("%f",&a)
#define sfs(a) scanf("%s",a)
#define sfs2(a,b) scanf("%s%s",a,b)
#define sfs3(a,b,c) scanf("%s%s%s",a,b,c)
#define sfd(a) scanf("%lf",&a)
#define sfd2(a,b) scanf("%lf%lf",&a,&b)
#define sfd3(a,b,c) scanf("%lf%lf%lf",&a,&b,&c)
#define sfd4(a,b,c,d) scanf("%lf%lf%lf%lf",&a,&b,&c,&d)
#define sfc(a) scanf("%c",&a)
#define ull unsigned long long
#define pp pair<int,int>
#define debug printf("***\n")
const double PI = acos(-1.0);
const double e = exp(1.0);
const int INF = 0x7fffffff;;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<class T> inline T Min(T a, T b) { return a < b ? a : b; }
template<class T> inline T Max(T a, T b) { return a > b ? a : b; }
bool cmpbig(int a, int b){ return a>b; }
bool cmpsmall(int a, int b){ return a<b; }
using namespace std;
ll dp[20][10]; //存第i个位置时%10的值
ll solve(ll num)
{
int k=0;
mem(dp,0);
mem(dp,0);
// for(int j=0;j<10;j++)
// dp[k][j]=1;
int res=0;
for(int i=k-1;i>=0;i--)
{
for(int j=0;j<10;j++) //
for(int z=0;z<10;z++)
dp[i][(j+z)%10]+=dp[i+1][j];
for(int j=0;j<lowbit[i]-'0';j++)
dp[i][(j+res)%10]++;
res=(res+lowbit[i])%10;
}
if(!res) dp[0][0]++;
return dp[0][0];
}
int main()
{
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
int n,tot=1;
sf(n);
while(n--)
{
ll l,r;
sf264(l,r);
printf("Case #%d:%I64d\n",tot++,solve(r)-solve(l-1));
}
return 0;
}