首页 > 其他分享 >[学习笔记+做题记录] 数位 DP

[学习笔记+做题记录] 数位 DP

时间:2023-05-12 14:46:10浏览次数:44  
标签:__ pbds gnu int tree 做题 include DP 数位

一、数位 DP

你说的对,但是数位 DP 是用于解决一种数位统计类似的问题,往往数据范围很大,比如 \(10^9, 10^{12}, 10^{18}\) 甚至更大,这种 DP 一般需要记录 当前考虑到第几位,是否贴上界 / 下界,以及一些题目里的东西,需要具体题目具体分析。

二、HDU 3652 - B-number

https://acm.hdu.edu.cn/showproblem.php?pid=3652

设 \(f_{i, 0/1, 0/1, j, k}\) 表示现在考虑到第 \(i\) 位,是否贴上界,是否出现连续 \(13\),以及 \(\bmod 13\) 的值和上一位的数。

然后就直接按照模拟转移即可,没什么好说的。

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair < int, int > PII;
typedef int itn;
mt19937 RND_MAKER (chrono :: steady_clock :: now ().time_since_epoch ().count ());
inline ll randomly (const ll l, const ll r) {return (RND_MAKER () ^ (1ull << 63)) % (r - l + 1) + l;}
//#define int long long
const double pi = acos (-1);
//__gnu_pbds :: tree < Key, Mapped, Cmp_Fn = std :: less < Key >, Tag = rb_tree_tag, Node_Upadte = null_tree_node_update, Allocator = std :: allocator < char > > ;
//__gnu_pbds :: tree < PPS, __gnu_pbds :: null_type, less < PPS >, __gnu_pbds :: rb_tree_tag, __gnu_pbds :: tree_order_statistics_node_update > tr;
inline int read () {
	int x = 0, f = 0;
	char c = getchar ();
	for ( ; c < '0' || c > '9' ; c = getchar ()) f |= (c == '-');
	for ( ; c >= '0' && c <= '9' ; c = getchar ()) x = (x << 1) + (x << 3) + (c & 15);
	return !f ? x : -x;
}
int awa, n, num[15], f[15][2][2][13][10];
inline void solve (int awa) {
	n = 0;
	memset (f, 0, sizeof f);
	while (awa) {
		num[++ n] = awa % 10;
		awa /= 10;
	}
	reverse (num + 1, num + 1 + n);
	f[0][1][0][0][0] = 1;
	for (int i = 0;i < n; ++ i) {
		for (int j = 0;j < 2; ++ j) {
			for (int k = 0;k < 2; ++ k) {
				for (int mod13 = 0;mod13 < 13; ++ mod13) {
					for (int las = 0;las < 10; ++ las) {
						for (int _ = 0;_ < 10; ++ _) {
							if (j == 0) {
								auto &tmp = f[i + 1][0][k | (las == 1 && _ == 3)][(mod13 * 10 + _) % 13][_];
								tmp += f[i][j][k][mod13][las];
							}
							else {
								if (_ == num[i + 1]) {
									auto &tmp = f[i + 1][1][k | (las == 1 && _ == 3)][(mod13 * 10 + _) % 13][_];
									tmp += f[i][j][k][mod13][las];
								}
								else if (_ < num[i + 1]) {
									auto &tmp = f[i + 1][0][k | (las == 1 && _ == 3)][(mod13 * 10 + _) % 13][_];
									tmp += f[i][j][k][mod13][las];
								}
							}
						}
					}
				}
			}
		}
	}
	int ans = 0;
	for (int i = 0;i < 10; ++ i) ans += f[n][0][1][0][i];
	for (int i = 0;i < 10; ++ i) ans += f[n][1][1][0][i];
	printf ("%d\n", ans);
	return ;
}
signed main () {
	int x;
	while (cin >> x) solve (x);
	return 0;
}
// Always keep it simple and stupid

三、洛谷 P4317 花神的数论题

https://www.luogu.com.cn/problem/P4317

设 \(f_{i, 0/1, j}\) 表示现在考虑到(二进制下的)第 \(i\) 位,是否贴上界,有 \(j\) 个 \(1\) 的方案数。

令 \(g_k = f_{n,0,k} + f_{n,1,k}\),即有 \(k\) 个 \(1\) 的数的个数。

所以答案就是:\(\prod\limits_{k}k^{g_k}\)。

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair < int, int > PII;
typedef int itn;
mt19937 RND_MAKER (chrono :: steady_clock :: now ().time_since_epoch ().count ());
inline ll randomly (const ll l, const ll r) {return (RND_MAKER () ^ (1ull << 63)) % (r - l + 1) + l;}
#define int long long
const double pi = acos (-1);
//__gnu_pbds :: tree < Key, Mapped, Cmp_Fn = std :: less < Key >, Tag = rb_tree_tag, Node_Upadte = null_tree_node_update, Allocator = std :: allocator < char > > ;
//__gnu_pbds :: tree < PPS, __gnu_pbds :: null_type, less < PPS >, __gnu_pbds :: rb_tree_tag, __gnu_pbds :: tree_order_statistics_node_update > tr;
inline int read () {
	int x = 0, f = 0;
	char c = getchar ();
	for ( ; c < '0' || c > '9' ; c = getchar ()) f |= (c == '-');
	for ( ; c >= '0' && c <= '9' ; c = getchar ()) x = (x << 1) + (x << 3) + (c & 15);
	return !f ? x : -x;
}
const int mod = 10000007;
int f[55][2][55], num[55], n, awa;
inline int pow_mod (int a, int b, int p) {
	int res = 1;
	while (b) {
		if (b & 1) res = res * a % p;
		b >>= 1, a = a * a % p; 
	}
	return res;
}
signed main () {
	awa = read ();
	while (awa) {
		num[++ n] = awa % 2;
		awa /= 2;
	}
	reverse (num + 1, num + 1 + n);
	memset (f, 0, sizeof f);
	f[0][1][0] = 1;
	for (int i = 0;i < n; ++ i) {
		for (int j = 0;j < 2; ++ j) {
			for (int k = 0;k <= i; ++ k) {
				for (int _ = 0;_ < 2; ++ _) {
					if (!j) {
						auto &tmp = f[i + 1][0][k + _];
						tmp += f[i][j][k];
					}
					else {
						if (_ == num[i + 1]) {
							auto &tmp = f[i + 1][1][k + _];
							tmp += f[i][j][k];
						}
						else if (_ < num[i + 1]) {
							auto &tmp = f[i + 1][0][k + _];
							tmp += f[i][j][k];
						}
					}
				}
			}
		}
	}
	int ans = 1;
	for (int i = 1;i < 55; ++ i) ans = ans * pow_mod (i, f[n][0][i] + f[n][1][i], mod) % mod;
	printf ("%lld\n", ans);
	return 0;
}
// Always keep it simple and stupid

标签:__,pbds,gnu,int,tree,做题,include,DP,数位
From: https://www.cnblogs.com/RB16B/p/17394086.html

相关文章

  • 2022-01-30:最小好进制。 对于给定的整数 n, 如果n的k(k>=2)进制数的所有数位全为1,则称 k(k
    2022-01-30:最小好进制。对于给定的整数n,如果n的k(k>=2)进制数的所有数位全为1,则称k(k>=2)是n的一个好进制。以字符串的形式给出n,以字符串的形式返回n的最小好进制。输入:“4681”输出:“8”解释:4681的8进制是11111。提示:n的取值范围是[3,10^18]。输入总是有效且......
  • 解决 apt-get Could not get lock /var/lib/dpkg/lock-frontend , it is held by proc
    问题: 用lsof命令看看这几个文件是被哪个进程锁住的啊,然后先杀掉那几个进程。不杀进程,直接移除文件的话,可能仍有其他进程在操作apt的缓存,多个命令同时写apt缓存很容易发生冲突sudolsof/var/lib/dpkg/lock-frontend 杀掉进程kill-98798 ......
  • wordpress中使用Nonce防止网站受到CSRF攻击
    使用Nonce(numberusedonce)是防止WordPress主题或者插件受到CSRF(cross-siterequestforgery)攻击最好的方法,WordPressNonce通过提供一个随机数,来实现在数据请求(比如,在后台保存插件选项,AJAS请求,执行其他操作等等)的时候防止未授权的请求。使用流程1、首先使用一个唯一的......
  • R语言分位数回归、最小二乘回归OLS北京市GDP影响因素可视化分析
    全文链接:http://tecdat.cn/?p=32372原文出处:拓端数据部落公众号对于影响北京市GDP因素分析常用的方法是最小二乘回归。【1】但最小二乘有自身的缺陷,该方法要求较高,例如许多观测数据很难满足全部假设条件。相比普通最小二乘法只能描述协变量对因变量条件均值变化的影响,分位数回......
  • 网络流做题记录
    网络流做题记录主要用来记录除了网络流24题之外的网络流题目。1.P4126[AHOI2009]最小割题意:对于每条边,求①这条边有没有可能在一种最小割中②这条边是不是一定在所有最小割中。思路:首先看第一问。首先可以想到,如果一条边没有满流,那显然不能在最小割里。那如果满流的边一定在......
  • AtCoder DP系列刷题记录
    直面自己的弱点。AFrog1简单线性\(\text{dp}\),令\(dp_i\)为跳到第\(i\)块石头的最小花费,易得:\[dp_i+=\min(|a_i-a_{i-1}|,|a_i-a_{i-2}|)\]BFrog2很快就写完了,但是一直调了十分钟,耻辱啊。如果反着跳,第\(i\)根木桩只能从第\(i+1\)或\(i+2\)木桩上跳到,则有:\[d......
  • JC1503 Object-OrientedProgramming
    UniversityofAberdeenSchoolofNaturalandComputingSciencesDepartmentofComputingScience2022–2023Programmingassignment–IndividuallyAssessed(noteamwork)Deadline:22:00,16thMay2023(SCNUlocaltime)Title:JC1503–Object-OrientedProgrammingN......
  • wordpress 为自定义类型文章新增自定义字段
    wordpress强大之处在于有很强的可自定义性,使得插件、主题的开发变得及其便利。就拿我们今天要说的自定义文章添加自定义字段来说,就很便捷。        比如我们要录入一个客户信息到wordpress中,那么需要的字段可不仅仅是什么标题、内容、摘要这么简单了,我们可能需要录入客户......
  • 倒计时 1 天:Tapdata LDP V3 发布会预告,看实时数据集成平台如何赋能企业 AI 落地
    更多LDP+AI场景细节,敬请期待5月10日(今天)的Tapdata发布会。最近几个月,AI领域可谓经历了近十年以来最为魔幻且不可思议的时刻。自ChatGPT发布以来,无论是底层大模型、训练框架、应用框架还是GPT插件等等各种新构想和产品层出不穷,为各行各业带来了深刻的变革和前所未......
  • 使用Actor-Critic的DDPG强化学习算法控制双关节机械臂
    在本文中,我们将介绍在Reacher环境中训练智能代理控制双关节机械臂,这是一种使用UnityML-Agents工具包开发的基于Unity的模拟程序。我们的目标是高精度的到达目标位置,所以这里我们可以使用专为连续状态和动作空间设计的最先进的DeepDeterministicPolicyGradient(DDPG)......