当前位置:网站首页>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 .
边栏推荐
- 关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解
- VNCTF2022 WriteUp
- [Yu Yue education] reference materials of complex variable function and integral transformation of Northwestern Polytechnic University
- Visio draws Tai Chi
- 729. My schedule I (set or dynamic open point segment tree)
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (柔性对接 AutoDock)
- Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?
- POI add border
- C. The Third Problem(找规律)
- Hashlimit rate control
猜你喜欢
Solution of storage bar code management system in food industry
捷码赋能案例:专业培训、技术支撑,多措并举推动毕业生搭建智慧校园毕设系统
11. Intranet penetration and automatic refresh
1291_Xshell日志中增加时间戳的功能
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 does computer nail adjust sound
Deep learning framework installation (tensorflow & pytorch & paddlepaddle)
颠覆你的认知?get和post请求的本质
食品行业仓储条码管理系统解决方案
Sorting out the latest Android interview points in 2022 to help you easily win the offer - attached is the summary of Android intermediate and advanced interview questions in 2022
随机推荐
How to solve the problem of slow downloading from foreign NPM official servers—— Teach you two ways to switch to Taobao NPM image server
The most detailed and comprehensive update content and all functions of guitar pro 8.0
POI add border
Selection of slow motion function
[HBZ sharing] how to locate slow queries in cloud database
Canal synchronizes MySQL data changes to Kafka (CentOS deployment)
E. Best Pair
Redis - redis in action - redis actual combat - actual combat Chapter 1 - SMS login function based on redis - redis + token shared session application - with code
About some basic DP -- those things about coins (the basic introduction of DP)
2/12 didn't learn anything
Basic explanation of turtle module - draw curve
SharedPreferences 源码分析
2328. Number of incremental paths in the grid graph (memory search)
Visio draws Tai Chi
Certbot failed to update certificate solution
Practical development of member management applet 06 introduction to life cycle function and user-defined method
Overturn your cognition? The nature of get and post requests
Recommendation | recommendation of 9 psychotherapy books
NPM command -- install dependent packages -- Usage / explanation
关于进程、线程、协程、同步、异步、阻塞、非阻塞、并发、并行、串行的理解