标签:
Description
Input
Output
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\n",solve(n)); } //system("Pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5932605.html