首页 > 其他分享 >Bomb(数位DP)

Bomb(数位DP)

时间:2024-08-23 23:16:11浏览次数:4  
标签:typedef return sequence int ll pos Bomb DP 数位

题目描述
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?
输入
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.
输出
For each test case, output an integer indicating the final points of the power.
样例输入 Copy
3
1
50
500
样例输出 Copy
0
1
15
提示
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.

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef vector<string> VS;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
inline int log_2(int x) {return 31-__builtin_clz(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int lowbit(int x) {return x&-x;}
ll f[20][2],b[20];
ll dfs(int pos,bool is_4,bool is_max)
{
    if(!pos) return 1;
    if(!is_max&&(~f[pos][is_4])) return f[pos][is_4];
    ll ans = 0;
    int m = is_max?b[pos]:9;
    for(int i=0;i<=m;++i)
    {
        if(is_4&&i==9) continue;
        ans += dfs(pos-1,i==4,is_max&&i==m);
    }
    if(!is_max) f[pos][is_4] = ans;
    return ans;
}
//dig_dp搜索的是从0到x的合法解
ll dig_dp(ll x)
{
    int len = 0;
    //注意此处的b数组写成while
	while(x) b[++len] = x%10,x/=10;
	return dfs(len,0,1);
}
void solve()
{
    ll n;
    cin>>n;
    cout<<n+1-dig_dp(n)<<'\n';
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    memset(f,-1,sizeof f);
    int T;
    cin>>T;
    while(T--)
    {
        solve();
    }
}

标签:typedef,return,sequence,int,ll,pos,Bomb,DP,数位
From: https://www.cnblogs.com/ruoye123456/p/18377225

相关文章

  • 半回文串(dp套dp)
    第4题   半回文串 查看测评数据信息给定一个长度为n的只含小写英文字母的字符串S和一个整数k,请你将S分成k个子字符串,使得每个子字符串变成半回文串需要修改的字符数目最少。请你返回一个整数,表示需要修改的最少字符数目。下面定义什么事半回文串:如果一个字符串从左往右......
  • 线性dp:编辑距离
    编辑距离本题与力扣72.编辑距离题意一样,阅读完本文可以尝试leetcode72.力扣题目链接题目叙述输入两个字符串a,b。输出从字符串a修改到字符串b时的编辑距离输入NOTVLOVER输出4题目解释:动态规划思路这个问题显然是一个最优解问题,我们可以考虑动态规划的思路,那么我......
  • 计算机网络——TCP协议与UDP协议详解(下)
    一、TCP协议1.1TCP协议的报文TCP全称为"传输控制协议(TransmissionControlProtocol")。人如其名,要对数据的传输进行一个详细的控制。我们先看其报文格式,如下图:TCP报文由以下几个字段组成:源端口号和目标端口号:每个TCP连接都有一个源端口号和一个目标端口号。源端口号......
  • 代码实现WordPress主动推送及自动推送至百度搜索收录
    站长们辛辛苦苦写的文章,无非就是让百度收录,也可以帮助人,也可以给自己站或者帮人优化的站带来流量,今天就来发一篇关于wordprss主动推送给百度的方法;使用方法,U8格式放在wp当前模板functions.php里即可12345678910111213141516171819202122232425262......
  • 阿里dataworks通过pyodps 3获取表元数据及质量稽核
    用途:本脚本的主要作用就是获取所属工作空间中表字段信息核心脚本:本逻辑主要需要五个核心脚本:00_task_meta_setup_time#用于创建表及设置odps的启动时间01_task_meta_fields_move#搬迁数据02_task_meta_tables#表元数据获取及数据量统计03_task_meta_fields_parallel......
  • QPointer、QScopedPointer、QSharedPointer、QWeakPointer
    QPointer、QScopedPointer、QSharedPointer、QWeakPointerQSharedPointer:std::shared_ptrQWeakPointer:std::weak_ptrQScopedPointer:std::unique_ptrQPointer:无STL等效项。QObject析构时为空。QPointer功能:一个“半自动”的指针包装器。通常情况下,我们在手动delete一......
  • 遷移Wordpress到新域名,新子域名
    1.0前言把Wordpress遷移到WordpressMultiSites的子域名,因此“All-in-OneWPMigrationandBackup”就需要付費VIP才支持遷移子域名。但用手動方法也可以實現遷移到子域名。延伸文章:Wordpress主題文章wordpress更改domain域名和數據庫連接2.0 “All-in-OneWPMigratio......
  • 线性dp:最长公共子序列
    最长公共子序列本文讲解的题与leetcode1143.最长公共子序列这题一样,阅读完可以挑战一下。力扣题目链接题目叙述:给定两个字符串,输出其最长公共子序列,并输出它的长度输入:ADABEC和DBDCA输出:DBC3解释最长公共子序列是DBC,其长度为3动态规划思路:我们这题先构建一个模......
  • 线性dp:最长公共子串
    最长公共子串本文讲解的题与leetcode718.最长重复子数组,题意一模一样,阅读完本文以后可以去挑战这题。力扣链接题目叙述:给定两个字符串,输出其最长公共子串的长度。输入ABACCBAACCAB输出3解释最长公共子串是ACC,其长度为3。与最长公共子序列的区别公共子串:字符必须......
  • dp
    前情提要dp专题复习树形dp其实对我来说树形dp会比序列dp学得好一些,因为树是有一个具体形态的东西,推式子是比较具象的。其实序列就是把树拍平在数轴上去dp的,只要考虑到这一点,画出dp的转移图,式子就可以呼之欲出了。不套路的dp考验人类智慧的时刻到了!P1352没有上司......