3 1 50 500
0 1 15HintFrom 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
含49的个数。
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #define N 1010 #define ll long long using namespace std; ll dp[30][3];//0:合法 1:9开头 2:不合法 ///dp[i][j] 长度为i的j状态 void init() { memset(dp,0,sizeof dp); dp[0][2]=1; for(int i=1; i<=24; 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 solve(ll n) { int a[40]; int len=1; while(n) { a[len++]=n%10; n/=10; } a[len]=0; ll ans=0; int flag=0; for(int i=len-1; i>=1; i--) { ans+=dp[i-1][0]*a[i]; if(flag) ans+=dp[i-1][2]*a[i]; if(!flag&&a[i]>4) ans+=dp[i-1][1]; if(a[i]==9&&a[i+1]==4) ///xx49***的情况 *为待填数位 flag=1; } return ans; } int main() { init(); int t; cin>>t; while(t--) { ll n; cin>>n; printf("%I64d\n",solve(n+1)); } return 0; }
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/45223707