其他分享
首页 > 其他分享> > P4127 [AHOI2009]同类分布

P4127 [AHOI2009]同类分布

作者:互联网

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#define int long long 
using namespace std ;
int read() {
    int x = 0 ;bool f = 1 ; char s = getchar() ;
    while(s > '9' || s < '0') {if(s == '-') f = -1 ; s = getchar() ;}
    while(s <='9' && s >='0') {x = x * 10 + (s-'0'); s = getchar() ;}
    return x*f ;
}
signed num[20] ;
int f[20][201][201][2] ;
int l , r , MOD ;
int dfs(int pos ,int sum ,int ha, int lead ,int limit) {
    if(!pos) {
        if(!ha&&sum==MOD) return 1 ;
        else return 0 ;
    }
    if(!limit&&!lead&&f[pos][sum][ha][lead] != -1) return f[pos][sum][ha][lead] ;
    int up = limit ? num[pos] : 9 ;
    int res = 0 ;
    for(int i = 0 ; i <= up ; i ++) {
        res += dfs(pos-1,sum+i,(ha*10+i)%MOD,lead&&(i==0),limit&&(i==up)) ;
    }
    if(!limit&&!lead) f[pos][sum][ha][lead] = res ;
    return res ;
}
int calc(int x) {
    int len = 0 ;
    while(x) {
        num[++len] = x % 10 ;
        x /= 10 ;
    }
    int res = 0 ;
    for(MOD = 1 ; MOD <= len * 9 ; MOD ++) {
        memset(f,-1,sizeof f) ;
        res += dfs(len , 0 , 0 , 1 , 1) ;   
    }
    return res ;
} 
signed main () {
    l = read() , r = read() ;
//  cout << calc(r) - calc(l-1) << endl ;
    printf("%lld\n",calc(r)-calc(l-1)) ;
    return 0 ;
}

标签:return,lead,AHOI2009,sum,pos,同类,int,include,P4127
来源: https://www.cnblogs.com/lyt020321/p/11781223.html