POJ 1426 Find The Multiple(寻找倍数)

POJ 1426 Find The Multiple(寻找倍数)

Time Limit: 1000MS    Memory Limit: 65536K

 

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.

给定一个整数n,敲个代码来找出n的仅有数字0和1构成的非零倍数m。你可以认为n不超过200且m不超过100个十进制数。
CN

 

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.

多组测试用例。每行有一个数n (1 <= n <= 200)。某行一个0为输入结束。
CN

 

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.

对于每个输入的n,输出一行m。十进制数m必须不能超过100位。如果对于n存在多解,则随便输出一种。
CN

 

Sample Input - 输入样例

2
6
19
0

 

Sample Output - 输出样例

10
100100100100100100
111111111111111111

 

题解

  刚刚开始在想直接2^100可能有点问题,打算模拟除法来做…………然而水平太渣,写条件真是要死要活。(虽然可以出结果但是回溯/转移的时候没考虑好导致长度有问题……)
  发觉讨论里表示可以直接暴力出来……然后发现…………居然可以?!
  (估计数据不多)
  从100位开始发现可以刚好剪到19位,int64范围。
  不过似乎由于编译器对自动转换的支持问题,提交的时候要手动弄好int64才可以。

 

代码 C++

 1 #include <cstdio>
 2 __int64 isFid, n;
 3 void DFS(__int64 now, int len){
 4     if (!isFid || len > 18) return;
 5     if (isFid = now%n){
 6         DFS(now * 10, len + 1); DFS(now * 10 + 1, len + 1);
 7     }
 8     else printf("%I64d
", now);
 9 }
10 int main(){
11     while (scanf("%I64d", &n), isFid = n) DFS(1, 0);
12     return 0;
13 }
原文地址:https://www.cnblogs.com/Simon-X/p/6298461.html