CF1036C
Classy Numbers
题面翻译
定义一个数字是“好数”,当且仅当它的十进制表示下有不超过\(3\)个数字\(1 \sim 9\)
举个例子:\(4,200000,10203\)是“好数”,然而\(4231,102306,7277420000\)不是
给定\([l,r]\),问有多少个\(x\)使得\(l \le x \le r\),且\(x\)是“好数”
一共有\(T(1 \le T \le 10^{4})\)组数据,对于每次的询问,输出一行一个整数表示答案
\(1 \le l_i \le r_i \le 10^{18}\)
样例 #1
样例输入 #1
4
1 1000
1024 1024
65536 65536
999999 1000001
样例输出 #1
1000
1
0
2
数位DP题 但是可以生成打表做
很好的一个思路 先把所有满足要求的存入vector
注意不能用set 因为后面查找的是位置 而set的查找只能找到值
细节要注意 是到了18位再v.push_back(num)否则会有重复
点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
int t,l,r;
vector<int>v;
void dfs(int ws,int gs,int num)//位数 1~9的个数 整个数
{
if(ws==18)
{
v.push_back(num);
return ;
}
dfs(ws+1,gs,10*num);//+ 0
if(gs<3)
{
for(int i=1;i<=9;i++)
dfs(ws+1,gs+1,10*num+i);
}
}
signed main()
{
dfs(0,0,0);
v.push_back(1000000000000000000);
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&l,&r);
printf("%lld\n",(long long)(upper_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l)));
}
}
此生无悔入OI 来生AK IOI

浙公网安备 33010602011771号