当前位置:网站首页>CF1036C Classy Numbers 题解
CF1036C Classy Numbers 题解
2022-07-06 07:23:00 【q779】
CF1036C Classy Numbers 题解
题意:定义一个数字是“好数”,当且仅当它的十进制表示下有不超过 3 3 3个数字 1 ∼ 9 1 \sim 9 1∼9
举个例子: 4 , 200000 , 10203 4,200000,10203 4,200000,10203是“好数”,然而 4231 , 102306 , 7277420000 4231,102306,7277420000 4231,102306,7277420000不是
给定 [ l , r ] [l,r] [l,r],问有多少个 x x x使得 l ≤ x ≤ r l \le x \le r l≤x≤r,且 x x x是“好数”
一共有 T ( 1 ≤ T ≤ 1 0 4 ) T(1 \le T \le 10^{4}) T(1≤T≤104)组数据,对于每次的询问,输出一行一个整数表示答案
1 ≤ l i ≤ r i ≤ 1 0 18 1 \le l_i \le r_i \le 10^{18} 1≤li≤ri≤1018
一般数位dp可以用来解决 ∑ i = l r f ( i ) \sum _{i=l}^{r} f(i) ∑i=lrf(i) 的问题,
其中 f ( i ) f(i) f(i) 为与 i i i 的数位有关的某个函数或判定式
这题稍微变形了一下,那我们就理所当然地记录一下出现数字的情况
设 f i , j f_{i,j} fi,j 表示满 i i i 位数,有 j j j 个非 0 0 0 位
采用记忆化搜索,详见代码
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)()
int num[25],f[25][5];
// u表示当前位数,st表示当前的j,limit表示是否有高位限制
int dfs(int u,int st,bool limit)
{
if(!u)return 1;
if(!limit&&f[u][st]!=INF)
return f[u][st]; // 记忆化
int up=limit?num[u]:9,ans=0;
for(int i=0; i<=up; i++)
{
if(!i)ans+=dfs(u-1,st,limit&&num[u]==i);
else if(st!=3)ans+=dfs(u-1,st+1,limit&&num[u]==i);
}
if(!limit) f[u][st]=ans; // 只需记录无高位限制的
return ans;
}
int solve(int x)
{
int len=0;
while(x>0)
{
num[++len]=x%10;
x/=10;
}
return dfs(len,0,1);
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
memset(f,0x3f,sizeof(f));
int Q;cin >> Q;
while(Q--)
{
int l,r;
cin >> l >> r;
cout << solve(r)-solve(l-1) << '\n';
}
return 0;
}
参考文献
[1] https://www.luogu.com.cn/blog/rated/solution-cf1036c
转载请说明出处
边栏推荐
- jmeter性能测试步骤实战教程
- Oracle database 11gr2 uses TDE transparent data encryption to report an error ora28353. If you run to close the wallet, you will report an error ora28365. If you run to open the wallet, you will repor
- 位运算异或
- Structure summary of SystemVerilog integrable model
- SSM learning
- Establishment and operation of cloud platform open source project environment
- 多线程和并发编程(二)
- 软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
- Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
- OpenJudge NOI 2.1 1749:数字方格
猜你喜欢

Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied

Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file

Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin

数据仓库建设思维导图

SSM学习

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Week6 weekly report

Oracle column to row -- a field is converted to multiple rows according to the specified separator

剪映的相关介绍

Leetcode35. search the insertion position (simple, find the insertion position, different writing methods)
随机推荐
Project GFS data download
Typescript interface properties
Configure raspberry pie access network
jmeter性能测试步骤实战教程
Lesson 12 study notes 2022.02.11
TS Basics
LeetCode Algorithm 2181. Merge nodes between zero
杰理之开发板上电开机,就可以手机打开 NRF 的 APP【篇】
Markdown 中设置图片图注
Go learning -- implementing generics based on reflection and empty interfaces
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Bugku CTF daily question: do you want seeds? Blackmailed
Structure summary of SystemVerilog integrable model
navicat如何导入MySQL脚本
烧录场景下的源代码防泄密方案分享
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
word设置目录
学go之路(二)基本类型及变量、常量
【mysql学习笔记29】触发器
杰理之如若需要大包发送,需要手机端修改 MTU【篇】