zoukankan      html  css  js  c++  java
  • hdu 4389 X mod f(x) 数位dp

    X mod f(x)

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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
     
    Source
    dp进阶之路写法;
    或者分块打表;
    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<cstring>
    #include<vector>
    #include<list>
    #include<set>
    #include<map>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-4
    #define bug(x)  cout<<"bug"<<x<<endl;
    const int N=1e5+10,M=1e6+10,inf=2147483647;
    const ll INF=1e18+10,mod=2147493647;
    int f[11][83][83][83],bit[11];
    int dp(int pos,int sum,int m,int p,int flag)
    {
        if(pos==0)return (sum==p&&m==0);
        if(flag&&f[pos][sum][m][p]!=-1)return f[pos][sum][m][p];
        int x=flag?9:bit[pos];
        int ans=0;
        for(int i=0;i<=x;i++)
        {
            ans+=dp(pos-1,sum+i,(m*10+i)%p,p,flag||i<x);
        }
        if(flag)f[pos][sum][m][p]=ans;
        return ans;
    }
    int getans(int x,int p)
    {
        int len=0;
        while(x)
        {
            bit[++len]=x%10;
            x/=10;
        }
        return dp(len,0,0,p,0);
    }
    int main()
    {
        int T,cas=1;
        memset(f,-1,sizeof(f));
        scanf("%d",&T);
        while(T--)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            int ans=0;
            for(int i=1;i<=81;i++)
                ans+=getans(r,i)-getans(l-1,i);
            printf("Case %d: %d
    ",cas++,ans);
        }
        return 0;
    }
     
  • 相关阅读:
    建立自己的开发知识库?分享制作电子书的经验
    海量Office文档搜索
    为什么要检测数据库连接是否可用
    多年的.NET开发,也只学会了这么几招
    总结一下ERP .NET程序员必须掌握的.NET技术
    菜单设计器(Menu Designer)及其B/S,C/S双重实现(B/S开源)
    软件公司为什么要加密源代码
    .NET开发中经常用到的扩展方法
    在Win8 Mertro 中使用SQLite
    SQLite
  • 原文地址:https://www.cnblogs.com/jhz033/p/6591145.html
Copyright © 2011-2022 走看看