Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 6762 Accepted Submission(s): 2351
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
The input terminates by end of file marker.
數位dp模板題
#include<set>
#include<queue>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<utility>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define Inf (1<<30)
#define LL unsigned long long
#define MOD 1000000009
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
LL dp[100][100];
void init()
{
dp[0][0]=1;
for(int i=1;i<=64;i++)//dp[i][j]表示長度是i的以j開頭的沒有49的字串個數
{
for(int j=0;j<=9;j++)
{
for(int k=0;k<=9;k++)
if(!(k==9&&j==4))dp[i][j]+=dp[i-1][k];
}
}
}
LL query(LL x)
{
LL a[70],L=0,ans=0;
for(int i=1;x;i++)
{
a[i]=x%10;
x/=10;
L++;
}
a[L+1]=0;
for(int i=L,j=0;i>=1;i--)
{
for(j=0;j<a[i];j++)
{
if(!(j==9&&a[i+1]==4))ans+=dp[i][j];
}
if(a[i]==9&&a[i+1]==4)break;
}
return ans;
}
int main()
{
int T;
LL n;
init();
//freopen("D:\\oo.txt","r",stdin);
scanf("%d",&T);
while(T--)
{
scanf("%I64u",&n);
printf("%I64u\n",n+1-query(n+1));///查詢0到n,[0,n]的沒有49的個數
}
return 0;
}