Bomb
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
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
分析:数位dp;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define Lson L, mid, ls[rt] #define Rson mid+1, R, rs[rt] const int maxn=1e5+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int m,k,t,d[20]; ll n,dp[20][3]; ll dfs(int now,int ca,int ok) { if(now<0)return ca==2; if(ok&&dp[now][ca]!=-1)return dp[now][ca]; int len=ok?9:d[now]; ll ans=0; for(int i=0;i<=len;i++) { if(ca==0) { if(i==4)ans+=dfs(now-1,1,ok||i<len); else ans+=dfs(now-1,0,ok||i<len); } else if(ca==1) { if(i==9)ans+=dfs(now-1,2,ok||i<len); else if(i==4)ans+=dfs(now-1,1,ok||i<len); else ans+=dfs(now-1,0,ok||i<len); } else if(ca==2) { ans+=dfs(now-1,2,ok||i<len); } } if(ok)dp[now][ca]=ans; return ans; } ll solve(ll p) { int num=0; memset(dp,-1,sizeof(dp)); while(p) { d[num++]=p%10; p/=10; } return dfs(num-1,0,0); } int main() { int i,j; scanf("%d",&t); while(t--) { scanf("%lld",&n); printf("%lld ",solve(n)); } //system("Pause"); return 0; }