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

開始做这道题的时候半天没有读懂题意,例子数据是吓人的,把一个单词理解错误,multiple在这里是倍数的意思。这个题目就是要求我们求出给定的一个数的一个由0,1组成的倍数,100位也是吓人的。開始看那个例子,全然不好理解啊。看了半天没有理解题意,也想不到要用bfs来做。看了别人的一点提示就清晰啦。就用一个队列来模拟运算的过程,假设能够整除就直接输出,不能整除把他*10,和*10+1入队;
由于都是0,1组成的倍数,所以组成直接*10。或者是*10+1;
以下是用stl写的,第一次用stl写。有点水,用stl在poj上c++过不了,g++才过的;
  1. #include <iostream>
  2. #include <queue>
  3. #include <cstdio>
  4. using namespace std;
  5. void bfs(int n)
  6. {
  7. queue<__int64>q;//这里后面的数据可能会有大的,所以用个64位的就够了
  8. q.push(1);
  9. while(!q.empty())
  10. {
  11. __int64 x;
  12. x=q.front();
  13. q.pop();
  14. if(x%n==0)//能够整除就直接输出
  15. {
  16. printf("%I64d\n",x);
  17. return ;
  18. }
  19. q.push(x*10);//把x的10的倍数入队。
  20. q.push(x*10+1);
  21. }
  22. }
  23. int main()
  24. {
  25. int n;
  26. while(scanf("%d",&n)&&n)
  27. {
  28. bfs(n);
  29. }
  30. return 0;
  31. }

自己用数组模拟队列写了一下;貌似效率比stl好了一些;

主要的思想都是一样的;
  1. #include <cstdio>
  2. #include <cstring>
  3. long long q[2000000];
  4. void bfs(int n)
  5. {
  6. int front=0;
  7. int rear=0;
  8. q[front]=1;
  9. rear++;
  10. long long temp;
  11. while(rear>front)
  12. {
  13. temp=q[front];
  14. if(temp%n==0)
  15. {
  16. break;
  17. }
  18. temp*=10;
  19. q[rear]=temp;
  20. rear++;
  21. q[rear]=temp+1;
  22. rear++;
  23. front++;
  24. }
  25. printf("%lld\n",temp);
  26. }
  27. int main()
  28. {
  29. int n;
  30. while(scanf("%d",&n)&&n)
  31. {
  32. bfs(n);
  33. }
  34. return 0;
  35. }

在网上还看到了大神的代码用了同余取模定理,还是有点没看懂。还有的直接用满二叉树模拟的队列。

(a*b)%n = (a%n *b%n)%n

(a+b)%n = (a%n +b%n)%n



看到其它大神对这道的思路:

再贴一段pl大牛关于此题的分析思路。以学习:

要m整除n,那么能够用对n的余数来表示当前的状态。

搜到一个余数为0的状态就能够了。

直接bfs出去。

假设当前的余数是r。添在当前答案后面的数为,k

那么新的余数。也就是新的状态为:

( r * 10 + k ) % n 。

这样最多200个状态,判重一下。能够非常快出解。

大牛的代码:

  1. #include<iostream>
  2. using namespace std;
  3. int a[524300],i,n;
  4. int main(){
  5. while(cin>>n){
  6. if (!n) break;
  7. i=1; a[1]=1%n;
  8. while(a[i]){i++; a[i]=(a[i/2]*10+i%2)%n;}
  9. n=0; while(i){a[n++]=i%2;i>>=1;}
  10. while(n--) cout<<a[n]; cout<<endl;
  11. }
  12. return 0;
  13. }

这原来也是bfs,直接用一个满二叉树实现(左儿子是0,右儿子是1);

还是要多学习大神们的代码~





poj 1426 Find The Multiple (bfs 搜索)的更多相关文章

  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(搜索+数论)

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

  3. POJ 1426 Find The Multiple BFS

    没什么好说的 从1开始进行广搜,因为只能包涵0和1,所以下一次需要搜索的值为next=now*10 和 next=now*10+1,每次判断一下就可以了,但是我一直不太明白我的代码为什么C++提交会错 ...

  4. poj 1426 Find The Multiple ( BFS+同余模定理)

    Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18390   Accepted: 744 ...

  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

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

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

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

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

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

随机推荐

  1. python--fnmatch

    import fnmatch ''' 这个库专门是用来做文件名匹配的,可以使用通配符如下 * 匹配任何数量的任意字符 ? 匹配单个数量的任意字符 [seq] 匹配seq中的任意字符 [!seq] 匹配 ...

  2. iptables 用法及常用模块总结

    iptables传输数据包的过程: 1. 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去. 2. 如果数据包就是进入本机的,它就会沿着图向下移动, ...

  3. [ 总结 ] nginx 编译参数中文详解

    贴出来,方便查找和学习. nginx 编译参数: --prefix=PATH    指向安装目录--sbin-path=PATH    指向(执行)程序文件--conf-path=PATH    指向 ...

  4. Log4Net的控制台,WinForm,WebApplication使用

    一.Log4Net的控制台,WinForm,WebApplication使用 1.首先使用nuget 添加log4Net 到控制台项目中 log4j每个符号的具体含义:%d %5p %c{1}:%L ...

  5. BZOJ 1207

    1207: [HNOI2004]打鼹鼠 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3089  Solved: 1499[Submit][Statu ...

  6. BMP文件组成

    BMP文件组成 BMP文件由文件头.位图信息头.颜色信息和图形数据四部分组成. 如图: 位图文件头BITMAPFILEHEADER 位图信息头BITMAPINFOHEADER 调色板Palette 实 ...

  7. hit2739

    好题,回路的问题一般都要转化为度数来做若原图的基图不连通,或者存在某个点的入度或出度为0则无解.统计所有点的入度出度之差di对于di>0的点,加边(s,i,di,0):对于di<0的点,加 ...

  8. AC日记——由乃与大母神原型和偶像崇拜 洛谷 P3792

    由乃与大母神原型和偶像崇拜 思路: 逆元+线段树维护和+线段树维护平方和+线段树维护最大最小值: 代码: #include <bits/stdc++.h> using namespace ...

  9. Mathematica作图

    第2讲 在Mathematica中作图    一个较强的符号计算系统均有很好的绘图功能,Mathematica也不例外,Mathematica 拥有非常强大的绘图功能.并且提供了一大批基本数学函数的图 ...

  10. centos6.5 python2.7.8 安装scrapy总是出错【解决】

    pip install Scrapy 报错: UnicodeDecodeError: 'ascii' codec can't decode byte 0xb4 in position python s ...