[數位dp] spoj 10738 Ra-One Numbers

來源:互聯網
上載者:User

標籤:style   blog   http   io   os   ar   strong   for   sp   

題意:給定x、y,為[x,y]之間有多少個數的偶數位和減去奇數位和等於一。

個位是第一位。

例子: 10=1-0=1 所以10是這樣的數

思路:數位dp[i][sum][ok] i位和為sum 是否含有前置0.

然後就是因為有負數 所以根據範圍把0設定為100 然後最後和等於101則為所求的數。

代碼:

[cpp] view plaincopyprint?
  1. #include"cstdlib" 
  2. #include"cstdio" 
  3. #include"cstring" 
  4. #include"cmath" 
  5. #include"stack" 
  6. #include"algorithm" 
  7. #include"iostream" 
  8. using namespace std; 
  9. int dp[12][200][2],num[12]; 
  10. int fuck[2]={1,-1}; 
  11. int dfs(int site,int sum,int ok,int f) 
  12. { 
  13.     if(site==0) 
  14.     { 
  15.         if(ok==0) return 0; 
  16.         return sum==101?1:0;  //小處理 和為101 
  17.     } 
  18.     if(!f&&dp[site][sum][ok]!=-1) return dp[site][sum][ok]; 
  19.     int len=f?num[site]:9; 
  20.     int ans=0; 
  21.     for(int i=0;i<=len;i++) 
  22.     { 
  23.         if(ok==0) 
  24.         { 
  25.             if(i==0) ans+=dfs(site-1,sum,ok||i!=0,f&&i==len); 
  26.             else ans+=dfs(site-1,sum+i*fuck[site%2],ok||i!=0,f&&i==len); 
  27.         } 
  28.         else 
  29.         { 
  30.             ans+=dfs(site-1,sum+i*fuck[site%2],ok||i!=0,f&&i==len); 
  31.         } 
  32.     } 
  33.     if(!f) dp[site][sum][ok]=ans; 
  34.     return ans; 
  35. } 
  36. int solve(int x) 
  37. { 
  38.     if(x<0) return 0; 
  39.     int cnt=0; 
  40.     while(x) 
  41.     { 
  42.         num[++cnt]=x%10; 
  43.         x/=10; 
  44.     } 
  45.     return dfs(cnt,100,0,1); //進入的時候 sum=100 
  46. } 
  47. int main() 
  48. { 
  49.     int t; 
  50.     scanf("%d",&t); 
  51.     memset(dp,-1,sizeof(dp)); 
  52.     while(t--) 
  53.     { 
  54.         int x,y; 
  55.         scanf("%d%d",&x,&y); 
  56.         printf("%d\n",solve(y)-solve(x-1)); 
  57.     } 
  58.     return 0; 
  59. } 

[數位dp] spoj 10738 Ra-One Numbers

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.