当前位置:网站首页>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);
}
边栏推荐
- 【按鍵消抖】基於FPGA的按鍵消抖模塊開發
- Path of class file generated by idea compiling JSP page
- 2/13 qaq~~ greed + binary prefix sum + number theory (find the greatest common factor of multiple numbers)
- Data processing methods - smote series and adasyn
- C (thirty) C combobox listview TreeView
- Global and Chinese markets for endoscopic drying storage cabinets 2022-2028: Research Report on technology, participants, trends, market size and share
- Cf603e pastoral oddities [CDQ divide and conquer, revocable and search set]
- DM8 archive log file manual switching
- 颠覆你的认知?get和post请求的本质
- 查询mysql数据库中各表记录数大小
猜你喜欢
[Key shake elimination] development of key shake elimination module based on FPGA
ESP32(基于Arduino)连接EMQX的Mqtt服务器上传信息与命令控制
Maxay paper latex template description
User datagram protocol UDP
图应用详解
Plus d'un milliard d'utilisateurs de grandes entreprises comme Facebook ont été compromis, il est temps de se concentrer sur le did
20、 EEPROM memory (AT24C02) (similar to AD)
Detailed explanation of serialization and deserialization
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Execution order of scripts bound to game objects
随机推荐
Use js to complete an LRU cache
[Key shake elimination] development of key shake elimination module based on FPGA
Mathematical modeling regression analysis relationship between variables
Mlapi series - 04 - network variables and network serialization [network synchronization]
C form application of C (27)
Global and Chinese markets for patent hole oval devices 2022-2028: Research Report on technology, participants, trends, market size and share
Query the number and size of records in each table in MySQL database
Determine which week of the month the day is
Codeforces Round #770 (Div. 2) B. Fortune Telling
Solution to the problem that the root account of MySQL database cannot be logged in remotely
Hashcode and equals
[disassembly] a visual air fryer. By the way, analyze the internal circuit
2/11 matrix fast power +dp+ bisection
1291_Xshell日志中增加时间戳的功能
Cf603e pastoral oddities [CDQ divide and conquer, revocable and search set]
Lombok原理和同时使⽤@Data和@Builder 的坑
Slow SQL fetching and analysis of MySQL database
[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
【PSO】基于PSO粒子群优化的物料点货物运输成本最低值计算matlab仿真,包括运输费用、代理人转换费用、运输方式转化费用和时间惩罚费用
math_极限&微分&导数&微商/对数函数的导函数推导(导数定义极限法)/指数函数求导公式推导(反函数求导法则/对数求导法)