標籤: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?
- #include"cstdlib"
- #include"cstdio"
- #include"cstring"
- #include"cmath"
- #include"stack"
- #include"algorithm"
- #include"iostream"
- using namespace std;
- int dp[12][200][2],num[12];
- int fuck[2]={1,-1};
- int dfs(int site,int sum,int ok,int f)
- {
- if(site==0)
- {
- if(ok==0) return 0;
- return sum==101?1:0; //小處理 和為101
- }
- if(!f&&dp[site][sum][ok]!=-1) return dp[site][sum][ok];
- int len=f?num[site]:9;
- int ans=0;
- for(int i=0;i<=len;i++)
- {
- if(ok==0)
- {
- if(i==0) ans+=dfs(site-1,sum,ok||i!=0,f&&i==len);
- else ans+=dfs(site-1,sum+i*fuck[site%2],ok||i!=0,f&&i==len);
- }
- else
- {
- ans+=dfs(site-1,sum+i*fuck[site%2],ok||i!=0,f&&i==len);
- }
- }
- if(!f) dp[site][sum][ok]=ans;
- return ans;
- }
- int solve(int x)
- {
- if(x<0) return 0;
- int cnt=0;
- while(x)
- {
- num[++cnt]=x%10;
- x/=10;
- }
- return dfs(cnt,100,0,1); //進入的時候 sum=100
- }
- int main()
- {
- int t;
- scanf("%d",&t);
- memset(dp,-1,sizeof(dp));
- while(t--)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- printf("%d\n",solve(y)-solve(x-1));
- }
- return 0;
- }
[數位dp] spoj 10738 Ra-One Numbers