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

  1. 2
  2. 6
  3. 19
  4. 0

Sample Output

  1. 10
  2. 100100100100100100
  3. 111111111111111111

Source

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

  

              

[深度优先搜索] 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. Cocos2d-x 核心概念 - 导演(Director)

    导演类(Director) 用于管理场景对象,采用的是单例模式(单例模式能保存一致的配置信息,方便管理场景对象) 获得导演实例的语句如下 local director = cc.Director:ge ...

  2. iphone中 input圆角bug

    今天写了个简单的登录注册,在电脑手机(除了iphone)样式都没有问题,但在iphone中却出现了异常,提交的按钮变成圆角被背景渐变的效果,随后又测试两个iphone版都是一个样,断定应该是safar ...

  3. 使用Yii框架完整搭建网站流程入门

    下载地址: http://www.yiiframework.com/ http://www.yiichina.com/ 由美籍华人薛强研究而出, Yii 这个名字(读作易(Yee))代表 简单(eas ...

  4. 小谈一下JavaScript中的JSON

    一.JSON的语法可以表示以下三种类型的值: 1.简单值:字符串,数值,布尔值,null 比如:5,"你好",false,null JSON中字符串必须用双引号,而JS中则没有强制 ...

  5. C# 利用NPOI 实现Excel转html

    public void ExcelToHtml(string fileName, IWorkbook workbook) { ExcelToHtmlConverter excelToHtmlConve ...

  6. hub config

    @echo off title Selenium_Hub cd /d %~dp0 java -jar selenium-server-standalone-2.48.2.jar -role hub - ...

  7. Numpy Study 2----* dot multiply区别

    使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,* ...

  8. jq变态全选vs原生变态全选

    <script> $(function(){ var num=0; $("#btn").on('click',function(){ if(this.checked){ ...

  9. jeesite部署到Tomcat后,无法访问,cannot be resolved in either web.xml or the jar files deployed with this application

    HTTP Status 500 - /WEB-INF/views/modules/sys/sysLogin.jsp (line: 3, column: 0) The absolute uri: htt ...

  10. angularjs不同页面间参数的传递

    1.在路由中定义要接收的参数 .state('userDetails', { url: '/userDetails?phone', //以?为标识接收参数 templateUrl: 'assets/v ...