设为首页 加入收藏

TOP

HDU4734――F(x)(数位DP)
2015-07-20 18:04:08 来源: 作者: 【 】 浏览:3
Tags:HDU4734 (数位

dp[i][j]表示i位数权值不超过j的数的个数

注意点

dp[i][j]的值不用每次都初始化,因为它的值不受输入的影响,如果前面算过了就直接拿来用,没算过就拿来算并记录下来


#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #include
         
           #include
          
            #include
            #include
            
              #include
             
               #include
              
                #include
               
                 #define inf 0x3f3f3f3f #define maxn 10000 typedef long long LL; using namespace std; int m,n,pos,f[11],fa; int dp[11][5000]; int num[11]; int dfs(int pos,int s,int e) { if(s<0) return 0; if(pos==-1) return s>=0; if(!e&&dp[pos][s]!=-1) return dp[pos][s]; int end=e?num[pos]:9; int sum=0; for(int i=0;i<=end;i++){ sum+=dfs(pos-1,s-i*f[pos],e&&i==end); } return e?sum:dp[pos][s]=sum; } int main() { for(int i=0;i<=10;i++){ f[i]=1<
                
                 >t; memset(dp,-1,sizeof dp); while(t--){ scanf("%d%d",&m,&n); int x=1,fa=0; while(m){ fa+=m%10*x; x<<=1; m/=10; } pos=0; while(n){ num[pos++]=n%10; n/=10; } printf("Case #%d: %d\n",iCase++,dfs(pos-1,fa,1)); } return 0; }
                
               
              
             
            
          
         
        
       
      
     
    
   



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj2689Prime Distance(数论) 下一篇hdu 4865 Peter's Hobby (隐..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: