红魔馆的馆主

C-红魔馆的馆主_牛客周赛 Round 37 (nowcoder.com)

思路:dfs+剪枝优化。

注意:给的值可能是 1 0 18 10^{18} 1018,如果加一位LL超了。可以用取模操作来消除溢出影响,也许也可以用int128

代码如下:

#include <bits/stdc++.h>
using namespace std;
int main()
{   
    using LL = long long;
    LL n; cin>>n;
    if(n % 495 == 0) {
        cout<<-1;
        return 0;
    }
    string ans(25, '9');
    auto dfs = [&](auto &&self,LL yu, string now) -> void {
        if(now.size() >= ans.size()) return ;
        if(yu == 0) {
            if(now.size() < ans.size()) ans = now;
            return ;
        }
        for(int i = 0; i < 10; ++i) {
            string tmp = now + char(i + '0');
            LL ny = (yu * 10 + i) % 495;
            self(self,ny, tmp);
        }
    };
    dfs(dfs,n % 495, to_string(n));
    cout<<ans.substr(to_string(n).size());
}

相关推荐

  1. 2024-03-20 09:02:05       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-20 09:02:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-20 09:02:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-20 09:02:05       87 阅读
  4. Python语言-面向对象

    2024-03-20 09:02:05       96 阅读

热门阅读

  1. SQL-存储过程介绍

    2024-03-20 09:02:05       49 阅读
  2. 未来之路:Python PDF处理技术的革新

    2024-03-20 09:02:05       44 阅读
  3. 使用 pypdf 快速切分 PDF 文件

    2024-03-20 09:02:05       48 阅读
  4. 信息学奥赛之C++中的数据类型数据结构

    2024-03-20 09:02:05       48 阅读
  5. 比特币,区块链及相关概念简介(一)

    2024-03-20 09:02:05       48 阅读