Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 7487 Accepted Submission(s): 2610
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.
1.dp[len][0] 代表數字長度爲len不含49的個數
2.dp[len][1] 代表數字長度爲len不含49但是以9開頭的個數(顯然dp[len][1]包含在dp[len][0]中)
3.dp[len][2] 代表數字長度爲len含有49的個數
AC代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;
const int maxn = 3005;
const int INF = 1e9;
const int mask = 0x7fff;
ll dp[25][3];
ll digit[25];
void init(){
memset(dp, 0, sizeof(dp));
dp[0][0] = 1;
for(int i = 1; i < 21; i++)
{
dp[i][0] = 10 * dp[i - 1][0] - dp[i - 1][1]; //在前面添上0~9,要減去4加到9前面這種情況
dp[i][1] = dp[i - 1][0];
dp[i][2] = 10 * dp[i - 1][2] + dp[i - 1][1];
}
}
int main()
{
init();
int t;
ll n;
scanf("%d", &t);
while(t--)
{
scanf("%I64d", &n);
int len = 1;
memset(digit, 0, sizeof(digit));
while(n)
{
digit[len++] = n % 10;
n /= 10;
}
ll ans = 0;
bool flag = 0;
for(int i = len - 1; i >= 1; i--)
{
ans += dp[i - 1][2] * digit[i];
if(flag) ans += digit[i] * dp[i - 1][0];
else if(digit[i] > 4) ans += dp[i - 1][1];
if(digit[i + 1] == 4 && digit[i] == 9) flag = 1;
}
if(flag) ans++;
printf("%I64d\n", ans);
}
return 0;
}