P1836 数页码_NOI导刊2011提高(04)

和某道题出奇的相似

#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 , 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 ;
}
int f[20][20][2][2][11] , num[20] ;
int dfs(int pos ,int sum ,bool lead,bool limit ,int d) {
	if(!pos) return sum ;
	if(!limit&&!lead&&f[pos][sum][lead][limit][d] != -1) return f[pos][sum][lead][limit][d] ;
	int up = limit ? num[pos] : 9 ;
	int res = 0 ;
	for(int i = 0 ; i <= up ; i ++) {
		res += dfs(pos-1,sum+(i==d&&(i||!lead)),lead&&(i==0),limit&&(i==up),d) ;
	}
	if(!limit&&!lead) f[pos][sum][lead][limit][d] = res ;
	return res ;
}
int calc(int x,int d) {
	int len = 0 ;
	while(x) {
		num[++len] = x % 10 ;
		x /= 10 ;
	}
	memset(f,-1,sizeof f) ;
	return dfs(len,0,1,1,d) ;
} 
int r , ans ;
signed main () {
	r = read() ;
	for(int i = 0 ; i < 10 ; i ++) {
		ans += calc(r,i)*i ;
	}
	printf("%lld\n",ans) ;
	return 0 ;
}
posted @ 2019-11-02 08:16  _L_Y_T  阅读(128)  评论(0编辑  收藏  举报