poj1426--Find The Multiple(广搜,智商题)

Find The Multiple

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18527   Accepted: 7490   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

在广搜的题中看到这一个,表示根本想不到广搜,,,,,

每一位只能是0或1,那么求n的倍数,从第一位开始搜,一直找到为止。

第一位一定是1,然后存余数temp,如果下一位是1,那么(temp*10+1)%n得到新的余数,如果是0,那么(temp*10)%n得到余数,这样进行广搜,大小是2^100

剪枝的方法:对于每一个求的余数,最多有200个,每一个只要出现过一次就好了,出现多的减掉

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
struct node{
    int k , temp ;
    int last ;
}p[1000000] , q ;
int flag[210] , a[120] , n ;
int bfs()
{
    int low = 0 , high = 0 ;
    p[high].k = 1 ;
    p[high].temp = p[high].k % n ;
    flag[p[high].temp] = 1 ;
    p[high++].last = -1 ;
    while( low < high )
    {
        q = p[low++] ;
        if( q.temp == 0 )
            return low-1 ;
        if( !flag[ (q.temp*10+1)%n ] )
        {
            p[high].k = 1 ;
            p[high].temp = (q.temp*10+1)%n;
            flag[ p[high].temp ] = 1 ;
            p[high++].last = low-1 ;
        }
        if( !flag[ (q.temp*10)%n ] )
        {
            p[high].k = 0 ;
            p[high].temp = (q.temp*10)%n ;
            flag[ p[high].temp ] = 1 ;
            p[high++].last = low-1 ;
        }
    }
    return -1 ;
}
int main()
{
    int i , j ;
    while(scanf("%d", &n) && n)
    {
        memset(flag,0,sizeof(flag));
        i = 0 ;
        j = bfs();
        while( j != -1 )
        {
            a[i++] = p[j].k ;
            j = p[j].last ;
        }
        for(j = i-1 ; j >= 0 ; j--)
            printf("%d", a[j]);
        printf("\n");
    }
    return 0;
}
时间: 2024-10-15 09:27:34

poj1426--Find The Multiple(广搜,智商题)的相关文章

杭电 1372 Knight Moves(广搜模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6439    Accepted Submission(s): 3886 Problem Description A friend of you is doing res

POJ 1426 Find The Multiple (广搜)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20235   Accepted: 8203   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

一道广搜寻路题

同样是在qq群里看到的题目,想了好久算法,实现也用了很久. 关于题目首先看图: 总的来说,就是一个二维迷宫的寻路,迷宫中有对应的钥匙和刺,每走一步会消耗1点Hp,当走到刺上时会额外消耗100点hp,持有对应颜色的钥匙通过刺时不用额外消耗Hp. 给予起点和终点的坐标,,输出移动方式,让人物抵达终点所消耗的Hp尽可能的小. 例子: 3 3 1..a##A...1 13 3这个是输入数据 第一个代表高 第二个宽 第三个是钥匙和陷阱的对数 .代表平地 #代表墙 小写字母是钥匙 大写字母是对应的陷阱输出为

POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

http://poj.org/problem?id=1426 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 c

hdu 1180(广搜好题)

诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 12487    Accepted Submission(s): 3120 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比 如下面的例子里,一开始楼梯在

广搜简单题

Catch That Cow 题目传送:POJ - 3278 - Catch That Cow 题解:点击即传送 迷宫问题 题目传送:POJ - 3984 - 迷宫问题 DFS也可以,见另一个题解 AC代码(BFS): #include <map> #include <set> #include <cmath> #include <deque> #include <queue> #include <stack> #include &l

广搜模板题马的遍历题解

题目描述 有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步 输入格式 一行四个数据,棋盘的大小和马的坐标 输出格式 一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达则输出-1) 本题思路是利用队列存储,将当前点可到达的下一个点入队以保证步数最小 #include<bits/stdc++.h> using namespace std; int n; int a,b,c,d,k[1001][1001]

poj 3984:迷宫问题(广搜,入门题)

迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7635   Accepted: 4474 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, }; 它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要

HDU 2267 How Many People Can Survive(广搜,简单)

题目 //一道简单的广搜水题 #include<queue> #include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; struct tt { int x,y; }; char mp[310][310]; int vis[310][310]; //看了题解,发现只有4个方向,而不是8个方向....题目貌似都没说清楚