HDU 3555 Bomb

    xiaoxiao2021-03-26  9

    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 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.   Author fatboy_cw@WHU   Source 2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU  

    听说是一道基础的数位dp,然而看到这么大的数据范围,萌新一脸懵逼。

    听了sfailsth,dalao讲的数位dp,醍醐灌顶。

    具体来说,就是先预处理出从1位到第i位,首位是9的,没有49的和已经有49的。

    之后的询问就是分类讨论,还有一个技巧,query(n+1)。

    因为程序只能处理开区间。

    #include<iostream> #include<cstring> #include<cstdio> using namespace std; int T; long long n,dp[31][3]; long long query(long long x) { long long num[30],ans=0; int tp=0; memset(num,0,sizeof(num)); while(x) { num[++tp]=x; x/=10; } bool flg=0; for (int i=tp;i;--i) { ans+=num[i]*dp[i-1][0]; if(!flg) { if(num[i]>4) ans+=dp[i-1][1]; } else ans+=dp[i-1][2]*num[i]; if(num[i]==9&&num[i+1]==4) flg=1; } return ans; } int main() { dp[0][2]=1; for(int i=1;i<=30;i++) { dp[i][0]=dp[i-1][1]+dp[i-1][0]*10; dp[i][1]=dp[i-1][2]; dp[i][2]=dp[i-1][2]*10-dp[i-1][1]; } scanf("%d",&T); while(T--) { scanf("%lld",&n); printf("%lld\n",query(n+1)); } return 0; }

     
    转载请注明原文地址: https://ju.6miu.com/read-600083.html

    最新回复(0)