Bomb

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 

Hint

From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
         

题意:求1到n一共有多少个49

一个简单的数位DP题。

<pre name="code" class="plain">#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
__int64 dp[22][4],n;
int b[22];
//dp[i][0]表示长度为i,包括49的个数
//dp[i][1]表示长度为i,开头为9的个数
//dp[i][2]表示长度为i,没有49
void fun()
{
    int i;
   memset(dp,0,sizeof(dp));
    dp[0][2]=1;
    for(i=1;i<20;i++)
    {
      dp[i][0]=(__int64)dp[i-1][0]*10+dp[i-1][1];//有49=上一位有9的 + 上一位有 49 *10
      dp[i][1]=dp[i-1][2];//有 9的 =上一位无 49的
      dp[i][2]=(__int64)dp[i-1][2]*10-dp[i-1][1];//    没有49的 =上一位无 49*10-上一位有9的
    }
}
int main()
{
  int t,i;
  fun();
  while(scanf("%d",&t)!=EOF)
  {
     while(t--)
     {
       scanf("%I64d",&n);
       n++;
       int len=0;
       while(n)
       {
         b[++len]=n%10;
         n/=10;
       }
       b[len+1]=0;
       bool f=0;
       __int64 ans=0;
       for(i=len;i>0;i--)
       {
         ans+=(__int64)b[i]*dp[i-1][0];
         if(f) ans+=(__int64)dp[i-1][2]*b[i];
         if(!f&&b[i]>4)  ans+=dp[i-1][1];
         if(b[i]==9&&b[i+1]==4) f=1;
       }
       printf("%I64d\n",ans);
     }
  }
return 0;
}
				
时间: 2024-08-03 15:51:03

Bomb的相关文章

CSAPP 3e: Bomb lab (secret_phase)

这是秘密关卡,需要通过主动调用secret_phase函数才能触发,可以通过call secret 或者jump *0x地址来调用. 贴出函数:(fun7函数部分没有注释,后边续上了手写的图来解析这个函数了) 0000000000401204 <fun7>: 401204: 48 83 ec 08 sub $0x8,%rsp 401208: 48 85 ff test %rdi,%rdi 40120b: 74 2b je 401238 <fun7+0x34>;如果%rdi==0,r

CSAPP 3e: Bomb lab (phase_6)

这一关很复杂,需要非常耐心.如果感觉容易在循环中绕晕,可以参考一下我最后附上的画图分析法2333,小把戏,不过挺有用的. 先看函数phase_6: 00000000004010f4 <phase_6>: 4010f4: 41 56 push %r14 4010f6: 41 55 push %r13 4010f8: 41 54 push %r12 4010fa: 55 push %rbp 4010fb: 53 push %rbx 4010fc: 48 83 ec 50 sub $0x50,%rs

CSAPP 3e: Bomb lab (phase_5)

调出phase_5函数: 0000000000401062 <phase_5>: 401062: 53 push %rbx 401063: 48 83 ec 20 sub $0x20,%rsp 401067: 48 89 fb mov %rdi,%rbx 40106a: 64 48 8b 04 25 28 00 mov %fs:0x28,%rax ;此处搞不懂 401071: 00 00 401073: 48 89 44 24 18 mov %rax,0x18(%rsp) 401078: 31

HDU 3622 Bomb Game(二分+2-SAT)

Bomb Game Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5396    Accepted Submission(s): 1925 Problem Description Robbie is playing an interesting computer game. The game field is an unbounde

[HDU3555]Bomb

试题描述 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 o

HDU 3555 —— Bomb

Bomb 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 "4

HDU 5653 Bomber Man wants to bomb an Array. DP

Bomber Man wants to bomb an Array. Problem Description Given an array and some positions where to plant the bombs, You have to print the Total Maximum Impact. Each Bomb has some left destruction capability L and some right destruction capability R wh

hdu 3555 Bomb(数位dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题目大意:就是给你一个数n,判断从0到n有多少个数含有数字49...... 是不是觉得跟hdu2089很相似呀... 思路:跟hdu2089一样的,注意给出的数比较大,所以这儿用__int64  .... code: #include<cstdio> #include<iostream> #include<cstring> #include<algorithm&

HDOJ 3622 Bomb Game 2-sat

http://acm.hdu.edu.cn/showproblem.php?pid=3622 题意:上个月写的,题目好像是说一对点要选一个引爆,引爆半径自己选,任意两圆不能相交,最后分数是所有圆的最小半径,求最大分数. 分析:二分半径,2-sat判定可行性. 1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 using namespace std;