hdu3555 Bomb (判斷子串是否出現49) 數位dp
阿新 • • 發佈:2017-10-31
nbsp title show 出現 n) sum long long stream bom
題意:http://acm.hdu.edu.cn/showproblem.php?pid=3555
比不要62還要簡單 只不過在long long 上面註意些
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<vector> #include<math.h> #include<string> using namespacestd; #define INF 0x3f3f3f3f #define LL long long #define N 106 #define Lson rood<<1 #define Rson rood<<1|1 LL dp[63][11][3],d[63];///大數一定要用LL 避免出錯也可用LL LL dfs(int now,int up,int falg,int fp) { if(now==1) return falg; if(!fp&&dp[now][up][falg]!=-1) return dp[now][up][falg]; LL ans=0; int ma=fp?d[now-1]:9; for(int i=0;i<=ma;i++)///標記是否出現49 ans+=dfs(now-1,i,falg||(up==4&&i==9),fp&&ma==i); if(!fp&&dp[now][up][falg]==-1) dp[now][up][falg]=ans; return ans; } LL calc(LL x) { LL xxx=x; int len=0; while(xxx) { d[++len]=xxx%10; xxx/=10; } LL sum=0; for(int i=0;i<=d[len];i++) sum+=dfs(len,i,0,i==d[len]); return sum; } int main() { int T; LL n; scanf("%d",&T); memset(dp,-1,sizeof(dp)); while(T--) { scanf("%lld",&n); printf("%lld\n",calc(n)); } return 0; }
hdu3555 Bomb (判斷子串是否出現49) 數位dp