【HDOJ】【3555】Bomb

数位DP



cxlove基础数位DP第二题

与上题基本相同(其实除了变成long long以外其实更简单了……)

 1 //HDOJ 3555
 2 #include<cmath>
 3 #include<vector>
 4 #include<cstdio>
 5 #include<cstring>
 6 #include<cstdlib>
 7 #include<iostream>
 8 #include<algorithm>
 9 #define rep(i,n) for(int i=0;i<n;++i)
10 #define F(i,j,n) for(int i=j;i<=n;++i)
11 #define D(i,j,n) for(int i=j;i>=n;--i)
12 #define pb push_back
13 using namespace std;
14 int getint(){
15     int v=0,sign=1; char ch=getchar();
16     while(!isdigit(ch)) {if(ch==‘-‘) sign=-1; ch=getchar();}
17     while(isdigit(ch))  {v=v*10+ch-‘0‘; ch=getchar();}
18     return v*sign;
19 }
20 const int N=1e7+10,INF=~0u>>2;
21 const double eps=1e-8;
22 typedef long long LL;
23 /*******************template********************/
24 LL dp[25][3];
25 LL len,bit[25];
26 //dp[i][0]表示不含49的i位数字
27 //dp[i][1]表示不含49,且最高位为9
28 //dp[i][2]表示存在49
29 void init(){
30     dp[0][0]=1;
31     F(i,1,20){
32         dp[i][0]=(LL)dp[i-1][0]*10-dp[i-1][1];
33         dp[i][1]=dp[i-1][0];
34         dp[i][2]=(LL)dp[i-1][2]*10+dp[i-1][1];
35     }
36 }
37 LL solve(LL n){
38     len=0; n++;
39     for(;n;n/=10) bit[++len]=n%10;
40     bit[len+1]=0;
41     printf("check : ");
42     D(i,len,1) printf("%lld",bit[i]);
43     printf("\n");
44     LL ans=0;
45     bool flag=false;
46     D(i,len,1){
47         ans+=(LL)dp[i-1][2]*bit[i];
48         if (flag) ans+=(LL)dp[i-1][0]*bit[i];
49         if (!flag && bit[i]>4) ans+=dp[i-1][1];
50         if (bit[i+1]==4 && bit[i]==9) flag=true;
51     }
52     return ans;
53 }
54 int main(){
55     init();
56     int T;LL n;
57     for(T=getint();T;T--){
58         scanf("%I64d",&n);
59         printf("%I64d\n",solve(n));
60     }
61     return 0;
62 }

时间: 2024-10-05 05:11:48

【HDOJ】【3555】Bomb的相关文章

【HDOJ图论题集】【转】

1 =============================以下是最小生成树+并查集====================================== 2 [HDU] 3 1213 How Many Tables 基础并查集★ 4 1272 小希的迷宫 基础并查集★ 5 1325&&poj1308 Is It A Tree? 基础并查集★ 6 1856 More is better 基础并查集★ 7 1102 Constructing Roads 基础最小生成树★ 8 1232

【HDOJ 3555】Bomb

[HDOJ 3555]Bomb 基础数位dp 跟那个4 62差不多 这个能更简单点 慢慢来吧= .= 预处理好49连续的情况即可 代码如下: #include <iostream> #include <cstdio> #include <cstring> #define ll long long #define sc "%I64d" using namespace std; ll dp[20][3]; /* dp[0] 无连续49 dp[1] 无连续

【HDOJ 5653】 Bomber Man wants to bomb an Array.(DP)

[HDOJ 5653] Bomber Man wants to bomb an Array.(DP) Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 389    Accepted Submission(s): 117 Problem Description Give

【HDU 3555】Bomb

Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 12923 Accepted Submission(s): 4619 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists impr

hdoj 4738 Caocao&#39;s Bridges【双连通分量求桥】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3000    Accepted Submission(s): 953 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. Bu

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ 4763】 Theme Section (KMP+strstr)

[HDOJ 4763] Theme Section Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1999    Accepted Submission(s): 947 Problem Description It's time for music! A lot of popular musicians a