【HDU 3709】Balanced Number

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3709
中文题面:http://blog.csdn.net/to_be_better/article/details/50731499

就是枚举一下平衡点,然后dp一下
另外……
我似乎真的爱上这种递归的写法了…..
7`XEKOC~R{6W$L~J1UPP9MX

#include<bits/stdc++.h>
#define LL long long
using namespace std;

int sta[20];
LL f[20][20][1500];

inline LL read(){
	char c=getchar(); LL ret=0;
	while (c<'0'||c>'9') c=getchar();
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret;
}

LL DFS(int t, int crt, int val, bool ruf) {
	if (val < 0) return 0;
	else if (!t) {
		return !val;
	} else if (~f[t][crt][val] && !ruf) {
		return f[t][crt][val];
	} else {
		LL ret = 0;
		for (int i=0,lim=ruf?sta[t]:9;i<=lim;i++) {
			ret += DFS(t-1,crt,val+(t-crt)*i,ruf&&i==sta[t]);
		}
		return ruf? ret: f[t][crt][val] = ret;
	}
}

inline LL cal(LL lim) {
	int cnt = 0;
	while (lim) {
		sta[++cnt] = lim % 10;
		lim /= 10;
	}
	LL ret = 0;
	for (int i=1;i<=cnt;i++) {
		ret += DFS(cnt,i,0,1);
	}
	return ret - cnt + 1;
}

int main(){
	memset(f,-1,sizeof(f));
	for (int T=read();T;--T) {
		LL l = read(), r = read();
		printf("%I64d\n",cal(r)-((l>0)?cal(l-1):0));
	}
	return 0;
}

16 thoughts to “【HDU 3709】Balanced Number”

  1. When someone writes an post he/she maintains the thought of a user in his/her brain that how a user can understand it.
    So that’s why this piece of writing is perfect.
    Thanks!

  2. I do agree with all of the ideas you’ve presented on your post.
    They’re very convincing and can certainly work.
    Nonetheless, the posts are too brief for newbies.
    May just you please lengthen them a bit from next time?
    Thank you for the post.

  3. Simply desire to say your article is as astounding.
    The clarity for your submit is just cool and i can think you’re a professional in this subject.
    Fine together with your permission allow me to grasp
    your RSS feed to stay up to date with drawing close post.
    Thank you 1,000,000 and please continue the enjoyable work.

  4. I like the valuable information you provide in your articles.
    I will bookmark your blog and check again here frequently.
    I am quite certain I’ll learn many new stuff right here!
    Best of luck for the next!

  5. Thanks for the marvelous posting! I truly enjoyed reading it, you
    happen to be a great author. I will be sure
    to bookmark your blog and definitely will come back from
    now on. I want to encourage you continue your great posts, have a nice afternoon!

  6. Wonderful blog! I found it while surfing around on Yahoo
    News. Do you have any suggestions on how to get listed in Yahoo News?
    I’ve been trying for a while but I never seem to get there!
    Thanks

  7. Hello there! Would you mind if I share your blog with my myspace
    group? There’s a lot of people that I think would really
    enjoy your content. Please let me know. Many thanks

  8. Magnificent site. Plenty of helpful info here. I’m sending it to several buddies ans also sharing in delicious.
    And obviously, thanks for your sweat!

  9. I think this is one of the most important info for
    me. And i am glad reading your article. But wanna remark on some general things, The website
    style is great, the articles is really great : D. Good job,
    cheers

  10. Woah! I’m really enjoying the template/theme of this site.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and visual appeal.
    I must say you have done a fantastic job with this. Additionally,
    the blog loads very quick for me on Chrome. Outstanding Blog!

Leave a Reply

Your email address will not be published. Required fields are marked *