POJ 1426 Find the multiple search advanced-summer Camp

Source: Internet
Author: User

E-find the multipleTime limit:1000MS Memory Limit:10000KB 64bit IO Format:%i64d &%i64 U SubmitStatus

Description

Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains on Ly the digits 0 and 1. You may assume this n is not greater than and there are a corresponding m containing no more than 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 digits. If There is multiple solutions for a given value of N, any one of the them is acceptable.

Sample Input

26190

Sample Output

10100100100100100100111111111111111111

#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace Std;

int n, Flag;  void DFS (unsigned long long result, int n, int digit); //result is the result of our final request is a multiple of N, n is the read data, digit is the number of results,         So a limit on the size of the logarithm 。 This program limit is 19 bits,        

int main ()
{
While (CIN >> N, N)
{
flag=0;
DFS (1, n, 0); //Because any data consisting of 0 and 1 begins with 1, and the number of bits is zero -based;
}
return 0;
}
void DFS (unsigned long long result, int n, int digit)
{
if (flag==1) return; //This represents the result of finding the first multiple of N, consisting of 0 and 1, to prevent infinite recursion, because there are many result;
if (result%n==0)
{
flag=1;
printf ("%i64u\n", result);
return;
}
if (digit==19)//Because unsigned long long is a 19-bit
return;
//Search in two directions, the latter one of result may be 0 or maybe 1
DFS (result*10, N, digit+1); //After one is 0
DFS (result*10+1, N, digit+1); //After one is 1
}

POJ 1426 Find the multiple search advanced-summer Camp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.