当前位置:网站首页>Digital children < daily question> (Digital DP)
Digital children < daily question> (Digital DP)
2022-07-06 04:31:00 【CTGU-Yoghurt】
subject :
Topic link : Sign in — major IT Written interview preparation platform _ Cattle from
https://ac.nowcoder.com/acm/contest/23480/D
Ideas :
digit dp The title of is usually a set of templates
Based on the original template
Change according to the subject conditions
So as to meet the requirements of the topic
The following three questions are analyzed
To meet the requirements of the topic 1 We have to be right
Sum every two adjacent digits
And judge whether it is a prime number
If not, proceed dfs
Because the title is required to be at least one digit 1
We don't know who is 1
So the judgment of this condition
You need to fill in this number completely before you can judge
If the digits have 1 And
No, 1 The situation of
General digits dp The topic will have some requirements
We can go through lead
Find the highest position
In this way, you can remove the leading 0 Influence
Code details :
#include<stdio.h>
#include<iostream>
using namespace std;
#include<string.h>
const int N = 15;
#define int long long// take int The defined variable is converted to long long
bool check(int n)// Check whether it is prime
{
return n == 2 || n == 3 || n == 5 || n == 7 || n == 11
|| n == 13 || n == 17 || n == 19;
}
int l, r, a[N], len, dp[N][N][2];// first [] What's the number , the second [] Indicates the number on this bit , Third [] Indicates whether it contains at least one 1
int dfs(int pos, int pre, int lead, int sum1, int limit)
{
if (pos == 0) return sum1;// Traverse all digits , Number of schemes returned ( contains 1 Then return to 1, Otherwise return to 0)
if (limit == 0 && lead==0 && dp[pos][pre][sum1] != -1)
return dp[pos][pre][sum1];// Not the highest position , There is no forerunner 0, And have been memorized ( Once traversed )
int up = limit ? a[pos] : 9;// Determine whether it is the highest
int ans = 0;
for (int i = 0; i <= up; i++)
{
if (lead == 0 && check(pre + i)==0) continue;// There is no forerunner 0, And the addition of adjacent positions is not a prime number
ans += dfs(pos - 1, i, lead && i == 0, (i == 1) || sum1, limit && i == up);
}
// The front is not the highest position , There is no forerunner 0
if ( limit == 0 && lead==0 )
dp[pos][pre][sum1] = ans;// Memorization results
return ans;
}
int quwei(int n)
{
len = 0;
while (n) a[++len] = n % 10,n/=10;
return dfs(len, 0,1,0,1);
}
signed main()// Pay attention to the int Transformed into long long So use signed main)()
{
cin >> l >> r;
memset(dp, -1, sizeof(dp));
cout << quwei(r) - quwei(l - 1)<<endl;
return 0;
}PS: I don't understand digital dp You can see what bloggers upload PPT Oh .
边栏推荐
- 捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
- Knowledge consolidation source code implementation 3: buffer ringbuffer
- Lagrange polynomial
- ue5 小知识点 开启lumen的设置
- English Vocabulary - life scene memory method
- 牛顿插值法
- 1008 circular right shift of array elements (20 points)
- HotSpot VM
- Etcd database source code analysis -- etcdserver bootstrap initialization storage
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
猜你喜欢
![[tomato assistant installation]](/img/06/672a616d4fc2a43b83054eb1057628.jpg)
[tomato assistant installation]

ue5 小知识点 开启lumen的设置

Fedora/rehl installation semanage

Comprehensive ability evaluation system

Recommendation | recommendation of 9 psychotherapy books

Yyds dry inventory automatic lighting system based on CC2530 (ZigBee)
![[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University](/img/22/ead74bc121a64910ef6ef374cd029b.png)
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University

10个 Istio 流量管理 最常用的例子,你知道几个?

MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】

IDEA编译JSP页面生成的class文件路径
随机推荐
English Vocabulary - life scene memory method
word封面下划线
图应用详解
newton interpolation
11. Intranet penetration and automatic refresh
Solve the compilation problem of "c2001: line breaks in constants"
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
P3500 [poi2010]tes intelligence test (two points & offline)
Can CDC pull the Oracle table in full
[face recognition series] | realize automatic makeup
1291_Xshell日志中增加时间戳的功能
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
ue5 小知识点 开启lumen的设置
[Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
Easyrecovery reliable and toll free data recovery computer software
cdc 能全量拉去oracle 表嘛
Mysql database storage engine
How does computer nail adjust sound
canal同步mysql数据变化到kafka(centos部署)
In depth MySQL transactions, stored procedures and triggers
