Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 13181 Accepted Submission(s): 4725
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
3 1 50 500
Sample Output
0 1 15
求1~n中有多少个数中没有49连续子序列的。
思路:
比较简单的数位dp。dp[len][w][is4]表示第len位的时候,之前是否存在49,并且之前的数字是否为4.
dp[len][w][is4] = sum(dp[len-1][fw][fis4]);
#include<set> #include<map> #include<queue> #include<stack> #include<cmath> #include<string> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define INF 1000000001 #define MOD 1000000007 #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define pi acos(-1.0) using namespace std; const int MAXN = 50; ll dp[MAXN][2][2]; int digit[MAXN]; char s[MAXN]; ll dfs(int len,int w,int ismax,int pa,int is4) { if(len == 0)return w ? 1 : 0; if(!ismax && dp[len][w][is4])return dp[len][w][is4]; int maxv = ismax ? digit[len] : 9; ll ans = 0; for(int i = 0; i <= maxv; i++){ if(pa == 4 && i == 9){ ans += dfs(len-1,1,ismax && i == maxv,i,i == 4); } else { ans += dfs(len-1,w,ismax && i == maxv,i,i == 4); } } if(!ismax)dp[len][w][is4] = ans; return ans; } void solve() { int slen = strlen(s); int len = 0; for(int i = slen - 1; i >= 0; i--){ digit[++len] = s[i] - '0'; } memset(dp,0,sizeof(dp)); printf("%lld ",dfs(len,0,1,-1,0)); } int main() { int t; scanf("%d",&t); while(t--){ scanf("%s",s); solve(); } return 0; }
Bomb
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 13181 Accepted Submission(s): 4725
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
3 1 50 500
Sample Output
0 1 15
求1~n中有多少个数中没有49连续子序列的。
思路:
比较简单的数位dp。dp[len][w][is4]表示第len位的时候,之前是否存在49,并且之前的数字是否为4.
dp[len][w][is4] = sum(dp[len-1][fw][fis4]);