HDU 2089 不要62 && HDU 3555 Bomb (数位DP)
2016-08-10 15:50
316 查看
【题意】【L,R】中不含4或62的数字的个数。
【解题方法】数位DP,板子题。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int digit[10],dp[10][2],vis[10][2];
int dfs(int l,int six,int jud){
if(l==0) return 1;
if(!jud&&vis[l][six]) return dp[l][six];
int len=jud?digit[l]:9;
int nes=0;
for(int i=0; i<=len; i++){
if((i==4)||(six&&i==2)) continue;
nes+=dfs(l-1,i==6,jud&&(i==len));
}
if(!jud){
vis[l][six]=1;
dp[l][six]=nes;
}
return nes;
}
int f(int k){
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
int pos=0;
while(k){
digit[++pos]=k%10;
k/=10;
}
return dfs(pos,false,true);
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(n==0&&m==0) break;
int ans=f(n)-f(m-1);
printf("%d\n",ans);
}
}
【HDU 3555】
【题意】区间中含有49的个数。
【AC 代码】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int digit[25];
LL dp[25][2];
LL dfs(int l,int num,bool jud){
if(l==0) return 1;
if(!jud&&dp[l][num]!=-1) return dp[l][num];
LL ans=0;
int nex=jud?digit[l]:9;
for(int i=0; i<=nex; i++){
if(num&&i==9) continue;
ans+=dfs(l-1,i==4,jud&&i==nex);
}
if(!jud) dp[l][num]=ans;
return ans;
}
LL f(LL k){
int pos=0;
while(k){
digit[++pos]=k%10;
k/=10;
}
return dfs(pos,false,true);
}
int main()
{
memset(dp,-1,sizeof(dp));
int T;LL n;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
printf("%I64d\n",n-f(n)+1);
}
}
【解题方法】数位DP,板子题。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int digit[10],dp[10][2],vis[10][2];
int dfs(int l,int six,int jud){
if(l==0) return 1;
if(!jud&&vis[l][six]) return dp[l][six];
int len=jud?digit[l]:9;
int nes=0;
for(int i=0; i<=len; i++){
if((i==4)||(six&&i==2)) continue;
nes+=dfs(l-1,i==6,jud&&(i==len));
}
if(!jud){
vis[l][six]=1;
dp[l][six]=nes;
}
return nes;
}
int f(int k){
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
int pos=0;
while(k){
digit[++pos]=k%10;
k/=10;
}
return dfs(pos,false,true);
}
int main()
{
int n,m;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(n==0&&m==0) break;
int ans=f(n)-f(m-1);
printf("%d\n",ans);
}
}
【HDU 3555】
【题意】区间中含有49的个数。
【AC 代码】
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int digit[25];
LL dp[25][2];
LL dfs(int l,int num,bool jud){
if(l==0) return 1;
if(!jud&&dp[l][num]!=-1) return dp[l][num];
LL ans=0;
int nex=jud?digit[l]:9;
for(int i=0; i<=nex; i++){
if(num&&i==9) continue;
ans+=dfs(l-1,i==4,jud&&i==nex);
}
if(!jud) dp[l][num]=ans;
return ans;
}
LL f(LL k){
int pos=0;
while(k){
digit[++pos]=k%10;
k/=10;
}
return dfs(pos,false,true);
}
int main()
{
memset(dp,-1,sizeof(dp));
int T;LL n;
scanf("%d",&T);
while(T--){
scanf("%I64d",&n);
printf("%I64d\n",n-f(n)+1);
}
}
相关文章推荐
- hdu 2089 不要62 hdu 3555 Bomb (数位DP)
- HDU 3555 Bomb + HDU 2089 不要62 数位dp入门题目
- 【数位dp】hud 2089 不要62 hdu 3555 Bomb
- fzu 2113 Jason的特殊爱好 && hdu 2089 不要62 ( 数位dp )
- HDU 2089 不要62(数位DP·记忆化搜索)
- HDU 2089 不要62(数位dp&记忆化搜索)
- [HDU - 2089] 不要62 (动态规划 && 数位dp)
- HDU 2089 不要62(数位DP·记忆化搜索)
- HDU 2089 不要62 & HDU 3652 B-number(初级数位DP)
- HDU - 2089 不要62 (数位DP&记忆化dfs)水
- HDU 2089 不要62(数位dp)
- hdu 2089 不要62 数位dp
- hdu 2089 不要62(数位DP)
- hdu 2089 不要62(数位dp入门)
- HDU-2089 不要62 数位DP 模板题
- HDU 2089 不要62(数位DP)
- hdu 2089 不要62(初学数位DP)
- hdu 2089 不要62--数位dp入门
- HDU 2089 不要62 (数位DP)
- HDU 2089 不要62(数位DP入门)