HDOJ3555 Bomb
来源:互联网 发布:matlab 矩阵转置 编辑:程序博客网 时间:2024/05/01 00:53
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 17591 Accepted Submission(s): 6473
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
3150500
Sample Output
0115
法一(数位DP
#include <bits/stdc++.h>#define mst(a) memset(a,0,sizeof(a))#define f(i,a,b) for(int i=a;i<b;i++)using namespace std;long long dp[20][3];void init(){ mst(dp); dp[0][2]=1; f(i,1,20) { dp[i][0]=dp[i-1][0]*10+dp[i-1][1]; //含49 dp[i][1]=dp[i-1][2]; //不含49但最高位为9 dp[i][2]=dp[i-1][2]*10-dp[i-1][1]; //不含49且最高位不为9 }}long long fun(long long n){ int len=1; int bit[25]; while(n) { bit[len++]=n%10; n/=10; } bit[len]=0; int flag=0; long long sum=0; for(int i=len;i>0;i--) { sum+=bit[i]*dp[i-1][0]; if(flag) sum+=bit[i]*dp[i-1][2]; if(!flag&&bit[i]>4) sum+=dp[i-1][1]; if(bit[i+1]==4&&bit[i]==9) flag=1; } return sum;}int main(){ init(); int t; long long n; scanf("%d",&t); while(t--) { scanf("%I64d",&n); printf("%I64d\n",fun(n+1)); } return 0;}
法二(记忆化搜索)
#include <bits/stdc++.h>#define mst(a) memset(a,-1,sizeof(a))#define f(i,a,b) for(int i=a;i<=b;i++)using namespace std;int bit[20];long long dp[20][3];//0:没有49且最后一位不为4,1:没有49且最后一位为4,2:有49//flag=1;无限制,flag=0,有限制long long dfs(int pos,int st,int flag){ if(pos==0) return st==2; if(flag&&dp[pos][st]!=-1) return dp[pos][st]; long long sum=0; int x=flag?9:bit[pos]; f(i,0,x) { if((st==2)||(st==1&&i==9)) sum+=dfs(pos-1,2,flag||i<x); else if(i==4) sum+=dfs(pos-1,1,flag||i<x); else sum+=dfs(pos-1,0,flag||i<x); } if(flag) dp[pos][st]=sum; return sum;}long long cal(long long x){ int len=0; while(x) { bit[++len]=x%10; x/=10; } return dfs(len,0,0);}int main(){ int t; scanf("%d",&t); mst(dp); while(t--) { long long n; scanf("%I64d",&n); printf("%I64d\n",cal(n)); } return 0;}
- HDOJ3555 Bomb
- HDOJ3555
- Bomb
- Bomb
- Bomb
- Bomb
- Bomb
- bomb
- Bomb
- Bomb
- bomb
- hdu3555 Bomb
- Bomb lab
- Fork bomb
- ZIP BOMB
- Fork bomb
- hdu3555 Bomb
- HDU3555 Bomb
- leetcode167. Two Sum II
- 交换机ip与网关ip冲突的问题
- 读《大学》领悟
- TCP,UDP浅析
- 三个水杯
- HDOJ3555 Bomb
- 组队赛3-B ,HDU 4961 Boring Sum(数学题)
- 使用BigDecimal进行精确运算
- SQL Server 基础之增删查改
- 冒泡排序,简单选择排序
- [R语言统计]秩转换的非参数检验
- linux时间编程学习
- Python1
- 关于java的深克隆与浅克隆