当前位置:网站首页>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 .
边栏推荐
- Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
- How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
- P2102 地砖铺设(dfs&贪心)
- MLAPI系列 - 04 - 网络变量和网络序列化【网络同步】
- tengine 内核参数
- POI add border
- Basic use of MySQL (it is recommended to read and recite the content)
- View workflow
- 729. My schedule I (set or dynamic open point segment tree)
- C. The Third Problem(找规律)
猜你喜欢
Is the mode of education together - on campus + off campus reliable
Recommendation | recommendation of 9 psychotherapy books
CertBot 更新证书失败解决
Slow SQL fetching and analysis of MySQL database
The most detailed and comprehensive update content and all functions of guitar pro 8.0
Basic use of MySQL (it is recommended to read and recite the content)
Solutions: word coverage restoration, longest serial number, Xiaoyu buys stationery, Xiaoyu's electricity bill
Figure application details
Dry goods collection | Vulkan game engine video tutorial
One question per day (Mathematics)
随机推荐
Slow SQL fetching and analysis of MySQL database
Guitar Pro 8.0最详细全面的更新内容及全部功能介绍
Execution order of scripts bound to game objects
Certbot failed to update certificate solution
Tengine kernel parameters
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Data processing methods - smote series and adasyn
How do programmers teach their bosses to do things in one sentence? "I'm off duty first. You have to work harder."
How to realize automatic playback of H5 video
Easyrecovery reliable and toll free data recovery computer software
HotSpot VM
SharedPreferences source code analysis
The most detailed and comprehensive update content and all functions of guitar pro 8.0
How does computer nail adjust sound
How does vs change the project type?
Yyds dry goods inventory OSI & tcp/ip
Lagrange polynomial
CADD course learning (8) -- virtual screening of Compound Library
Word cover underline
2327. Number of people who know secrets (recursive)