题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555
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 <iostream> #include <stdio.h> #include <string.h> #include <string> #include <cstdio> #include <cmath> typedef long long ll; using namespace std; ll dp[22][4]; ll digit[22]; void sove() { dp[0][0]=1; for(int i=1;i<21;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 main() { ll T,n; cin>>T; sove(); while(T--) { scanf("%I64d",&n); memset(digit,0,sizeof(digit)); int len=0; while(n) { digit[++len]=n%10; n/=10; } int flag=0,last=0; ll cnt=0; for(int i=len;i>=1;i--) { cnt+=dp[i-1][2]*digit[i]; if(flag) cnt+=dp[i-1][0]*digit[i]; if(!flag&&digit[i]>4) cnt+=dp[i-1][1]; if(last==4&&digit[i]==9) flag=1; last=digit[i]; } if(flag)cnt++; printf("%I64d\n",cnt); } return 0; }
原文地址:http://blog.csdn.net/liusuangeng/article/details/38822249