HDU - 4389 X mod f(x)(数位dp)

http://acm.hdu.edu.cn/showproblem.php?pid=4389

题意

为[A,B] 区间内的数能刚好被其位数和整除的数有多少个。

分析

典型的数位dp。。。比赛时想不出状态怎么构造,太弱了我。

言归正传,数位和有81种状态,怎么判断当前数字是否被整除呢?可以利用余数的思想,至此,定义状态dp[pos][sum][mod][res]表示前pos位的数位和为sum模mod的结果为res的个数,采用记忆化搜索比较简洁。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define lc idx<<1
#define rc idx<<1|1
#define lson l,mid,lc
#define rson mid+1,r,rc
using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
//const ll mod = 1000000007;
int T;
void testcase(){
    printf("Case %d: ",++T);
}
const int MAXN = 2e5+10;
const int MAXM = 30;

int bit[11];
int dp[11][82][82][82];

int dfs(int pos,int sum,int mod,int res,bool f){
    if(pos<0) return sum==mod&&res==0;
    if(!f&&dp[pos][sum][mod][res]!=-1) return dp[pos][sum][mod][res];
    int ed = f?bit[pos]:9;
    int ans=0;
    for(int i=0;i<=ed;i++){
        ans+=dfs(pos-1,sum+i,mod,(res*10+i)%mod,f&&(i==ed));
    }
    if(!f) dp[pos][sum][mod][res]=ans;
    return ans;
}

int solve(int x){
    int cnt = 0;
    while(x){
        bit[cnt++]=x%10;
        x/=10;
    }
    int ans=0;
    for(int i=1;i<=81;i++){
        ans+=dfs(cnt-1,0,i,0,1);
    }
    return ans;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif // LOCAL
    int t;
    scd(t);
    T=0;
    mset(dp,-1);
    while(t--){
        
        int A,B;
        scdd(A,B);
        testcase();
        cout<<solve(B)-solve(A-1)<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fht-litost/p/9291248.html
今日推荐