HDU 3555(数位dp)
来源:互联网 发布:mac如何设置语言 编辑:程序博客网 时间:2024/06/09 15:53
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 19861 Accepted Submission(s): 7380
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?
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.
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题意:很简单:找出从1到n中含有49的数字的个数。思路: 暴力不可以,所以考虑数位dp。第一种做法://dp【i】【j】表示长度为i 开头为j的不含有49的个数。#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;long long dp[21][10];void init(){int i,j,k;memset(dp,0,sizeof(dp));dp[0][0]=1;for(i=1;i<=20;i++){for(j=0;j<10;j++){for(k=0;k<10;k++){if(!(j==4&&k==9)) dp[i][j]+=dp[i-1][k];}}}return ;}long long solve(long long n){long long m;m=n;int num[21];int i,j,k;memset(num,0,sizeof(num));int cnt=0;while(n){num[++cnt]=n%10;n/=10;}long long ans=0;for(i=cnt;i>=1;i--){for(j=0;j<num[i];j++){if(!(j==9&&num[i+1]==4)) ans+=dp[i][j];}if(num[i]==9&&num[i+1]==4) break;}return m-ans;}int main(){init();int t;cin>>t;long long n;while(t--){cin>>n;cout<<solve(n+1)<<endl;}return 0;}
第二种做法:更详细的思路:粘一下一位大佬的博客:点击打开链接#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespace std;long long dp[21][3];//dp[i][j]表示长度为j==0表示长度为i不含49的个数//j==1表示长度为i含有前缀9的个数//j==2表示长度为i含有49的个数 void init(){memset(dp,0,sizeof(dp));int i,j,k;dp[0][0]=1;for(i=1;i<20;i++){dp[i][0]=dp[i-1][0]*10-dp[i-1][1];//-dp[i-1][1]是因为如果第i-1位为9的话,i位为4则可以组成含49的数。 dp[i][1]=dp[i-1][0];//只要长为i的数字没有49 那么第i位是可以为9. dp[i][2]=dp[i-1][2]*10+dp[i-1][1]; //要想长为i的数字中含有49 第一是 之前的数字中含有49 第二是前缀为9,那么第i位为4就可以。 }return ;}long long solve(long long n){int i,j;int num[20];int cnt=0;memset(num,0,sizeof(num));while(n){num[++cnt]=n%10;n/=10;}long long fin=0;int f=0;for(i=cnt;i>=1;i--){fin+=num[i]*dp[i-1][2];if(f) fin+=num[i]*dp[i-1][0];if(!f&&num[i]>4){fin+=dp[i-1][1];}if(num[i]==9&&num[i+1]==4) f=1;}return fin;}int main(){init();int t;scanf("%d",&t);long long n;while(t--){scanf("%I64d",&n);printf("%I64d\n",solve(n+1));}return 0;}
dfs搜索做法(此算法粘的某位同学的。也比较简单):#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<queue>#include<algorithm>#include<stack>using namespace std;typedef long long ll;ll dp[40][2];int dis[30];ll dfs(int len,int pre,int lim){ if(len<0) return 1; if(!lim&&dp[len][pre]!=-1) return dp[len][pre]; ll res=0; int u=lim?dis[len]:9; for(int s=0; s<=u; s++) { if(!(pre&&s==9)) { res+=dfs(len-1,s==4,lim&&s==u); } } if(!lim) return dp[len][pre]=res; return res;}ll n;ll solve(ll u){ int len=0; while(u) { dis[len++]=u%10; u/=10; } return dfs(len-1,0,1);}int main(){ int T; scanf("%I64d",&T); while(T--) { scanf("%I64d",&n); memset(dp,-1,sizeof(dp)); printf("%I64d\n",n-(solve(n)-1)); } return 0;}
阅读全文
0 0
- hdu 3555 数位dp
- hdu 3555 数位dp
- HDU 3555 数位DP
- HDU 3555 \数位DP
- hdu 3555 数位DP
- HDU 3555 数位DP
- hdu 3555 数位DP
- hdu 3555 数位dp
- 数位DP HDU-3555
- HDU 3555 数位DP
- hdu 3555(数位dp)
- hdu 3555(数位dp)
- hdu 3555(数位dp)
- HDU 3555 数位DP
- hdu 3555【数位DP】
- hdu 3555(数位dp)
- HDU 3555 数位DP
- hdu 3555 数位dp
- 背包九讲之0-1背包问题 滑动数组实现 Java实现
- 常用的数据库查询语句
- find locate whereis which type
- jQuery中获取三个高度方法
- java设计模式
- HDU 3555(数位dp)
- POJ – 1503 Integer Inquiry Integer Inquiry
- weka源码调试libSVM出现libsvm classes not in CLASSPATH
- HTML-图片、音频、视频链接
- 正则表达式
- Java IO流结构图 -- JAVA 基础
- Generative Adversarial Nets(译)
- 浏览器以下载方式打开一张图片
- 1127. ZigZagging on a Tree (30)