标签:
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.
#include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<climits> #include<list> #include<iomanip> #include<stack> #include<set> using namespace std; typedef long long ll; ll dp[20][3]; ll work(string s) { int len=s.length(); ll ans=0; bool flag=0; for(int i=0;i<len;i++) { if(flag) ans+=(dp[len-i-1][0]+dp[len-i-1][2])*(s[i]-'0'); else { ans+=dp[len-i-1][2]*(s[i]-'0'); if(s[i]>'4') ans+=dp[len-i-1][1]; } if(!flag&&i>0&&s[i-1]=='4'&&s[i]=='9') flag=1; } if(flag) ans++; return ans; } int main() { dp[0][0]=1; for(int i=1;i<19;i++) { dp[i][0]=dp[i-1][0]*10-dp[i-1][1]; dp[i][1]=dp[i-1][0]; dp[i][2]=dp[i-1][2]*10+dp[i-1][1]; } int T; cin>>T; while(T--) { string s; cin>>s; cout<<work(s)<<endl; } }
标签:
原文地址:http://blog.csdn.net/stl112514/article/details/45064519