HDU3709 Balanced Number
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3709
枚举中心位置,进行数位DP
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll dp[25][25][5000];//dp[pos][center][Value]; ll x,y; vector<int>shu; ll dfs(int pos,int center,int V,int sp){ if(V<0) return 0; if(pos==-1) return !V; if(!sp&&dp[pos][center][V]!=-1) return dp[pos][center][V]; ll ans=0; int maxn=sp?shu[pos]:9; for(int i=0;i<=maxn;i++){ ans+=dfs(pos-1,center,V+(pos-center)*i,sp&&i==maxn); } if(!sp) dp[pos][center][V]=ans; return ans; } ll cal(ll num){ shu.clear(); while(num){ shu.push_back(num%10); num/=10; } ll ans=0; for(int i=0;i<shu.size();i++){ ans+=dfs(shu.size()-1,i,0,1); } return ans-shu.size()+1;//每次枚举都有0 } int main(){ int t;cin>>t; memset(dp,-1,sizeof(dp)); while(t--){ cin>>x>>y; cout<<cal(y)-cal(x-1)<<endl; } }