POJ - 1426 Find The Multiple 【DFS】

本文提供了一种解决POJ 1426问题的有效方法,通过深度优先搜索寻找由0和1构成且能被给定整数n整除的最大数。n不超过200,数字长度不超过100位。实际情况下,在不超过20位的情况下即可找到解。使用C++实现,代码简洁高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

http://poj.org/problem?id=1426

题意
给出一个数 要求找出 只有 0 和 1 组成的 十进制数字 能够整除 n
n 不超过 200 十进制数字位数 不超过100

思路
其实 十进制数字位数 不超过 20 下就有可以满足的答案 所以直接用 unsinged long long 就可以过了
。。

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits>

#define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back

using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss;

const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-30;

const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7;

int n;

ull ans;

void dfs(ull x, int cur)
{
    if (ans)
        return;
    if (cur > 19)
        return;
    if (x % n)
    {
        dfs(x * 10, cur + 1);
        dfs(x * 10 + 1, cur + 1);
    }
    else
    {
        ans = x;
        return;
    }
}

int main()
{
    while (scanf("%d", &n) && n)
    {
        ans = 0;
        dfs(1, 0);
        cout << ans << endl;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值