Poj 1426 find the multiple (BFS + same modulus theorem)

Source: Internet
Author: User
Tags modulus

Find the multiple
Time limit:1000 ms   Memory limit:10000 K
Total submissions:18390   Accepted:7445   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 in 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


Same modulus theorem:

(A * B) % N = (a % N * B % N) % N;

(A + B) % N = (a % N + B % N) % N;

The first 14 digits are: 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100...

1110% 6 = (110*10 + 0) % 6 = (110% 6) * 10) % 6 + (0% 6 );

MoD [x] = (mod [X/2] * 10 + x % 2) % N;

#include<stdio.h>#define N 600000int mod[N];int ans[200];int main(){    int i,k,n;    while(scanf("%d",&n),n)    {        mod[1]=1%n;         for(i=2;mod[i-1]!=0;i++)        {                                mod[i]=(mod[i/2]*10+i%2)%n;        }        i--;        k=0;        while(i)        {            ans[k++]=i%2;            i/=2;        }        for(i=k-1;i>=0;i--)            printf("%d",ans[i]);        puts("");    }    return 0;}


Poj 1426 find the multiple (BFS + same modulus theorem)

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.