首页 > 其他分享 >AcWing 第 93 场周赛 4868. 数字替换(dfs+剪枝)

AcWing 第 93 场周赛 4868. 数字替换(dfs+剪枝)

时间:2023-03-13 21:03:55浏览次数:54  
标签:剪枝 const minn 周赛 LL dfs return sum

https://www.acwing.com/problem/content/4871/

题目大意:

给定两个整数 n,x。(x为原始数据,n为需要我们把x变成的位数)

可以对x进行任意次以下操作:

选择x的一位数字y,将x替换为x*y。

求出把x变成n位数的最小操作数,如果无法达到,输出-1.
扩展数据
输入
5 403
输出
2

有很多年度迷惑大bug,为啥LL mp[10];提成全局这个扩展数据就输出3了???
我不李姐(抓狂,这个点找了快1个小时

写法一

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=9e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,x,minn=MAXN;
void dfs(LL xx,LL sum)
{
    string sx=to_string(xx);
    //cout<<sx<<endl;
    if(sx.size()>n) return ;
    if(sx.size()==n)
    {
        minn=min(minn,sum);
        return ;
    }
    //如果当前已经积累的位数+还需要凑满的位数>=目前我已经获得过的最小值
    //就没有重复的必要了
    if(sum+n-sx.size()>=minn) return ;
    LL mp[10];
    for(int i=0;i<10;i++)
        mp[i]=0;
    for(LL i=0;i<sx.size();i++)
    {
        mp[sx[i]-'0']=1;
    }
    for(int i=9;i>1;i--)
    {
        if(mp[i]) dfs(xx*i,sum+1);
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>x;
        dfs(x,0);
        if(minn==MAXN) cout<<"-1"<<endl;
        else cout<<minn<<endl;
    }
    return 0;
}

写法二

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=9e18,MINN=-1e18;
const LL N=1e6+10,M=2023;
const LL mod=998244353;
const double PI=3.1415926535;
#define endl '\n'
LL n,x;
LL minn=MAXN;
LL get(LL num)
{
    LL res=0;
    while(num)
    {
        res++;
        num/=10;
    }
    return res;
}
void dfs(LL xx,LL sum)
{
    LL t=get(xx);
    if(t>n) return ;
    if(t==n)
    {
        minn=min(minn,sum);
        return ;
    }
    if(sum+n-t>=minn) return ;
    LL mp[10];
    for(int i=0;i<10;i++)
        mp[i]=0;
    LL flag=xx;
    while(flag)
    {
        mp[flag%10]=1;
        flag/=10;
    }
    for(int i=9;i>1;i--)
    {
        if(mp[i]) dfs(xx*i,sum+1);
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    int T=1;
    //cin>>T;
    while(T--)
    {
        cin>>n>>x;
        dfs(x,0);
        if(minn==MAXN) cout<<"-1"<<endl;
        else cout<<minn<<endl;
    }
    return 0;
}

标签:剪枝,const,minn,周赛,LL,dfs,return,sum
From: https://www.cnblogs.com/Vivian-0918/p/17212843.html

相关文章

  • 2018年东北农业大学春季校赛(周赛训练)
    题解报告题解顺序不是原来比赛的题目顺序题目意思可以去原题了解基本的一些理解和问题都在注释中题目一:wyh的矩阵//思维题,找规律,考虑中点的性质。#include<cstd......
  • 知识蒸馏、轻量化模型架构、剪枝…几种深度学习模型压缩方法
    摘要:模型压缩算法旨在将一个大模型转化为一个精简的小模型。工业界的模型压缩方法有:知识蒸馏、轻量化模型架构、剪枝、量化。本文分享自华为云社区《深度学习模型压缩方法......
  • LeetCode 周赛 336,多少人直接 CV?
    本文已收录到AndroidFamily,技术和职场问题,请关注公众号[彭旭锐]提问。大家好,我是小彭。今天早上是LeetCode第336场周赛,你参加了吗?这场周赛整体质量比较高,但是最......
  • 2023学校周赛Round1 Div1
    \(A\)拿个栈模拟一下。\(B\)推一推式子,把\((\displaystyle\sum_{i=1}^{n}a_i)^3\)展开,会得到三种类型的式子,其中两个都是可以线性求出来的,第三个的6倍就是答案。\(C\)......
  • AcWing 第 94 场周赛 A B(最短路dij) C(最短路floyed)
    https://www.acwing.com/activity/content/competition/problem_list/2961/AcWing4870.装物品水题#include<bits/stdc++.h>usingnamespacestd;typedeflonglon......
  • AcWing94场周赛复盘
    快速手速题不能犹豫已知,每个背包最多可以装件物品。请你计算,要装下件物品最少需要多少个背包。输入格式一个整数。输出格式一个整数,表示所需背包的最少数量。数据范围......
  • 6317.统计美丽子数组的数目-336周赛
    统计美丽子数组的数目给你一个下标从0 开始的整数数组nums 。每次操作中,你可以:选择两个满足 0<=i,j<nums.length 的不同下标 i 和 j 。选择一个非负整数......
  • 2020 年百度之星·程序设计大赛 - 初赛一 GPA DFS深搜
    problemGPAAccepts:1554Submissions:3947TimeLimit:2000/1000MS(Java/Others)MemoryLimit:32768/32768K(Java/Others)ProblemDescription小沃沃一共参加了......
  • dfs入门,一个简单的迷宫问题
    AcWing走迷宫问题给定一个 n×m 的二维整数数组,用来表示一个迷宫,数组中只包含 0或 1,其中 0 表示可以走的路,1 表示不可通过的墙壁。最初,有一个人位于左上角 (1,1......
  • A. Computer Game【dfs诈骗】
    A.ComputerGame代码点击查看代码#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<vector>#include<queue......