POJ3252-Round Numbers
来源:互联网 发布:c语言中switch 编辑:程序博客网 时间:2024/05/29 15:59
#include<stdio.h>
#include<string.h>
int a[100];
int dp[100][100][100];
int dfs(int pos,int cot0,int cot1,int lead,bool limit){
if(pos==-1){
if(cot0>=cot1) return 1;
if(lead==0) return 1;
return 0;
}
if(!limit && lead==1 && dp[pos][cot0][cot1]!=-1)
return dp[pos][cot0][cot1];
int ans=0;
int up = limit ? a[pos] : 1;
for(int i=0;i<=up;i++){
if(lead==0){
if(i==0)
ans+=dfs(pos-1,0,0,0,limit&&i==up);
else
ans+=dfs(pos-1,cot0,cot1+1,1,limit&&i==up);
}
else{
if(i==1)
ans+=dfs(pos-1,cot0,cot1+1,1,limit&&i==up);
else
ans+=dfs(pos-1,cot0+1,cot1,1,limit&&i==up);
}
}
if(!limit && lead == 1)
dp[pos][cot0][cot1]=ans;
return ans;
}
int solve(int x){
int pos=0;
while(x){
a[pos]=x%2;
x/=2;
pos++;
}
return dfs(pos-1,0,0,0,true);
}
int main(){
int l,r;
while(~scanf("%d%d",&l,&r)){
memset(dp,-1,sizeof(dp));
printf("%d\n",solve(r)-solve(l-1));
}
return 0;
}
#include<string.h>
int a[100];
int dp[100][100][100];
int dfs(int pos,int cot0,int cot1,int lead,bool limit){
if(pos==-1){
if(cot0>=cot1) return 1;
if(lead==0) return 1;
return 0;
}
if(!limit && lead==1 && dp[pos][cot0][cot1]!=-1)
return dp[pos][cot0][cot1];
int ans=0;
int up = limit ? a[pos] : 1;
for(int i=0;i<=up;i++){
if(lead==0){
if(i==0)
ans+=dfs(pos-1,0,0,0,limit&&i==up);
else
ans+=dfs(pos-1,cot0,cot1+1,1,limit&&i==up);
}
else{
if(i==1)
ans+=dfs(pos-1,cot0,cot1+1,1,limit&&i==up);
else
ans+=dfs(pos-1,cot0+1,cot1,1,limit&&i==up);
}
}
if(!limit && lead == 1)
dp[pos][cot0][cot1]=ans;
return ans;
}
int solve(int x){
int pos=0;
while(x){
a[pos]=x%2;
x/=2;
pos++;
}
return dfs(pos-1,0,0,0,true);
}
int main(){
int l,r;
while(~scanf("%d%d",&l,&r)){
memset(dp,-1,sizeof(dp));
printf("%d\n",solve(r)-solve(l-1));
}
return 0;
}
阅读全文
0 0
- POJ3252 Round Numbers
- POJ3252 Round Numbers
- poj3252 Round Numbers
- poj3252 Round Numbers
- poj3252--Round Numbers
- poj3252 Round Numbers
- POJ3252 Round Numbers
- poj3252 Round Numbers
- POJ3252 Round Numbers
- POJ3252-Round Numbers
- poj3252 Round Numbers
- poj3252[Round Numbers]
- 【POJ3252】 Round Numbers
- POJ3252-Round Numbers
- POJ3252 Round Numbers
- POJ3252 Round Numbers 数位DP
- poj3252.Round Numbers,组合数
- POJ3252---Round Numbers(数位dp)
- Day02 Jinja2模板引擎&Bootstrap
- c代码实现大位数减法
- 对int main(int argc,char* argv[])的理解
- 【Noip2011】选择客栈
- 转载:电子商务软件测试的探讨
- POJ3252-Round Numbers
- Idea的插件管理——持续更新
- TcpServe
- 技术与技术人员的价值
- Day03 Web表单
- ACM 暑假有感
- 8月19日CCPC——Vertex Cover(未解决)
- 201612-3 权限查询
- kafka异步发送数据,不阻塞