poj 1426(同余搜索)

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[210]; ///这里是最强的剪枝,同余剪枝,如果一个小数的余数和大数的余数相同
               ///假设小点的数 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 = "1";
    s.mod = 1%n;
    q.push(s);
    while(!q.empty()){
        Node now = q.front();
        q.pop();
        if(now.mod==0){
            return now.ans;
        }
        Node next;
        next.mod = (now.mod*10+1)%n;
        next.ans = now.ans+"1";
        if(!mod[next.mod]){
            mod[next.mod] = true;
            q.push(next);
        }
        next.mod = (now.mod*10)%n;
        next.ans = now.ans+"0";
        if(!mod[next.mod]){
            mod[next.mod] = true;
            q.push(next);
        }
    }
}
int main(){

    while(scanf("%d",&n)!=EOF,n){
        cout<<bfs()<<endl;
    }
}
时间: 2024-11-08 22:34:19

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

(搜索) poj 1426

G - Find The Multiple Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1426 Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representat

[题解] [BFS] POJ 1426 - Find The Multiple

p { margin-bottom: 0.25cm; line-height: 115% } a:link { } VJudge题目:https://cn.vjudge.net/contest/279018#problem/L 即POJ 1426 - Find The Multiple:http://poj.org/problem?id=1426 题目要求: 给出一个不大于200的整数n,要求找到任意一个n的倍数(十进制),并且这个倍数只由1与0组成,最多100位. 输入输出:输入以0结束. 示

20200202 POJ - 3126 Prime Path POJ - 1426 Find The Multiple

>>>>>>>>>POJ 1426直达?? >>>>>>>>>POJ 3126直达?? 做了这么几道搜索题,感觉差不多也摸出了门路,模板差不多记下来了,只是面对不同的题目算法不同罢了 简单写一下想法,搞明白搜索的主题到底是什么(之前的mutiple那题,一开始就没想明白到底搜谁,就没想到算法 TBC(晚上再写 ===================================分割线=======

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

POJ 1426 Find The Multiple(寻找倍数)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 1699 Best Sequence (搜索技巧 剪枝 dfs)

题目链接 题意:给出几个基因片段,要求你将它们排列成一个最短的序列,序列中使用了所有的基因片段,而且不能翻转基因. 分析:先计算出add数组,再dfs枚举. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio> 6 #include <vector> 7 #include <al

广搜+打表 POJ 1426 Find The Multiple

POJ 1426   Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25734   Accepted: 10613   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representati

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

题目传送门 1 /* 2 题意:找出一个0和1组成的数字能整除n 3 DFS:200的范围内不会爆long long,DFS水过~ 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-2 14:21:51 8 File Name :POJ_1426.cpp 9 ******************************************

POJ 2196 Computer(搜索-深度优先搜索)

Computer Problem Description A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are