HDU 3555 数位DP
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
//#define __int64 LL
LL dp[22][3];
int pos[22];
//[][0] 包含49
//[][1] 最高位是9 但不含49
//[][2] 不含49
void initial()
{
dp[0][2]=1;
for(int i = 1; i < 22; i++)
{
dp[i][0] = dp[i-1][0] * 10 + dp[i-1][1];
dp[i][1] = dp[i-1][2];
dp[i][2] = dp[i-1][2] * 10 - dp[i-1][1];
}
}
LL ans(LL a)
{
int i,cnt = 0;
LL res = 0;
memset(pos,0,sizeof(pos));
a++;
while(a>10)
{
pos[cnt++] = a % 10;
a /= 10;
}
pos[cnt] = a;
bool flag = false;
for(i = cnt; i >= 0; i--)
{
//cout<<dp[i][0]<<" "<<dp[i][1]<<" "<<dp[i][2]<<endl;
res += pos[i]*dp[i][0];
if(flag)
res += dp[i][2] * pos[i];
if(!flag && pos[i] > 4 )
res += dp[i][1];
if(pos[i+1] == 4 && pos[i] == 9)
flag = true;
}
return res;
}
int main()
{
int n;
cin>>n;
initial();
while(n--)
{
LL a;
cin>>a;
cout<<ans(a)<<endl;
}
return 0;
}
没什么好说的水题一枚...虽然我也不是很会做也是看题解才懂得
第一题数位DP...mark一下咯

浙公网安备 33010602011771号