zoukankan      html  css  js  c++  java
  • 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
    ",calc(r)-calc(l-1)) ;
    	return 0 ;
    }
    
  • 相关阅读:
    SVN导入maven项目
    jQuery (二)DOM 操作
    jQuery (一)选择器
    jQuery 初识
    mysql的OFFSET实现分页
    MongoDB学习之(三)增删查改
    java安全性的一种简单思路
    java Spring 事务的初次使用与验证
    C#语法之匿名函数和Lambda表达式
    C#语法之委托和事件
  • 原文地址:https://www.cnblogs.com/lyt020321/p/11781223.html
Copyright © 2011-2022 走看看