uva11361 Investigating Div-Sum Property

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 The rst line of input is an integer T ( T< 100) that indicates
the number of test cases. Each case is a line containing 3 positive
integers A , B and K . 1 A B< 2 31 and 0 < K <
10000. Output For each case, output the number of integers in the range [ A;B ] which is divisible by K and the sum of its digits is
also divisible by K .

数位dp,记录当前数模k的余数和数字和模k的余数。
注意到总共只有十位数,数字之和不会很大。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define DP dp[p][m1][m2]
int dp[15][110][110],mod,l,a[15];
int dfs(int p,int m1,int m2,bool less)
{
    int r,k,ret=0;
    if (less&&DP>=0) return DP;
    if (p==0) return m1==0&&m2==0;
    r=less?9:a[p];
    for (k=0;k<=r;k++)
        ret+=dfs(p-1,(m1*10+k)%mod,(m2+k)%mod,less||k<r);
    if (less) DP=ret;
    return ret;
}
int solve(int x)
{
    l=0;
    while (x)
    {
        a[++l]=x%10;
        x/=10;
    }
    memset(dp,0xff,sizeof(dp));
    return dfs(l,0,0,0);
}
int main()
{
    int x,y,T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d%d",&x,&y,&mod);
        if (mod>100)
        {
            printf("0\n");
            continue;
        }
        printf("%d\n",solve(y)-solve(x-1));
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值