【Undoubtedly Lucky Numbers】【CodeForces - 244B】(dfs+暴力打表)

本文介绍了一个算法问题,即计算不超过给定数字n的幸运数字的数量。幸运数字定义为仅由两个特定数字组成的正整数。通过深度优先搜索(DFS)算法,文章详细展示了如何生成所有可能的幸运数字,并统计它们的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:
 

Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky.

Let's call a positive integer a undoubtedly lucky, if there are such digits x and y(0 ≤ x, y ≤ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y.

Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number.

Input

The first line contains a single integer n (1 ≤ n ≤ 109) — Polycarpus's number.

Output

Print a single integer that says, how many positive integers that do not exceed nare undoubtedly lucky.

Examples

Input

10

Output

10

Input

123

Output

113

Note

In the first test sample all numbers that do not exceed 10 are undoubtedly lucky.

In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky.

解题报告:题目很简单就是给你一个数字n,问小于等于n有多少个幸运数字,幸运数字的定义就是其只由1或者2位数字循环组成的,因为之前做过类似的题目,就是dfs把每位的情况都实现出来,虽然n的数据范围达到了1e9,然而实际数字数目远远小于这个,就不用担心会爆内存了。

ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;

const int  maxn=1e5+1000;	
set<ll> a;
int n,x,y;
void dfs(ll num,int pos)	
{
	if(num>n||pos>10)
		return;
	a.insert(num);
	dfs(num*10+x,pos+1);
	dfs(num*10+y,pos+1);
}
int main()
{
	scanf("%d",&n);
		for(x=0;x<10;x++)
			for(y=x+1;y<10;y++)
				dfs(0,0);
	printf("%d\n",a.size()-1);
}       
//这里偷懒使用了x y为全局变量,使得dfs简化了数据。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值