2012-12-23 22:22:50Morris

[UVA] 1189 - Find The Multiple

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 $ leq$ n $ leq$ 200). A line containing a zero (0) 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 個倍數,
那麼要怎麼找呢?先假是 a 是由 1 構成的十進制數,那麼長度先不管,
逐漸將長度從 1 ... m 討論所有的 a,一定有存在重覆的餘數,此時,
兩數相減不會發生借位問題,就是答案了。


#include <stdio.h>

int main() {
int n;
while(scanf("%d", &n) == 1 && n) {
int C[205] = {}, t = 1%n, i, j;
for(i = 1; ; i++) {
if(C[t]) {
for(j = C[t]; j < i; j++)
putchar('1');
for(j = C[t]-1; j >= 0; j--)
putchar('0');
puts("");
break;
}
C[t] = i;
t = (t*10+1)%n;
}
}
return 0;
}