当前位置:网站首页>A数位dp
A数位dp
2022-06-11 01:42:00 【栋栋颻】
奈斯,一晚上学会个数位dp还不错嘿嘿
如题:
600. 不含连续1的非负整数
给定一个正整数 n ,返回范围在 [0, n] 都非负整数中,其二进制表示不包含 连续的 1 的个数。
就是dp[i][j]表示i位数以j开头,一共有多少种数。
这样的话dp[i][j]=dp[i-1][k](按照权求和)
然后题目里面可能会有乱七八糟的规则,比如说连续1,不连续1,不含有某个数…等等;这个时候只要改变dp数组就可以,但是注意,这里面的dp仅仅是初始化。
如果是求0~n区间上的数,现在有的只是多少位数的值,咋办?
参考树状数组那个思想,就是比如说324这个数,先求dp[3][0]+dp[3][1]+dp[3][2];这样就把小于300的都求出来了,然后再求dp[2][0]+dp[2][1],这样就把<320的都求出来,可以看成是从高位先按住一个数让他不变然后改变低位的数值。这个题就按照二进制那样求就行,然后需要求的时候注意检查高位有没有连续1,有的话这个dp值不能加
贴个代码:
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;
}
边栏推荐
- 1031. 两个非重叠子数组的最大和
- 环糊精金属有机骨架(β-CD-MOF)装载二巯丁二酸/大黄素/槲皮素/三氯蔗糖/二氟尼柳/奥美拉唑(OME)
- Test questions and answers of 2022r1 quick opening pressure vessel operation certificate
- Binary tree sequence traversal
- clang-format 最全格式说明
- SQL | 外连接
- Seven principles that should be known by software testers
- 贵金属白银行情走势图缘何强势?
- Go develop web
- 心态不能崩......
猜你喜欢

SQL | calculate sum

大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?

Secret

金属有机骨架材料Fe-MIL-53,Mg-MOF-74,Ti-KUMOF-1,Fe-MIL-100,Fe-MIL-101)负载异氟醚/甲氨蝶呤/阿霉素(DOX)/紫杉醇/布洛芬/喜树碱

NFT insider 61:animoca brands holds US $1.5 billion of encrypted assets in 340 investments

Multilevel mesoporous organometallic framework material zif-8 loaded with lactic acid oxidase (LOD) / ferric oxide (Fe304) / doxorubicin / insulin /cas9 protein / metronidazole / emodin methyl ether

Bingbing learning notes: find the greatest common divisor and the least common multiple. Complex version reverse string

Shader of double sided material

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

SQL | return customer name, relevant order number and total price of each order
随机推荐
SD3.0笔记
Online courses avaiable
Introduction for i-Teams
14:00面试,14:08就出来了 ,问的实在是太...
cannot import name ‘dtensor‘ from ‘tensorflow. compat. v2.experimental‘
项目 - Redis消息队列+工作线程取出用户操作日志并入库(二)
Binary tree sequence traversal
Find - (sequential search)
SQL | external connection
Seven principles that should be known by software testers
Byte beating client R & D Intern Tiktok side
How to guarantee the data quality of data warehouse?
What is the relationship between precious metal silver and spot Silver
Day code 300 lines learning notes day 22
Unity serial port communication
Epoll 反应堆模型核心原理及代码讲解
10007. ISBN number
从测试零基础到测试架构师,这10年,他是这样让自己成才的
Software testing interview reply: the technical side is not difficult for me, but the HR side is a hang up
InfoQ geek media's 15th anniversary solicitation | in depth analysis of container runtime Technology