hdu 5179(bfs打表+二分)

beautiful number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
Let A=ni=1ai10ni(1ai9) ( n is the number of A 's digits). We call A as “beautiful number” if and only if a[i]a[i+1] when 1i<n and a[i] mod a[j]=0 when 1in,i<jn (Such as 931 is a "beautiful number" while 87 isn't).
Could you tell me the number of “beautiful number” in the interval [L,R] (including L and R)?
 

Input
The fist line contains a single integer T (about 100), indicating the number of cases.
Each test case begins with two integers L,R(1LR109) .
 

Output
For each case, output an integer means the number of “beautiful number”.
 

Sample Input
  
  
2 1 11 999999993 999999999
 

Sample Output
  
  
10 2
解题思路:根据"beautiful number"的特征,在10^9范围内数的个数肯定较少,故可以先打表求出10^9内的所有"beautiful number",这里可以采用bfs打表生成,然后对于每一次的L和R,采用二分找到相应的范围。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

int table[1000000];
int tot,L,R;

bool judge(int u,int k)
{
	while(u)
	{
		int tmp = u % 10;
		if(tmp % k != 0) return false;
		u = u / 10;
	}
	return true;
}

void bfs()
{
	queue<int> q;
	tot = 0;
	for(int i = 1; i <= 9; i++) q.push(i);
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		if(u > 1000000000) break;
		table[++tot] = u;
		for(int i = 1; i <= 9; i++)
		{
			if(u % 10 < i) break;
			if(judge(u,i))
				q.push(u * 10 + i);
		}
	}
}

int main()
{
	int t;
	bfs();  //先用bfs打表
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&L,&R);
		int l = lower_bound(table+1,table+1+tot,L) - table;
		int r = upper_bound(table+1,table+1+tot,R) - table;
		printf("%d\n",r - l);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值