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

2
6
19
0

Sample Output

10
100100100100100100
111111111111111111

Source

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

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> using namespace std; int n;
int ans;
int v[5000]; struct node
{
int x;
int y;
} a[1000010]; void DFS(int k)
{
int pt = a[k].y;
if(pt <= 0)
{
printf("1");
return ;
}
DFS(pt);
printf("%d",a[pt].x);
} void BFS()
{
ans = 1;
memset(v,0,sizeof(v));
queue<node>q;
struct node t,f;
t.x = 1;
t.y = 0;
a[0].x = 1;
a[0].y = 0;
q.push(t);
while(!q.empty())
{
t = q.front();
q.pop();
for(int i=0; i<=1; i++)
{
f.x = t.x * 10 + i; /// 同余模定理应用
if(v[f.x] == 0)
{
f.x = f.x % n;
f.y = ans;
q.push(f);
v[f.x] = 1;
a[ans].x = i;
a[ans].y = t.y;
if(f.x == 0)
{
DFS(ans);
printf("%d\n",i);
return ;
}
ans++; } }
}
} int main()
{
while(scanf("%d",&n)!=EOF)
{
if(n == 0)
{
break;
}
BFS();
}
return 0;
}

51nod 1109

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue> using namespace std; int n;
int ans;
int v[1010000]; struct node {
int x;
int y;
} a[1000010]; void DFS(int k) {
int pt = a[k].y;
if(pt <= 0) {
printf("1");
return ;
}
DFS(pt);
printf("%d",a[pt].x);
} void BFS() {
ans = 1;
memset(v,0,sizeof(v));
queue<node>q;
while(!q.empty()){
q.pop();
}
struct node t,f;
t.x = 1;
t.y = 0;
a[0].x = 1;
a[0].y = 0;
q.push(t);
while(!q.empty()) {
t = q.front();
q.pop();
for(int i=0; i<=1; i++) {
f.x = t.x * 10 + i; /// 同余模定理应用
f.x = f.x % n;
if(v[f.x] == 0) {
f.y = ans;
q.push(f);
v[f.x] = 1;
a[ans].x = i;
a[ans].y = t.y;
if(f.x == 0) {
DFS(ans);
printf("%d\n",i);
return ;
}
ans++; } }
}
} int main() {
while(scanf("%d",&n)!=EOF) {
BFS();
}
return 0;
}

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. [shell 编程] if [ $# -eq 0 ]该语句是什么含义?

    $0: shell或shell脚本的名字$*:以一对双引号给出参数列表$@:将各个参数分别加双引号返回$#:参数的个数$_:代表上一个命令的最后一个参数$$:代表所在命令的PID$!:代表最后执行的后 ...

  2. [Git] 关于refs/for/ 和refs/heads/

    转载自: http://lishicongli.blog.163.com/blog/static/146825902013213439500/ 1.     这个不是git的规则,而是gerrit的规 ...

  3. python 输出所有大小写字母, range()以及列表切片

    所以在写的时候,只要把它们的ASCII列出,并转化成字符型chr 即可. print [chr(i) for i in range(65,91)]#所有大写字母 print [chr(i) for i ...

  4. 通过 react-native-keyboard-aware-scroll-view 解决键盘遮盖输入框的问题

    1.安装 yarn add react-native-keyboard-aware-scroll-view 2.引入 import { KeyboardAwareScrollView } from ' ...

  5. Excle数组用法

    现在有如下需求:需要将行与列进行乘积,并将结果录入到对应单元格 [需求展示] 面对上面这样的表格,你会怎么处理呢?一个个乘积后录入吗?还是使用公式一行行操作? [解决办法] 这种问题,使用数组是最好解 ...

  6. 反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑) C#中缓存的使用 C#操作redis WPF 控件库——可拖动选项卡的TabControl 【Bootstrap系列】详解Bootstrap-table AutoFac event 和delegate的分别 常见的异步方式async 和 await C# Task用法 c#源码的执行过程

    反爬虫:利用ASP.NET MVC的Filter和缓存(入坑出坑)   背景介绍: 为了平衡社区成员的贡献和索取,一起帮引入了帮帮币.当用户积分(帮帮点)达到一定数额之后,就会“掉落”一定数量的“帮帮 ...

  7. 数组传到后台 string[] 获取

    调试的 时候js  断点到后台 js上面也可以查看 传到后台的数据 方便查看~~

  8. YII用户注冊表单的实现熟悉前台各个表单元素操作方式

    模还是必须定义两个基本方法.还有部分label标签映射为汉字,假设进行表单验证,还要定义一些验证规则: <? php /* * 用户模型 * */ class user extends CAct ...

  9. 我的 Android 学习笔记-Okhttp 的使用(译)

    okhttp 已经是非常流行的网络请求库了.网上介绍的文章非常之多,但感觉都不是特别系统.遂想到官方应该有介绍的文档,仔细寻找一番,果然.但可惜是英文的,于是就大致翻译了一下,权当做笔记了. 1.Ca ...

  10. objective-c的观察者模式

    addObserver即添加消息响应函数.postNotificationName即发消息.