U

来源:互联网 发布:淘宝刷单工作室怎么开 编辑:程序博客网 时间:2024/04/29 20:57

题目链接:https://cn.vjudge.net/contest/194559#problem/U

An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible by 3 and 12 (3+7+0+2) is also divisible by 3. This property also holds for the integer 9.

In this problem, we will investigate this property for other integers.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains three positive integers A, B and K (1 ≤ A ≤ B < 231 and 0 < K < 10000).

Output

For each case, output the case number and the number of integers in the range [A, B] which are divisible by K and the sum of its digits is also divisible by K.

Sample Input

3

1 20 1

1 20 2

1 1000 4

Sample Output

Case 1: 20

Case 2: 5

Case 3: 64

大意:

每个位的总和能整除k,并且这个数能整除k的个数

分析+代码:

/*
此题属于正常的题。
但是容易超时的地方在于数据量大就想到用longlong,实际上可以清楚地知道,如果
2的31次方小于4000000000,十位数数,假设十位数都为9,那么最大为90。
也就是说k大于90的话,就没意思了,怎样都不会被整除的
*/




#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;




int dig[20];
int dp[20][100][100];




int dfs(int pos,int sum,int dangqian,int k,int limit)
{
    if(pos==0)
    {
        if(sum%k==0&&dangqian%k==0)
        return 1;
        else
        return 0;
    }
    if(dp[pos][sum][dangqian]!=-1&&!limit)
        return dp[pos][sum][dangqian];
        int up=limit?dig[pos]:9;
        ll ans=0;
    for(int i=0;i<=up;i++)
    {
        ans+=dfs(pos-1,(sum*10+i)%k,i+dangqian,k,limit&&i==up);
    }
    if(!limit)
    dp[pos][sum][dangqian]=ans;
    return ans;
}


int solve(int x,int k)
{
    if(k>=90)
        return 0;


    int id=0;
    while(x)
    {
        dig[++id]=x%10;
        x/=10;
    }
    return dfs(id,0,0,k,1);
}






int main()
{
    int T;
    cin>>T;
    ll a,b;
    int k;
    int p=1;
  //  memset(dp,-1,sizeof(dp));
    while(T--)
    {
        memset(dp,-1,sizeof(dp));
        cin>>a>>b>>k;


        cout<<"Case "<<p<<":"<<" "<<solve(b,k)-solve(a-1,k)<<endl;
        p++;
    }
    return 0;


}

原创粉丝点击