标签:dfs 杭电 acm
beautiful number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 136 Accepted Submission(s): 78
Problem Description
Let A=∑ni=1ai?10n?i(1≤ai≤9)(n is
the number of A‘s
digits). We call A as
“beautiful number” if and only if a[i]≥a[i+1] when 1≤i<n and a[i]mod a[j]=0 when 1≤i≤n,i<j≤n(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(1≤L≤R≤109).
Output
For each case, output an integer means the number of “beautiful number”.
Sample Input
2
1 11
999999993 999999999
Sample Output
前几天刚学的DFS,这题算是比较简单的,虽然还是看别人的代码了= =。看到有很多人离线暴力出来然后打表的,也是机智。
因为一个y没有用long longWA了好几次,看来以后再做这种题就全部变量用long long吧。
代码如下,解释在注释里:
#include "iostream"
#include "algorithm"
using namespace std;
__int64 ans,l,r;
void dfs(int x,__int64 y) //x记录下一位数,y记录这个数
{
int i;
if(y>r)
return ; //当这个数超过r时返回
if(y<=r&&y>=l)
ans++;
for(i=1;i<=9;i++)
{
if(x%i==0)
{
dfs(x/i,y*10+x/i); //这里返回的是x/i而不是x%i是因为i是小的优先的,
} //即为了下一位数尽可能的大所以用除不用余
}
}
int main(int argc, char const *argv[])
{
int i,t;
scanf("%d",&t);
getchar();
while(t--)
{
ans=0;
scanf("%I64d%I64d",&l,&r);
for(i=1;i<=9;i++)
dfs(i,i);
printf("%I64d\n",ans);
}
return 0;
}
BestCoder Round #31 B题
标签:dfs 杭电 acm
原文地址:http://blog.csdn.net/dreamon3/article/details/44001711