FZU 2113 Jason的特殊愛好

Problem 2113 Jason的特殊愛好 

Accept: 87    Submit: 300
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Jason很喜歡數字,特別是1這個數字,因爲他覺得1有特殊的含義。爲了讓更多的人喜歡上1,他決定出一題關於1的水題(每個人都喜歡水題)。

Input

輸入數據中有多組數據,每組數據輸入爲兩個正數,a,b(1<=a,b<=10^18)。

Output

輸出a到b之間的整數包含多少個1。

Sample Input

1 1000

Sample Output

301

#include<cstdio>
#include<cstring>
#define  ll __int64
ll p[20],s[20],dp[20][10];
int a[20];
ll dfs(int i,int pre,bool e){
	if(i==-1)return pre==1;
	if(!e&&dp[i][pre]!=-1)return dp[i][pre];
	ll res=0;
	int u=e?a[i]:9,d;
	for(d=0;d<=u;d++)res+=dfs(i-1,d,e&&d==u);
	if(pre==1){
		if(!e)res+=p[i+1];
		else res+=s[i]+1;
	}
	return e?res:dp[i][pre]=res;
}
ll cal(ll n){
	int i=0;
	ll x=n;
	while(n){
		a[i]=n%10,n/=10;
		s[i]=x%p[i+1];
		i++;
	}
	return dfs(i-1,0,1);
}
int main(){
	ll l,r;
	p[0]=1;for(int i=1;i<20;i++)p[i]=p[i-1]*10;
	memset(dp,-1,sizeof(dp));
	while(~scanf("%I64d%I64d",&l,&r))printf("%I64d\n",cal(r)-cal(l-1));
	return 0;
}


發佈了71 篇原創文章 · 獲贊 14 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章