当前位置:网站首页>P2022 有趣的数(二分&数位dp)
P2022 有趣的数(二分&数位dp)
2022-07-06 04:11:00 【Harris-H】
P2022 有趣的数(二分&数位dp)
麻了。
二分答案,然后用数位dp计算 [ 1 , x ] [1,x] [1,x]中有多少个数字字典序 < < < k k k。
数位dp的时候开两个数组 a , b a,b a,b存 x , k x,k x,k。
然后就是常规的数位dp操作。
注意如果 d p dp dp数组开到 l i m , l e a d lim,lead lim,lead 则每次 c h e c k check check 都需要初始化 d p dp dp为 − 1 -1 −1。
因为这两个变量与 x x x有关。
否则可以只初始化一次。
另外 w , w 1 w,w1 w,w1长度不一定相同,所以 p o s 2 pos2 pos2可能减到 − 1 -1 −1。因此让 w 1 w1 w1基数为 20 20 20。
// Problem: P2022 有趣的数
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2022
// Memory Limit: 125 MB
// Time Limit: 400 ms
// Date: 2022-07-05 13:23:31
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll f[20][40][2][2][2],n,m;
int a[20],w;
int b[40],w1;
ll dfs(int pos, int pos2, bool cmp, bool lim, bool lead) {
if (pos == 0)
return (cmp || pos2 > 20) && (!lead);
if ( f[pos][pos2][cmp][lim][lead] != -1)
return f[pos][pos2][cmp][lim][lead];
ll tmp = 0, up = lim ? a[pos] : 9;
for (int i = 0; i <= up; ++i) {
bool nxcmp = !(lead && i == 0) && i < b[pos2];
nxcmp |= cmp;
if (!cmp && i > b[pos2])
continue;
tmp += dfs(pos - 1, pos2 - ((lead && i == 0) ? 0 : 1), nxcmp, lim && i == a[pos], lead && i == 0);
}
return f[pos][pos2][cmp][lim][lead] = tmp;
}
ll check(ll n) {
w = 0;memset(f, -1, sizeof (f));
while (n)
a[++w] = n % 10, n /= 10;
//printf("w=%d\n",w);
ll re = dfs(w, w1, false, true, true);
return re;
}
int main()
{
scanf("%lld%lld", &n, &m);
w1 = 20;
ll l = m > n ? m : n, r = 7e17, ans = 0;
while (n)
b[++w1] = n % 10, n /= 10;
//printf("w1=%d\n",w1);
for (; l <= r;) {
ll mid = (l + r) >> 1, tmp = check(mid);
// printf("%lld %lld\n",mid,tmp);
if (tmp == m - 1) {
ans = mid; r = mid - 1;
}
else if (tmp < m - 1) l = mid + 1;
else r = mid - 1;
}
printf("%lld\n", ans);
}
边栏推荐
- Unity中几个重要类
- 1291_ Add timestamp function in xshell log
- P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
- 使用JS完成一个LRU缓存
- 【可调延时网络】基于FPGA的可调延时网络系统verilog开发
- How many of the 10 most common examples of istio traffic management do you know?
- Proof of Stirling formula
- STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
- Global and Chinese markets for otolaryngology devices 2022-2028: Research Report on technology, participants, trends, market size and share
- Basic use of MySQL (it is recommended to read and recite the content)
猜你喜欢

Overturn your cognition? The nature of get and post requests

TCP/IP协议里面的网关地址和ip地址有什么区别?
![[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos](/img/41/27ce3741ef29e87c0f3b954fdef87a.png)
[PSO] Based on PSO particle swarm optimization, matlab simulation of the calculation of the lowest transportation cost of goods at material points, including transportation costs, agent conversion cos

【按鍵消抖】基於FPGA的按鍵消抖模塊開發

STC8H开发(十二): I2C驱动AT24C08,AT24C32系列EEPROM存储
![[FPGA tutorial case 11] design and implementation of divider based on vivado core](/img/39/f337510c2647d365603a8485583a20.png)
[FPGA tutorial case 11] design and implementation of divider based on vivado core

10個 Istio 流量管理 最常用的例子,你知道幾個?
![[adjustable delay network] development of FPGA based adjustable delay network system Verilog](/img/82/7ff7f99f5164f91fab7713978cf720.png)
[adjustable delay network] development of FPGA based adjustable delay network system Verilog

Network security - Security Service Engineer - detailed summary of skill manual (it is recommended to learn and collect)

C (XXIX) C listbox CheckedListBox Imagelist
随机推荐
Lora gateway Ethernet transmission
P3033 [usaco11nov]cow steelchase g (similar to minimum path coverage)
[FPGA tutorial case 12] design and implementation of complex multiplier based on vivado core
math_ Derivative function derivation of limit & differential & derivative & derivative / logarithmic function (derivative definition limit method) / derivative formula derivation of exponential functi
[introduction to Django] 11 web page associated MySQL single field table (add, modify, delete)
综合能力测评系统
Web components series (VII) -- life cycle of custom components
Cf464e the classic problem [shortest path, chairman tree]
Global and Chinese markets for MRI safe implants 2022-2028: technology, participants, trends, market size and share Research Report
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
C form application of C (27)
Class A, B, C networks and subnet masks in IPv4
MySql數據庫root賬戶無法遠程登陸解决辦法
Ipv4中的A 、B、C类网络及子网掩码
Tips for using dm8huge table
asp. Core is compatible with both JWT authentication and cookies authentication
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
Figure application details
Path of class file generated by idea compiling JSP page
[disassembly] a visual air fryer. By the way, analyze the internal circuit