Find The Multiple
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 26926   Accepted: 11174   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 题意:找到一个由01组成的串m使得 m%n == 0
题解:同余定理.很强的搜索剪枝技巧。
同余剪枝,如果一个小数的余数和大数的余数相同,假设小点的数 a ,大点的为 b ,a%n==b%n => (a+c)%n == (b+c)%n ,我们假设 a+c是符合条件的,那么,b+c 也是符合条件的,所以我们可以直接不要
b+c 了,这就是一个很巧妙并且很强的剪枝.
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <string>
#include <iostream>
using namespace std;
int n;
bool mod[]; ///这里是最强的剪枝,同余剪枝,如果一个小数的余数和大数的余数相同
///假设小点的数 a ,大点的为 b ,a%n==b%n => (a+c)%n == (b+c)%n ,我们假设 a+c是符合条件的,那么
/// b+c 也是符合条件的,所以我们可以直接不要 b+c 了,这就是一个很巧妙并且很强的剪枝.
struct Node{
int mod;
string ans;
};
string bfs(){
memset(mod,false,sizeof(mod));
queue<Node> q;
Node s;
s.ans = "";
s.mod = %n;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.mod==){
return now.ans;
}
Node next;
next.mod = (now.mod*+)%n;
next.ans = now.ans+"";
if(!mod[next.mod]){
mod[next.mod] = true;
q.push(next);
}
next.mod = (now.mod*)%n;
next.ans = now.ans+"";
if(!mod[next.mod]){
mod[next.mod] = true;
q.push(next);
}
}
}
int main(){ while(scanf("%d",&n)!=EOF,n){
cout<<bfs()<<endl;
}
}

poj 1426(同余搜索)的更多相关文章

  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. POJ.3279 Fliptile (搜索+二进制枚举+开关问题)

    POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...

  6. hdu 1226(同余搜索)

    超级密码 Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

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

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

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

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

随机推荐

  1. vue-router的钩子

    vue-router的钩子分为三类: 1. 全局钩子2. 路由独享钩子3. 组件内钩子 1. 全局钩子 beforeEach(to,from,next) afterEach(route) 2. 路由独 ...

  2. 全面解析JavaScript的Backbone.js框架中的Router路由

    这篇文章主要介绍了Backbone.js框架中的Router路由功能,Router在Backbone中相当于一个MVC框架中的Controller控制器功能,需要的朋友可以参考下. Backbone ...

  3. Linux系统上的popen()库函数

    popen可以是系统命令,也可以是自己写的程序a.out. 假如a.out就是打印 “hello world“ 在代码中,想获取什么,都可以通过popen获取. 比如获取ls的信息, 比如获取自己写的 ...

  4. ClusterId read in ZooKeeper is null 处理

    ClusterId read in ZooKeeper is null. Re-running the program after fixing issue 1 will result in the ...

  5. Moodle安装图解

    Moodle安装图解 一.    Moodle运行环境搭建 Moodle主要是在Linux上使用Apache, PostgreSQL/MySQL/MariaDB及 PHP 开发(LAMP平台). 1. ...

  6. Oracle raw类型

    RAW(size):长度为size字节的原始二进制数据,size的最大值为2000字节; RAW类型好处:在网络中的计算机之间传输 RAW 数据时,或者使用 Oracle 实用程序将 RAW 数据从一 ...

  7. Update SSM agent to each EC2 via Bat and bash script

    1. copy the instance id from aws console to file 2. remove the , from file sed -i 's/,//g' file 3. g ...

  8. [技巧篇]01.Servlet的优化模版代码

    Servlet.java的模版 #---------------------------------------------# # <aw:description>Template for ...

  9. 应用Xml.Linq读xml文件

    c#提供了System.Xml.Linq操作xml文件,非常方便,本文主要介绍如何应用System.Xml.Linq读取xml文件. xml文本 <?xml version="1.0& ...

  10. 【网络】RFC1245-OSPF Protocol Analysis

    OSPF协议分析 摘要 这是OSPF协议的两份报告中的第一份,这些报告是因特网工程指导组要求的,是用来将一个因特网协议写成标准草案的.OSPF是一个TCP/IP协议族中的一个的路由协议,被设计用于一个 ...