HDOJ 4389 X mod f(x)

数位DP . dp[][][][mod] 然后枚举总和1~81

X mod f(x)

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2426 Accepted Submission(s): 954

Problem Description
Here is a function f(x):
   int f ( int x ) {
    if ( x == 0 ) return 0;
    return f ( x / 10 ) + x % 10;
   }

   Now, you want to know, in a given interval [A, B] (1 <= A <= B <= 109), how many integer x that mod f(x) equal to 0.

Input
   The first line has an integer T (1 <= T <= 50), indicate the number of test cases.
   Each test case has two integers A, B.

Output
   For each test case, output only one line containing the case number and an integer indicated the number of x.

Sample Input
2
1 10
11 20

Sample Output
Case 1: 10
Case 2: 3

Author
WHU

/* ***********************************************
Author        :CKboss
Created Time  :2015年09月08日 星期二 21时45分37秒
File Name     :HDOJ4389.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

int fn,wei[30];

void fj(int x)
{
    if(x==0) wei[0]=0,fn=1;
    else
    {
        fn=0;
        while(x)
        {
            wei[fn++]=x%10;
            x/=10;
        }
    }
}

int dp[10][82][82][82];

int dfs(int pos,int presum,int sum,int mod,bool limit)
{
    if(sum>81||presum>sum) return 0;
    if(pos==-1) 
    {
        return (presum==sum)&&(mod==0);
    }

    if(limit==false&&dp[pos][presum][sum][mod]!=-1)
        return dp[pos][presum][sum][mod];

    int ed=9;
    if(limit==true) ed=wei[pos];
    int ret=0;

    for(int i=0;i<=ed;i++)
    {
        ret+=dfs(pos-1,presum+i,sum,(mod*10+i)%sum,(limit==true&&i==ed));
    }

    if(limit==false) dp[pos][presum][sum][mod]=ret;
    return ret;
}

int Get(int x)
{
    int ans=0;
    fj(x);
    for(int i=1;i<=fn*9;i++) 
    {
        ans+=dfs(fn-1,0,i,0,1);
    }
    return ans;
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);


    memset(dp,-1,sizeof(dp));
    int T_T,cas=1;
    scanf("%d",&T_T);
    while(T_T--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("Case %d: %d\n",cas++,Get(b)-Get(a-1));
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值