X mod f(x) HDU - 4389 数位DP 寒假集训

定义一个函数 f(x):
int f ( int x ) {
   if ( x == 0 ) return 0;
   return f(x/10) + x%10;
}

现在,我们有个区间 (1 <= A <= B <= 10 9), 查询有多少个整数 x mod f(x) 等于 0.
Input
第一行是一个整数 T (1 <= T <= 50), 表示数据组数。
接下来 T 行每行包含两个整数 A B (1 <= A <= B <= 10 9)
Output
对于每组数据,输出区间内满足x mod f(x) 等于 0 的数的个数。
Sample Input
2
1 10
11 20
Sample Output
Case 1: 10
Case 2: 3
别开longlong 会爆MLE

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdio>
#define ls (p<<1)
#define rs (p<<1|1)
#define ll long long
using namespace std;
const int maxn = 2e6+5;
const int INF = 0x3f3f3f3f;
int dp[13][82][82][82];
int a[15];
int dfs(int pos,int sum,int mod,int wish,bool limt){
    
    
    if(pos<=-1) return sum==wish&&mod==0;
    if(!limt&&dp[pos][sum][mod][wish]!=-1) 
        return dp[pos][sum][mod][wish];
    int ans=0;
    int up;
    if(limt)
        up=a[pos];
    else up=9;
    for(int i=0;i<=up;i++){
    
    
        int tmp=(mod*10+i)%wish;
        ans+=dfs(pos-1,sum+i,tmp,wish,limt&&i==a[pos]);
    }
    if(!limt)
        dp[pos][sum][mod][wish]=ans;
    return ans;
}
int init(int x){
    
    
    int len=0;
    while(x){
    
    
        a[len++]=x%10;
        x/=10;
    }
    int ans=0;
    for(int i=1;i<=81;i++){
    
    
        ans+=dfs(len-1,0,0,i,true);
    }
    return ans;
}
void solve(){
    
    
    int t;
    cin>>t;
    memset(dp,-1,sizeof(dp));
    memset(a,0,sizeof(a));
    int cas=0;
    while(t--){
    
    
        int n,m;
        cin>>n>>m;
        ++cas;
        cout<<"Case "<<cas<<": ";
        cout<<init(m)-init(n-1)<<endl;
    }
}
int main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45891413/article/details/113000038
今日推荐