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

题意:输入一个正整数n(1<=n<=200),然后要求找一个仅仅包括0和1的十进制数字能整除n

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. #include<queue>
  7.  
  8. using namespace std;
  9.  
  10. int n;
  11. int ans;
  12. int v[5000];
  13.  
  14. struct node
  15. {
  16. int x;
  17. int y;
  18. } a[1000010];
  19.  
  20. void DFS(int k)
  21. {
  22. int pt = a[k].y;
  23. if(pt <= 0)
  24. {
  25. printf("1");
  26. return ;
  27. }
  28. DFS(pt);
  29. printf("%d",a[pt].x);
  30. }
  31.  
  32. void BFS()
  33. {
  34. ans = 1;
  35. memset(v,0,sizeof(v));
  36. queue<node>q;
  37. struct node t,f;
  38. t.x = 1;
  39. t.y = 0;
  40. a[0].x = 1;
  41. a[0].y = 0;
  42. q.push(t);
  43. while(!q.empty())
  44. {
  45. t = q.front();
  46. q.pop();
  47. for(int i=0; i<=1; i++)
  48. {
  49. f.x = t.x * 10 + i; /// 同余模定理应用
  50. if(v[f.x] == 0)
  51. {
  52. f.x = f.x % n;
  53. f.y = ans;
  54. q.push(f);
  55. v[f.x] = 1;
  56. a[ans].x = i;
  57. a[ans].y = t.y;
  58. if(f.x == 0)
  59. {
  60. DFS(ans);
  61. printf("%d\n",i);
  62. return ;
  63. }
  64. ans++;
  65.  
  66. }
  67.  
  68. }
  69. }
  70. }
  71.  
  72. int main()
  73. {
  74. while(scanf("%d",&n)!=EOF)
  75. {
  76. if(n == 0)
  77. {
  78. break;
  79. }
  80. BFS();
  81. }
  82. return 0;
  83. }

51nod 1109

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. #include<queue>
  7.  
  8. using namespace std;
  9.  
  10. int n;
  11. int ans;
  12. int v[1010000];
  13.  
  14. struct node {
  15. int x;
  16. int y;
  17. } a[1000010];
  18.  
  19. void DFS(int k) {
  20. int pt = a[k].y;
  21. if(pt <= 0) {
  22. printf("1");
  23. return ;
  24. }
  25. DFS(pt);
  26. printf("%d",a[pt].x);
  27. }
  28.  
  29. void BFS() {
  30. ans = 1;
  31. memset(v,0,sizeof(v));
  32. queue<node>q;
  33. while(!q.empty()){
  34. q.pop();
  35. }
  36. struct node t,f;
  37. t.x = 1;
  38. t.y = 0;
  39. a[0].x = 1;
  40. a[0].y = 0;
  41. q.push(t);
  42. while(!q.empty()) {
  43. t = q.front();
  44. q.pop();
  45. for(int i=0; i<=1; i++) {
  46. f.x = t.x * 10 + i; /// 同余模定理应用
  47. f.x = f.x % n;
  48. if(v[f.x] == 0) {
  49. f.y = ans;
  50. q.push(f);
  51. v[f.x] = 1;
  52. a[ans].x = i;
  53. a[ans].y = t.y;
  54. if(f.x == 0) {
  55. DFS(ans);
  56. printf("%d\n",i);
  57. return ;
  58. }
  59. ans++;
  60.  
  61. }
  62.  
  63. }
  64. }
  65. }
  66.  
  67. int main() {
  68. while(scanf("%d",&n)!=EOF) {
  69. BFS();
  70. }
  71. return 0;
  72. }

POJ 1426 Find The Multiple &amp;&amp; 51nod 1109 01组成的N的倍数 (BFS + 同余模定理)的更多相关文章

  1. 51nod 1109 01组成的N的倍数

    用01 组成 N的最小倍数 这个BFS搜索就好. 类似这道:  ZOJ Problem Set - 1530 每次 要么是0 要么是1, 记入余数,和前驱. #include<bits/stdc ...

  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 --- BFS || DFS

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

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

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

  5. POJ.1426 Find The Multiple (BFS)

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

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

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

  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( bfs )

    题目:http://poj.org/problem?id=1426 题意:输入一个数,输出这个数的整数 倍,且只有0和1组成 程序里写错了一个数,结果一直MLE.…… #include <ios ...

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

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

随机推荐

  1. AtomicInteger类的简单应用

    AtomicInteger,一个提供原子操作的Integer的类.在Java语言中,++i和i++操作并不是线程安全的,在使用的时候,不可避免的会用到synchronized关键字.而AtomicIn ...

  2. http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml

    http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml

  3. http://blog.csdn.net/huang_xw/article/details/7090173

    http://blog.csdn.net/huang_xw/article/details/7090173

  4. python实现word2vec训练结果bin文件转txt文件

    经理让我把word2vec训练后得到的bin文件转为txt文件,目前还不知道txt文件用来干什么.其实word2vec训练语料时可以选择训练处出bin文件或者txt文件,但是训练出bin文件时过程太漫 ...

  5. DevExpress.Build

    using System.Collections.Generic; using Microsoft.Build.AppxPackage; using Microsoft.Build.Framework ...

  6. XAMPP Apache + MySQL + PHP + Perl

    XAMPP Apache + MySQL + PHP + Perl 什么是XAMPP? XAMPP是最流行的PHP开发环境 XAMPP是完全免费且易于安装的Apache发行版,其中包含MySQL.PH ...

  7. yoman搭建你的react-webpack应用

    还没有npm和node的要提前做好准备工作 做好这一切之后 我们安装yo,记住安装在全局变量中,我们需要用他的命令工具 npm install -g yo 接下来安装yo提供的react-webpac ...

  8. [Unity3D]UI方案及制作细节(NGUI/EZGUI/原生UI系统)

    转载请留下本文原始链接,谢谢.本文会不定期更新维护,最近更新于2013.09.17.   http://blog.sina.com.cn/s/blog_5b6cb9500101bplv.html   ...

  9. 有关于apktool的使用的一些心得

    <span style="font-family: Arial, Helvetica, sans-serif;">1.配置Java的环境</span> 1) ...

  10. Python——在Python中如何使用Linux的epoll

    在Python中如何使用Linux的epoll 目录 序言 阻塞socket编程示例 异步socket的好处以及Linux epoll 带epoll的异步socket编程示例 性能注意事项 源代码 序 ...