火炬(dfs)

该文章展示了洛谷平台上的一道编程题目,主要涉及枚举算法和深度优先搜索(DFS)来构造01串,并解决特定条件下的数论问题。程序代码中定义了各种数据类型和辅助函数,通过输入数字n,找出所有能被n整除的01串。
摘要由CSDN通过智能技术生成

P1988 火炬 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

搜索 枚举01串
#include <bits/stdc++.h>
#include <iostream>
#include<unordered_map>
#define x first
#define y second
#define  ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);

using namespace std;

typedef  long  long LL ;
typedef  unsigned long  long ULL ;
typedef pair<int,int> PII ;
typedef pair<LL,int> PLI ;
const int N = 1e6 +  10 ,M=1000 + 10;
const LL INF2 = 1e17;


LL n,tot,a[N];

void dfs(LL x )
{
    a[++ tot ] = x;
    if(x > 1e18) return;
    //构造一个01串
    for(int i=0;i<=1;i ++ )//下一位数是0还是1
    dfs(x * 10 + i);

}

inline void solve()
{
    cin >> n;
    dfs(1);// 最高位必须是1
    sort(a+1,a+tot+1);

    for(int i=1;i<=tot; i++)
        if(a[i] % n == 0 )
        {
            cout << a[i]/n;
            return;
        }
    cout <<"No Solution\n";
}
signed main()
{
    ios
    int T=1;
//    cin>>T;
    while(T -- )
    {
        solve();

    }


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值