[kuangbin带我飞]数位DP F(x)
2016-07-18 16:14
316 查看
New~ 欢迎参加2016多校联合训练的同学们~ |
F(x)Time Limit: 1000/500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3768 Accepted Submission(s): 1397 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A). Input The first line has a number T (T <= 10000) , indicating the number of test cases. For each test case, there are two numbers A and B (0 <= A,B < 109) Output For every case,you should output "Case #t: " at first, without quotes. The t is the case number starting from 1. Then output the answer. Sample Input 3 0 100 1 10 5 100 Sample Output Case #1: 1 Case #2: 2 Case #3: 13 Source 2013 ACM/ICPC Asia Regional Chengdu Online Recommend liuyiding | We have carefully selected several similar problems for you: 5722 5721 5720 5719 5718 Statistic | Submit | Discuss | Note |
<<是左移,比如1<<n,表示1往左移n位,即数值大小2的n次方 >>右移,类似左移,数值大小除以2的n次方
题目意思:对于一个数a,一个数b;
求在[0,b]的范围内有多少个数的F函数的值要小于等于FA;
输出数的个数!
解题思路:
对于每一对a,b所求出的FA 显然都是不一样的!
有错误的做法是:用数位DP把每一个数的 F函数值求出来并和FA 比较,好像这样的思路也是可以的,但是你会发现没有办法保存DP[pos][sum],因为这个DP里面保存的res是针对一个FA所得到的结论,在第二组样例中这样的DP显然就不能重复利用了!如果对每一组样例的DP初始化重新计算,这样你的答案会正确。只是显然这样会TLE。
有正确的做法是:每一次数位DP,其sum值保存的为FA-FX,这样就可以重复利用你的DP啦!
错误代码:
#include<cstdio> #include<cmath> #include<cstring> #include<iostream> using namespace std; #define ll long long int fa; int DP[20][6000]; int str[20]; int multi(int x){ int count=1; int ans=0; while(x){ ans+=(x%10)*count; count<<=1; x=x/10; } return ans; } int dfs(int pos,int sum,int limit){ if(pos<0){ return sum-fa<=0; } if(sum>fa) return 0; if(!limit&&DP[pos][sum]!=-1) return DP[pos][sum]; int res=0; int end=limit?str[pos]:9; for(int i=0;i<=end;i++){ res+=dfs(pos-1,sum+i*(1<<pos),limit&&(i==end)); } return res; } int solve(int x){ int len=0; while(x){ str[len++]=x%10; x=x/10; } len--; return dfs(len,0,1); } int main(){ int cas; int a,b; scanf("%d",&cas); memset(DP,-1,sizeof(DP)); for(int ii=1;ii<=cas;ii++){ scanf("%d%d",&a,&b); fa=multi(a); printf("Case #%d: %d\n",ii,solve(b)); } return 0; }<span style="color:#ff0000;"> </span>
正确代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
using namespace std;
#define ll long long
int fa;
int DP[20][6000];
int str[20];
int multi(int x){
int count=1;
int ans=0;
while(x){
ans+=(x%10)*count;
count<<=1;
x=x/10;
}
return ans;
}
int dfs(int pos,int sum,int limit){
if(pos<0){
return sum>=0;
}
if(sum<0)
return 0;
if(!limit&&DP[pos][sum]!=-1)
return DP[pos][sum];
int res=0;
int end=limit?str[pos]:9;
for(int i=0;i<=end;i++){
res+=dfs(pos-1,sum-i*(1<<pos),limit&&(i==end));
}
if(!limit)
DP[pos][sum]=res;
return res;
}
int solve(int x){
int len=0;
while(x){
str[len++]=x%10;
x=x/10;
}
len--;
return dfs(len,fa,1);
}
int main(){
int cas;
int a,b;
scanf("%d",&cas);
memset(DP,-1,sizeof(DP));
for(int ii=1;ii<=cas;ii++){
scanf("%d%d",&a,&b);
fa=multi(a);
//cout<<fa<<endl;
printf("Case #%d: %d\n",ii,solve(b));
}
return 0;
}
相关文章推荐
- 在masm下编译一个较小的PE文件
- Web基础知识的复习
- Weblogic war包部署时获取Web根目录问题
- log4的配置和使用
- leetcode_c++:链表:Remove Linked List Elements(203)
- HDU 1564 考虑奇偶性的变化
- java数组遍历——iterator和for方法
- 分布式缓存技术redis学习(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
- 第二周实习总结
- Hadoop 中文编码相关问题 -- mapreduce程序处理GBK编码数据并输出GBK编码数据(转)
- 【HDU 2717】Catch That Cow(BFS)
- 如何解决Win10 Win7 无法通过\\访问winxp文件
- JavaScript强化教程——JavaScript 运算符
- ubuntu16.04 SVN的安装与使用
- 关于TextView 中设置一行显示字数的两个属性: maxEms 与 maxLength
- Oracle意外删除数据文件恢复方法
- Comparator 与Comparable 异同
- HDOJ 1236 排名
- Ubuntu 开机自启动工具 update-rd.d 使用详解
- Mysql 安装-操作-备份