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. React注释

    React中注释有以下三种 var content = ( <Nav> {/* 一般注释, 用 {} 包围 */} <Person /* 多 行 注释 */ name={window ...

  2. [AHOI2008] 逆序对

    link 我们可以很容易的推断出$-1$是单调不降的,若$i>j$且$a_i$与$a_j$都没有填数,若填完之后$a_i>a_j$或者$a_i<a_j$,则对答案产生影响的只在$[i ...

  3. MFC:CTime类和CTimeSpan类

    CTime类 CTime类表示日期和时间,上限是3000年12月31日,下限是1970年1月1日 12:00:00 AM GMT. CTime(); 构造一个未经初始化的CTime对象.此状态的CTi ...

  4. 2017 济南综合班 Day 4

    T1 外星人 二维前缀和 #include<cstdio> #define N 1001 using namespace std; bool v[N][N]; int sum[N][N]; ...

  5. 【Foreign】登山 [DP][数学]

    登山 Time Limit: 10 Sec  Memory Limit: 256 MB Description 恶梦是一个登山爱好者,今天他来到了黄山 俗话说的好,不走回头路.所以在黄山,你只能往前走 ...

  6. 【BZOJ5010】【FJOI2017】矩阵填数 [状压DP]

    矩阵填数 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定一个 h*w 的矩阵,矩阵的行 ...

  7. 实现拷贝函数(strcpy)

    #include <stdio.h> #include <stdlib.h> // 函数声明 char *mystrcpy(char *object, char *source ...

  8. POJ 2991 Crane (线段树)

    题目链接 Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of v ...

  9. TensorFlow 模型保存和导入、加载

    在TensorFlow中,保存模型与加载模型所用到的是tf.train.Saver()这个类.我们一般的想法就是,保存模型之后,在另外的文件中重新将模型导入,我可以利用模型中的operation和va ...

  10. MSSQL数据库 事务隔离级别

    数据库事务的隔离级别有4个,由低到高依次为Read uncommitted .Read committed .Repeatable read .Serializable ,这四个级别可以逐个解 脏读 ...