码迷,mamicode.com
首页 > 其他好文 > 详细

hdu3555 Bomb(要49)

时间:2015-04-23 13:22:02      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:数位dp

Problem Description
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
 

Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.
 

Output
For each test case, output an integer indicating the final points of the power.
 

Sample Input
3 1 50 500
 

Sample Output
0 1 15
这是一道数位dp,比62要简单一些。
#include<stdio.h> #include<string.h> __int64 dp[25][5];  int a[20]; void init() { int i,j; dp[0][0]=1;                  //这里dp[0][0]=1还是挺重要的,讨论这个数的个位时有用  dp[1][0]=10;dp[1][1]=1;dp[1][2]=0; for(i=2;i<=20;i++){ dp[i][0]=dp[i-1][0]*10-dp[i-1][1];//表示不超过i位数的不带49的数  dp[i][1]=dp[i-1][0];              //表示不超过i位数的不带49且最高位为9的数   dp[i][2]=dp[i-1][2]*10+dp[i-1][1];//表示不超过i位数的带49的数  } } __int64 cal(__int64 n)      //计算的是[0,n-1],其中n是不是含49的数没有判断也没有算进去  { int i,j,len=0,flag; __int64 num=0; memset(a,0,sizeof(a)); while(n){ a[++len]=n%10;n=n/10; } if(len==1)return 0; flag=0;        //判断是否前面已经有49了  for(i=len;i>=1;i--){ num=num+a[i]*dp[i-1][2]; if(flag)num=num+a[i]*dp[i-1][0]; if(!flag && a[i]>4) num=num+dp[i-1][1]; if(a[i]==9 && a[i+1]==4) flag=1; } return num; } int main() { int T,m,i,j; __int64 n; init(); scanf("%d",&T); while(T--) { scanf("%I64d",&n); printf("%I64d\n",cal(n+1)); } return 0; }

hdu3555 Bomb(要49)

标签:数位dp

原文地址:http://blog.csdn.net/kirito_acmer/article/details/45219747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!