Take modulus and digital DP in the digital game interval Winter vacation camp

As digital games are really popular in the Association for Science and Technology recently, someone named a modulo number. This type of number must satisfy that the sum of the numbers modN is 0. Now everyone is going to play the game again. Specify an integer closed interval [a, b], and ask how many modulo there are in this interval.

Input format The
question has multiple sets of test data. Each group contains only three numbers a, b, and N.

Output format
Output one line for each test data, indicating the number of each digit and the number with modN being 0.

Sample
Input Output
1 19 9
2
Data Range and Reminder
For all data, 1≤a,b≤231−1,1≤N<100.
Take the modulus, even if the remainder state is changed at the current position, write it down on the board

#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;
ll dp[30][110];//dp[pos][pre]记录了遍历第pos为位时,前一位为pre时的状态数
ll a[30],b[30];
int p;
// void init(){
    
    
//     dp[0][0]=1;
//     dp[0][1]=dp[0][2]=0;
//     for(int i=1;i<25;i++){
    
    
//         dp[i][0]=10*dp[i-1][0]-dp[i-1][1];//长度为i不含有49的个数
//         dp[i][1]=dp[i-1][0];//最高位为9但不含49的个数
//         dp[i][2]=10*dp[i-1][2]+dp[i-1][1];//含有49的个数
//     }
// }
int dfs(int pos,int pre,bool limit) {
    
    
	if(!pos){
    
    
        if(pre%p==0)
            return 1;
        else return 0;
    }	
	if(!limit&&dp[pos][pre]!=-1)	
        return dp[pos][pre];
    int up;
    if(limit)
        up=a[pos];
    else up=9;
    int ans=0;
	for(int i=0;i<=up;i++) {
    
    
		ans+=dfs(pos-1,pre+i%p,limit&&i==up);
	}
    if(!limit)
        dp[pos][pre]=ans;
	return ans;
}

int init(int n)
{
    
    
    int len=0;
    while(n){
    
    
        a[++len]=n%10;
        n/=10;
    }
    return dfs(len,0,1);
}

void solve(){
    
    
   int x,y;
   while(scanf("%d%d%d",&x,&y,&p)!=EOF){
    
    
       memset(dp,-1,sizeof(dp));
       cout<<init(y)-init(x-1)<<endl;
   }
}

int main()
{
    
    
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    solve();
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45891413/article/details/112909745