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
Hint
From 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.
数位dp,找子序列‘49’
记一下有没有出现过'4',是否已经出现过"49"
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 LL n,len; 26 LL f[63][10][2]; 27 int d[63]; 28 inline LL dfs(int now,int dat,int sat,int fp) 29 { 30 if (now==1)return sat; 31 if (!fp&&f[now][dat][sat]!=-1)return f[now][dat][sat]; 32 LL ans=0,mx=(fp?d[now-1]:9); 33 for (int i=0;i<=mx;i++) 34 { 35 if (sat||!sat&&dat==4&&i==9)ans+=dfs(now-1,i,1,fp&&mx==i); 36 else ans+=dfs(now-1,i,0,fp&&mx==i); 37 } 38 if (!fp&&f[now][dat][sat]==-1)f[now][dat][sat]=ans; 39 return ans; 40 } 41 inline LL calc(LL x) 42 { 43 LL xxx=x; 44 len=0; 45 while (xxx) 46 { 47 d[++len]=xxx%10; 48 xxx/=10; 49 } 50 LL sum=0; 51 for (int i=0;i<=d[len];i++) 52 sum+=dfs(len,i,0,i==d[len]); 53 return sum; 54 } 55 int main() 56 { 57 memset(f,-1,sizeof(f)); 58 int T=read(); 59 while (T--)n=read(),printf("%lld\n",calc(n)); 60 }
——by zhber,转载请注明来源
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· DeepSeek 解答了困扰我五年的技术问题。时代确实变了!
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!