当前位置:网站首页>A digit DP
A digit DP
2022-06-11 02:31:00 【Dongdongyu】
bernays , Learn a number in one night dp Not bad, hehe
As the title :
600. There is no continuity 1 Non-negative integer
Given a positive integer n , The return range is [0, n] All nonnegative integers , Its binary representation does not contain Successive 1 The number of .
Namely dp[i][j] Express i The number of digits is j start , How many kinds are there .
In this case dp[i][j]=dp[i-1][k]( Sum according to the right )
Then there may be a mess of rules in the title , For example, continuous 1, Discontinuous 1, Does not contain a certain number … wait ; At this time, just change dp Array can , But notice , Inside dp Just initialize .
If it is to ask for 0~n Number on interval , Now all we have is the number of digits , To do ?
Refer to the idea of tree arrays , For example 324 The number of , First seek dp[3][0]+dp[3][1]+dp[3][2]; In this way, less than 300 All of them have been worked out , And then ask dp[2][0]+dp[2][1], This way <320 All of them can be found out , It can be seen as holding down a number from the high position to keep it unchanged, and then changing the value of the low position . This problem is solved according to binary system , Then check whether the high position is continuous when required 1, If yes, this dp Value cannot be added
Post a code :
int dp[40][2];
int digit[40];
int check(int *p,int size)
{
for(int i=1;i<size;i++)
{
if(p[i]==1&&p[i-1]==1)
{
return 0;
}
}
return 1;
}
int findIntegers(int n)
{
for(int i=0;i<40;i++)
{
dp[i][0]=0;
dp[i][1]=0;
digit[i]=0;
}
dp[1][1]=1;
dp[1][0]=1;
for(int i=2;i<40;i++)
{
dp[i][0]=dp[i-1][0]+dp[i-1][1];
dp[i][1]=dp[i-1][0];
}
int end=39;
int start;
int flag=1;
for(start=end;n!=0;start--)
{
digit[start]=n%2;
n=n/2;
if(start<39&&digit[start+1]==1&&digit[start]==1)
flag=0;
}
start++;
int ans=0;
int len=end-start+1;
int init=start;
for(int i=start;i<=end;i++)
{
for(int j=0;j<digit[i];j++)
{
if(check(&digit[init],i-init))
ans+=dp[(end-i)+1][j];
// ans+=dp[(end-i)+1][1];
}
}
return ans+flag;
}
边栏推荐
- Unity serial port communication
- Kotlin apply method
- How to read PMBOK guide in 3 steps (experience + data sharing)
- 【无标题】
- 92. CompletableFuture 实战
- Tencent test development post interview programming questions
- Knowledge competition of safety production month -- how much do you know about new safety law
- Setting access to win10 shared folder without verification
- The annual salary of testers in large factories ranges from 300000 to 8K a month. Roast complained that the salary was too low, but he was ridiculed by netizens?
- Find - (block find)
猜你喜欢

Jetpack Compose Box控件

Setting access to win10 shared folder without verification

Unity serial port communication

Cyclodextrin metal organic framework( β- Cd-mof) loaded with dimercaptosuccinic acid / emodin / quercetin / sucralose / diflunisal / omeprazole (OME)

Navicat Premium 15 工具自动被杀毒防护软件删除解决方法

421. 数组中两个数的最大异或值
![[3.delphi common components] 6 scroll bar](/img/55/891e56de4500a9128ac89e3c5b1721.jpg)
[3.delphi common components] 6 scroll bar

MOFs, metal organic framework materials of folic acid ligands, are loaded with small molecule drugs such as 5-fluorouracil, sidabelamine, taxol, doxorubicin, daunorubicin, ibuprofen, camptothecin, cur

贵金属白银和现货白银之间是什么关系

What can the enterprise exhibition hall design bring to the enterprise?
随机推荐
Sd3.0 notes
环糊精金属有机骨架(β-CD-MOF)装载二巯丁二酸/大黄素/槲皮素/三氯蔗糖/二氟尼柳/奥美拉唑(OME)
Is it appropriate for a 27 - year-old girl to change her career from zero to software testing?
Technology sharing | quick intercom, global intercom
How to guarantee the data quality of data warehouse?
ADVANCE.AI首席执行官寿栋将在2022新兴市场品牌出海线上峰会分享跨境电商运用AI技术合规
安全生产月知识竞赛——新安法知多少
Talk about an annotation implementation interface retry
About stepping on the pit diary and the association of knowledge points
clang-format 最全格式说明
Unity determines whether the object is in the camera field of view
[parallel and distributed systems] cache learning
mysql重装时写my.ini配置文件出错
Metal organic framework MOF Al (Diba), MOF Zr (Diba), MOF Fe (Diba) loaded with curcumin / carboxybenzylpenicillin /mtx methotrexate / paclitaxel ptx/ DOX / cisplatin cddp/cpt camptothecin and other d
如何保障数仓数据质量?
889. construct binary tree according to preorder and postorder traversal
可扩/减容线程池C语言原理讲解及代码实现
Kotlin let方法
Why is the trend chart of precious metal silver strong?
English subtitle video translated into Chinese subtitles