当前位置: 首页 > news >正文

数位dp-

P4999 烦人的数学作业 - 洛谷

#include <bits/stdc++.h>		

#define fi first
#define se second
#define endl '\n'
#define pb push_back

using namespace std;
using LL = long long;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;


const int N = 1e5 + 10,T = 20;


LL l,r;
LL a[23];
LL dp[23][N];//到第i位,所有数的数位和

LL mo(LL x)
{
	return (x % mod + mod) % mod;
}

LL dfs(int pos,bool limit,LL sum)
{
	if (pos == 0) return sum;
	
	if (!limit && dp[pos][sum] != -1) return dp[pos][sum];
	
	int up = limit ? a[pos] : 9;
	LL res = 0;
	for (int i = 0;i <= up;i ++)//枚举第pos位选择的数i
	{
		res = (res + dfs(pos - 1,limit && (i == up),sum + i)) % mod;
	}
	
	if (!limit) dp[pos][sum] = res;
	return res;
	
}

LL f(LL x)
{
	int len = 0;
	while(x)
	{
		a[++ len] = x % 10;
		x /= 10;
	}
	
	return dfs(len,true,0);
}

void solve()
{
	
	cin >> l >> r;
	cout << mo(f(r) - f(l - 1)) << endl;//可能出现负数
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _ = 1;
	cin >> _;
	memset(dp,-1,sizeof dp);//可复用的,多组样例都可使用
	while(_--) solve();
	
	return 0;
}	

P2602 [ZJOI2010] 数字计数 - 洛谷

#include <bits/stdc++.h>		

#define fi first
#define se second
#define endl '\n'
#define pb push_back

using namespace std;
using LL = long long;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;


const int N = 1e5 + 10,T = 20;

LL l,r;
LL a[15];
LL dp[15][N];

LL dfs(int pos,bool limit,bool lead,int query,int cnt)
{
	if (pos == 0) return cnt;
	
	if (!limit && !lead && ~dp[pos][cnt]) return dp[pos][cnt];
	
	int up = limit ? a[pos] : 9;
	LL res = 0;
	for (int i = 0;i <= up;i ++)
	{
		if (i == 0 && lead)	
			res += dfs(pos - 1,limit && (i == up),true,query,cnt);
		else res += dfs(pos - 1,limit && (i == up),false,query,cnt + (query == i ? 1 : 0));
	}
	
	if (!limit && !lead) dp[pos][cnt] = res;
	
	return res;
}


LL f(LL x,int query)
{
	int len = 0;
	while(x)
	{
		a[++ len] = x % 10;
		x /= 10;
	}
	return dfs(len,true,true,query,0);
}

void solve()
{
	cin >> l >> r;
	for (int i = 0;i <= 9;i ++)
		cout << f(r,i) - f(l - 1,i) << " ";
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _ = 1;
	// cin >> _;
	memset(dp,-1,sizeof dp);//可复用的,多组样例都可使用
	while(_--) solve();
	
	return 0;
}	

1.谁是帕鲁?【算法赛】 - 蓝桥云课

#include <bits/stdc++.h>		

#define fi first
#define se second
#define endl '\n'
#define pb push_back

using namespace std;
using LL = long long;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
const LL inf = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;

const int mod = 1e9 + 7;


const int N = 1e5 + 10,T = 20;

LL l,r,k;
LL dp[15][N];//到第i位且没有限制,有j个封闭图形的数量
LL a[15];
int b[] = {1,0,0,0,1,0,1,0,2,1};

LL dfs(int pos,bool lim,bool lead,LL cnt)
{
	if(pos == 0)
	{
		if (cnt == k) return 1;//满足恰好有k个封闭图形
		else return 0;
	}
	
	if (!lim && !lead && ~dp[pos][cnt]) return dp[pos][cnt]; 
	
	int up = lim?a[pos]:9;
	LL res = 0;
	for (int i = 0;i <= up;i ++)
	{
		if (i == 0 && lead) 
			res += dfs(pos - 1,lim && i == up,true,cnt);
		else 
			res += dfs(pos - 1,lim && i == up, false,cnt + b[i]);
	}
	
	if (!lim && !lead) dp[pos][cnt] = res;
	
	return res;
	
}

LL f(LL x)
{
	int len = 0;
	while(x)
	{
		a[++ len] = x % 10;
		x /= 10;
	}
	return dfs(len,true,true,0);
}

void solve()
{
	cin >> l >> r >> k;
	cout << f(r) - f(l - 1) << endl;
}

int main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _ = 1;
	// cin >> _;
	memset(dp,-1,sizeof dp);//可复用的,多组样例都可使用
	while(_--) solve();
	
	return 0;
}	

 JB国

http://www.dtcms.com/a/35542.html

相关文章:

  • el-select滚动获取下拉数据;el-select滚动加载
  • 【信息系统项目管理师-案例真题】2011上半年案例分析答案和详解
  • 使用python接入腾讯云DeepSeek
  • 数据类型转换
  • 项目范围管理--从规划到控制项目范围的核心思想
  • 多弹协同末制导律设计
  • C++与Python实现LiDAR点云投影对比:关键差异与易错点详解
  • CAESAR II 14管道应力和柔性分析软件
  • 【WSL2】 Ubuntu20.04 GUI图形化界面 VcXsrv ROS noetic Vscode 配置
  • Scratch032(百发百中)
  • Kafka RecordTooLargeException问题解决
  • 第三章 语言基础
  • 2025年信息科学与工程学院科协机器学习介绍——机器学习基本模型介绍
  • 智慧后勤的消防管理:豪越科技为安全护航
  • TDengine数据订阅新手入门避坑指南1/3
  • 2025/2/17--2/23学习笔记(week1)_C语言
  • C语言进阶习题【3】(5 枚举)——找单身狗2进阶版本
  • 【前沿探索篇七】【DeepSeek自动驾驶:端到端决策网络】
  • AWS Bedrock平台引入DeepSeek-R1 模型,推动深度学习
  • 网站搭建wp
  • unity学习51:所有UI的父物体:canvas画布
  • AI大模型趣味实战专栏 预告篇
  • 文件上传-黑名单关键字绕过
  • 用C/C++绘制跳动的爱心:从数学方程到动画实现
  • 数据安全_笔记系列02:国密算法(商用密码算法)详解
  • JavaScript querySelector()、querySelectorAll() CSS选择器解析(DOM元素选择)
  • MySQL数据库——常见慢查询优化方式
  • 基于STM32单片机设计的宠物喂食监控系统
  • Zap:Go 的高性能日志库
  • Linux故障排查和性能优化面试题及参考答案