Find The Multiple ——深搜POJ

来源:互联网 发布:疲劳检测算法 编辑:程序博客网 时间:2024/05/22 07:53

Find The Multiple

 POJ - 1426 

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
26190
Sample Output
10100100100100100100111111111111111111
题意:找出任意一个是n的倍数且只含0和1的十进制数,这里说道n不大于200,long long int范围为19位,且long long int中只包含0和1的十进制数满足所有200之内任意一个数都有整数倍(这里需要自己想出)。
题解:开头不能为0,直接从1开始深搜,因为只能包含0和1,所以(x*10,step+1)和(x*10+1,step+1);不能超出long long范围,所以step最多是18。
代码:
#include<stdio.h>int n,flag;void dfs(long long int x,int step)//x定义为long long型{    if(flag||step>18)         return ;    if(x%n==0)    {        flag=1;        printf("%lld\n",x);        return ;    }    dfs(x*10,step+1);//满足只含0和1    dfs(x*10+1,step+1);}int main(){    while(scanf("%d",&n)&&n)    {        flag=0;//标记        dfs(1,0);    }    return 0;}