HUST 1600 由4,8组成的数且能整除48 (dfs枚举)

5 篇文章 0 订阅


Isun loves digit 4 and 8 very much. He thinks a number is lucky only if the number satisfy the following conditions: 
1.       The number only consists of digit 4 and 8. 
2.       The number multiples 48. 
One day, the math teacher gives  Isun a problem: 
         Given L and R(1 <= L <= R <= 10^15), how many lucky numbers are there between L and R. (i.e. how many x satisfy L <= x <= R, x is a lucky number). 

Input

Multiple test cases. For each test case, there is only one line consist two numbers L and R. 

Output

For each test case, print the number of lucky numbers in one line. 
  
Do use the %  lld specifier or  cin/  cout stream to read or write 64-bit integers in С++. 

Sample Input

1 48
1 484848

Sample Output

1
7



题意RT;

以后枚举数,可以用dfs来做!!!!!!! 这种题老是想不到啊。。。



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<stack>
#include<map>
using namespace std;
typedef long long ll;
stack<ll>ss;
map<int,ll>f;
void dfs(ll num)
{
	if(num>1000000000000000) return ;
	if(num%48==0) ss.push(num);
	dfs(num*10+4);
	dfs(num*10+8);
}
int main()
{
	int i,j,n,ans;
	ll r,l;
	dfs(0);
	n=0;
	while(!ss.empty()) {
		f[n++]=ss.top();
		ss.pop();
	}
	while(cin>>l>>r) {
		ans=0;
		for(i=0;i<n;i++) {
			if(f[i]>=l && f[i]<=r) ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值