Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28550   Accepted: 11828   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

Source

 
原题大意:找出任意一个只由0与1组成的数,使得其为n的倍数。
 
解题思路:问题在于对问题的优化。100位数字太长了(当然,实际到不了100位,最高19位左右),若是找余数的状态,又很难数清有多少种。于是可以先来一个裸的搜索先判最长位数,然后用深搜或广搜解题。
 
#include<stdio.h>
int n,ans[400];
bool found;
void dfs(int mod,int dep)
{
if(found || dep>19) return;
if(mod==0)
{
for(int i=0;i<=dep;++i) printf("%d",ans[i]);
printf("\n");
found=true;
return;
}
ans[dep+1]=1;
dfs((mod*10+1)%n,dep+1); //mod的运算规律,可以这样想:假设X为当前位以前所有的数,那么当前数为X*10+i(i=0/1),对其取n的余数,(X%n*10%n+i%n)%n,递推即可。
ans[dep+1]=0;
dfs((mod*10)%n,dep+1);
}
int main()
{
int i;
while(~scanf("%d",&n))
{
if(n==0) break;
found=false;
ans[0]=1;
dfs(1%n,0);
}
return 0;
}

  

              

[深度优先搜索] POJ 1426 Find The Multiple的更多相关文章

  1. POJ 1426 Find The Multiple --- BFS || DFS

    POJ 1426 Find The Multiple 题意:给定一个整数n,求n的一个倍数,要求这个倍数只含0和1 参考博客:点我 解法一:普通的BFS(用G++能过但C++会超时) 从小到大搜索直至 ...

  2. 广搜+打表 POJ 1426 Find The Multiple

    POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Ac ...

  3. POJ 1426 Find The Multiple(寻找倍数)

    POJ 1426 Find The Multiple(寻找倍数) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 Given ...

  4. POJ.1426 Find The Multiple (BFS)

    POJ.1426 Find The Multiple (BFS) 题意分析 给出一个数字n,求出一个由01组成的十进制数,并且是n的倍数. 思路就是从1开始,枚举下一位,因为下一位只能是0或1,故这个 ...

  5. DFS/BFS(同余模) POJ 1426 Find The Multiple

    题目传送门 /* 题意:找出一个0和1组成的数字能整除n DFS:200的范围内不会爆long long,DFS水过~ */ /************************************ ...

  6. POJ 1426 Find The Multiple (DFS / BFS)

    题目链接:id=1426">Find The Multiple 解析:直接从前往后搜.设当前数为k用long long保存,则下一个数不是k*10就是k*10+1 AC代码: /* D ...

  7. POJ - 1426 Find The Multiple(搜索+数论)

    转载自:優YoU  http://user.qzone.qq.com/289065406/blog/1303946967 以下内容属于以上这位dalao http://poj.org/problem? ...

  8. POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2

    http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...

  9. poj 1426 Find The Multiple (bfs 搜索)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18012   Accepted: 729 ...

随机推荐

  1. Mac下遇到 'reading initial communication packet’ 问题

    今天在开发过程中,一个单位跑的好好的项目,在家中的Mac下运行时,遇到了下面这个错误:   "Lost connection to MySQL server at 'reading init ...

  2. 特殊符号 && 和 ||

    一.值为false的情况 如果逻辑对象值为0,-0, null,undefined,false,"",NaN.那么值为false. 二.&& || 的 理解 1.& ...

  3. CVE-2016-4758: UXSS in Safari's showModalDialog

    I would like to share about details of Safari's UXSS bug(CVE-2016-4758). This bug was fixed in Safar ...

  4. fzu1036四塔问题(汉诺塔问题拓展)

    #include<iostream> #include<cstdio> #include<cmath> using namespace std; ]; int ru ...

  5. 从C++到GO

    从C++到GO 刚开始接触Go语言,看了两本Go语言的书,从c++开发者的角度来看看go语言的新特性,说下自己感触较深的几点: 并发编程 Go语言层面支持协程,将并发业务逻辑从异步转为同步,大幅提高开 ...

  6. SVN使用手册

    安装Tortoise SVN Icon TortoiseSVN 1.7版本及之后与之前的版本有很大的变化,主要区别如下: 1.7以前的版本会在每个目录中生成一个.svn的隐藏目录.1.7及以后的版本, ...

  7. iOS - AppStores App 上架

    前言 1.准备 开发者账号 完工的项目 2.上架步骤 1) 创建 App ID 2) 创建证书请求文件(CSR文件) 3) 创建发布证书(CER) 4) 创建 Provisioning Profile ...

  8. JQuery_表单选择器

    表单作为HTML 中一种特殊的元素,操作方法较为多样性和特殊性 开发者不但可以使用之前的常规选择器或过滤器,也可以使用jQuery 为表单专门提供的选择器和过滤器来准确的定位表单元素. 一.常规选择器 ...

  9. 【转】 C++的精髓——虚函数

    虚函数为了重载和多态的需要,在基类中是由定义的,即便定义是空,所以子类中可以重写也可以不写基类中的函数! 纯虚函数在基类中是没有定义的,必须在子类中加以实现,很像java中的接口函数! 虚函数 引入原 ...

  10. 本地jsp连到服务器上的sqlserver

    1.首先通过远程桌面连接到自己的服务器 方法很简答,打开远程桌面连接,然后输入服务器ip(外网ip),然后输入登录名和密码. 可以参考腾讯云给的教程:http://bbs.qcloud.com/for ...