Find The Multiple(搜索)

POJ1426-Find The Multiple【BFS+同余模】 北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码 立即下载
Find The Multiple
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 35088 Accepted: 14644 Special Judge

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

Input

The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

Output

For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

Sample Input

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111
题意:就是给定一个数,找到一个只由0,1组成的数并且可以整除给定的数
思路:直接一个搜索就可以,操作就有两个,一个是*10,另个是*10+1,这道题深搜广搜都是可以的
方法1:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n;
int solve;//标记量,在递归中,如果solve为1了,说明之前已经找到了符合条件的不用继续递归下去了
void DFS(unsigned long long num,int k){
    if(solve==1)return;
    if(num%n==0){
        printf("%llu\n",num);
        solve = 1;
        return;
    }
    if(k==19)return;//题目要求不超100位,如果没有这句话可能会一直递归下去,直到溢出
    //但是为什么前面已经用solve标记了还会继续递归呢,我觉得可能是这里有两个递归,如果solve等于1,返回到solve为零的情况又去下一个递归了而另一个递归可能没有答案
    DFS(num*10,k+1);//因为题目要求只能由0,1组成,所以对于寻找的方向就两个乘10
    DFS(num*10+1,k+1);//或者乘10+1,然后判断能否整除就停止
}
int main(){
    while(~scanf("%d",&n)&&n){
        solve = 0;
        DFS(1,0);//深搜从1开始
    }
    return 0;
}

方法2:是一种循环代替搜索的思路,而且很巧妙,具体解释看代码注释
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
long long mod[1000000];
int main(){
    int n;
    while(~scanf("%d",&n)&&n){
        int i;
        for(i = 1;; i++){//这里要从一开始,为什么看下面
            mod[i] = mod[i/2]*10+i%2;//这时关键语句,实际上和上一个代码的深搜是一个道理,只不过用循环代替了,很巧妙,我们来分析一下
            //因为每次只有两个操作,所以第i个数,应该由第i/2个数*10或者*10+1得到,因为每次都要乘十,所以每次都乘,只不过就是加一或者不加的问题
            //这里就巧妙用到下标奇偶性质,奇数模2为1,就相当与加一,而偶数相当于不加,所以下标从1开始是方便的,你可能问开始的两个加一或不加会不会出现零,因为题目要求不能出现
            //零,不会,因为第一个是1,第二个发现虽然不加1但是2/2=1了所以第二个元素还是1,所以从下标1开始很神奇!
            if(mod[i]%n==0){
                printf("%lld\n",mod[i]);
                break;
            }
        }
    }
    return 0;
}


[记录一个bug]av_hwdevice_ctx_create出现Cannot allocate memory(附带库运行时路径)[已解决] 新写的程序里调用ffmpeg的av_hwdevice_ctx_create()这个函数,出现Cannot allocate memory。但是另个程序一样的代码,居然是成功的。 反复检查之后无果,就跳出去查了一下,看到一个问答: Have you tried to use ffmpg executables with the same libs that you using when you run the app? Have you tried to use ffmpg executables 阅读详情

相关推荐

RK3566/RK3588 NPU性能实测:用mobilenet模型快速验证你的开发板NPU是否正常工作

本文提供了RK3566/RK3588开发板NPU性能实测指南,通过MobileNet模型快速验证NPU是否正常工作。详细介绍了环境搭建、测试资源获取、编译运行及结果分析的全流程,帮助开发者高效验证神经网络加速器功能,为后续AI应用部署奠定基础。

weixin_28826521的博客 447

Given a positive integer n....

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there...

大魚的个人博客,如有不足,欢迎指正。 2464

国产SR8201F以太网PHY芯片调试避坑指南:从机贴失败到杜邦线救场的实战记录

本文详细介绍了国产SR8201F以太网PHY芯片的调试避坑指南,涵盖硬件设计、PCB布局、SMT贴片及软件驱动等关键环节。通过实战案例解析,帮助工程师解决MII/RMII接口配置、电源设计、信号完整性等典型问题,提升以太网通信稳定性与EMC性能。

c6d7e8f9g的博客 1212

E - Find The Multiple 两种选项的搜索

Find The Multiple 题意:给定不大于200的数n,找出它的任意一个仅有0和1构成的且是它的倍数的十进制数字,这个数字位数不大于100 不要被100位唬住了,这题的答案范围没超过long long(也就是10e18多一点),也就是说没超过19位数字…… 既然如此,说明可以直接用%判断是否为n的倍数。仅需考虑如何得到仅由0和1构成的数字 从1开始,1,10,11,100,101,...

我于万物之中 979

Unity开发微信小游戏,使用Addressables管理游戏资源(本地进行远程测试)

快速构建Unity微信小游戏 使用Addressables管理游戏资源 使用小皮助手在本地进行远程资源加载测试

the_vnas的博客 6389

Android perfetto 简介

android linux 性能优化 原理及操作

求变,从思想开始 3474

简单搜索--DFS--Find The Multiple

简单搜索–DFS–Find The Multiple Description 给定一个正整数n,编写一个程序来找出一个n的非零倍数m,它的十进制表示法仅包含数字0和1。你可以假设n不大于200,并且对应的m不包含100小数位数。 Input 输入文件可能包含多个测试用例。每一行包含一个值n(1<=n<=200)。包含零的行结束输入。 Output 对于输入中的n个值,打印一行,其中包含...

wardel的博客 543

GNSS - PPP软件 - GAMP 在VS2019/2022下完成调试、跑通程序(超详细!)

WIN32;_DEBUG;_CONSOLE;O_WARNINGS;ENAGLO;ENACMP;ENAGAL;ENAQZS;NFREQ=3。

qq_43381998的博客 8246

Find The Multiple

传送门 题面: Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45740 Accepted: 19128 Given a positive integer n, write a program to find out a nonzero multiple m of n whose d...

都率的博客 285

【Unity3D】RTS Engine 笔记一

RTS Engine 中配置一个新场景, 初步认识GameManager、设置地形以及配置地形网格搜索

ViYeye的博客 1648

车载以太网-组播

车载以太网-组播

天赐好车的博客 251

Find The Multiple(简单搜索)

Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more tha

qq_48627750的博客 520

《企业AI能力地图构建:AI应用架构师的关键秘籍》

构建企业AI能力地图是企业在AI时代实现战略转型和创新发展的关键举措,它需要从评估企业当前AI能力出发,全面了解技术储备、数据资产和人才队伍的现状。将企业业务战略转化为具体的AI应用场景,并进行优先级排序,是确保AI应用能够为企业带来实际价值的重要步骤。通过深入理解业务战略和挖掘应用场景,企业可以找准AI应用的切入点,提高投资回报率。

AI架构全栈开发实战笔记 631

[搜索] Find the multiple

E - Find The Multiple Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u SubmitStatus Description Given a positive integer n, write a program to find out a nonzero multipl

童凌的技术博客 709

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

poj -- 1426 Find The Multiple (搜索)

题目连接:http://poj.org/problem?id=1426Find The MultipleTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8420 Accepted: 3443 Special JudgeDescriptionGiven a positive integer n, write a program to find out a nonzero multiple m of n whose decimal rep

蓝色污点的专栏 1471

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Find The Multiple+搜索

#include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; LL n; //题目要求每一位的数字是1或者0; //那么问题是如何寻找呢? //搜索树的如何画呢? 1->10,1->11;10->100,10-&...

地衣的博客——qdu 177

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【简单搜索】Find The Multiple

Find The Multiple 题目描述 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not gr...

Mark_Eleven的学习笔记 637

简单搜索 Find The Multiple

  Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and th...

脑白金刚的博客 351

搜索--Find The Multiple

Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35088 Accepted: 14644 Special Judge Description Given a positive integer n, write a program to find out a nonzero mul...

dajiangyou123456的博客 201

搜索专题训练 D Find The Multiple

题目:Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200...

b03188460的博客 219

简单搜索 - Find The Multiple

Description: Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more th

weixin_43856210的博客 146

【搜索】 Find The Multiple

#include<stdio.h> #include<stdlib.h> #include<string.h> bool found; void DFS(unsigned __int64 t ,int n,int k) { if(found) return ;//如果已经发现了答案就没搜的必要了 if(t%n=...

weixin_30776863的博客 99

poj 1426 Find The Multiple (bfs 搜索)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 7297   Special Judge Description Given a positive integer n, write a pr

Just do it ! 1787

【广度优先搜索】Find the Multiple

题目重述 给出一个正整数nnn,找到它的最小非零正整数mmm(仅含有0和1),这个数mmm是nnn的倍数,这个nnn不超过200,并且mmm不超过100位数。 输入 每一行输入一个范围内的nnn,直到输入0时结束程序 输出 每行输出一个mmm 题解 首先,这个mmm不超过100位,可以是很大很大的一个数,所以我们从小开始遍历可能的取值,如果能在非高精度运算的话,longlonglong longl...

我的技术栈碎碎念空间 656

Find The Multiple——简单搜索+大胆尝试

【题目描述】 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 an...

T_T233333333的博客 216

poj 1426 Find The Multiple(搜索)

A - Find The Multiple Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu Submit Status Practice POJ 1426 Description Given a positive integer n, write a progr

K键盘里的青春K 609
上一篇: lower_bound和upper_bound算法
下一篇: Divided Land
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值