一道经典B(打)F(表)S(题)之“Find The Multiple”

本文探讨了一种使用广度优先搜索(BFS)策略解决特定数学问题的方法,即寻找一个正整数n的倍数m,且m仅由0和1组成。通过对比两种不同的实现方式,一种是直接使用BFS算法,另一种是预先计算并存储所有可能的解,展示了如何在时间和空间复杂度之间做出权衡。

题目:

   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.

 

   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.

 

   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

题目大意:

  输入一个不超过200的整数 n,找出一个整数 m 使得 m 是 n 的整数倍,而且 m 只能由 0 和 1 组成。

解题思路:

  一道经典的BFS。

  一开始我们可能会认为这道题的结果很大,long 型存不下,但是我用程序实际跑了一下,发现 long 是能存下的,c++应该是unsign long long能存下。

  所以就交了几次,一发超时,一发超内存。。。。十分尴尬。

  然后我就特么的 打了个表。。。了个表。。。个表。。。表。。。

超内存代码(BFS思想,java语言):

 1 import java.util.*;
 2 import java.math.BigInteger;
 3 public class Main{
 4 
 5     public static void main(String[] args){
 6         Scanner sc = new Scanner(System.in);
 7         while(sc.hasNext()){
 8             int n = sc.nextInt();
 9             if(n == 0){break;}
10             Queue<String> m = new LinkedList<String>();
11             m.offer("1");
12             long p = Long.valueOf(m.peek());
13             while(p % n != 0){
14                 String t = m.peek();
15                 m.poll();
16                 m.offer(t + "0");
17                 m.offer(t + "1");
18                 p = Long.valueOf(m.peek());
19             }    
20             System.out.println(m.peek());
21         }
22     }
23 }

AC代码(打表):

  1 import java.util.*;
  2 import java.math.BigInteger;
  3 public class POJ1426{
  4     
  5     static String list[] = {
  6         "1",
  7         "10",
  8         "111",
  9         "100",
 10         "10",
 11         "1110",
 12         "1001",
 13         "1000",
 14         "111111111",
 15         "10",
 16         "11",
 17         "11100",
 18         "1001",
 19         "10010",
 20         "1110",
 21         "10000",
 22         "11101",
 23         "1111111110",
 24         "11001",
 25         "100",
 26         "10101",
 27         "110",
 28         "110101",
 29         "111000",
 30         "100",
 31         "10010",
 32         "1101111111",
 33         "100100",
 34         "1101101",
 35         "1110",
 36         "111011",
 37         "100000",
 38         "111111",
 39         "111010",
 40         "10010",
 41         "11111111100",
 42         "111",
 43         "110010",
 44         "10101",
 45         "1000",
 46         "11111",
 47         "101010",
 48         "1101101",
 49         "1100",
 50         "1111111110",
 51         "1101010",
 52         "10011",
 53         "1110000",
 54         "1100001",
 55         "100",
 56         "100011",
 57         "100100",
 58         "100011",
 59         "11011111110",
 60         "110",
 61         "1001000",
 62         "11001",
 63         "11011010",
 64         "11011111",
 65         "11100",
 66         "100101",
 67         "1110110",
 68         "1111011111",
 69         "1000000",
 70         "10010",
 71         "1111110",
 72         "1101011",
 73         "1110100",
 74         "10000101",
 75         "10010",
 76         "10011",
 77         "111111111000",
 78         "10001",
 79         "1110",
 80         "11100",
 81         "1100100",
 82         "1001",
 83         "101010",
 84         "10010011",
 85         "10000",
 86         "1111111101",
 87         "111110",
 88         "101011",
 89         "1010100",
 90         "111010",
 91         "11011010",
 92         "11010111",
 93         "11000",
 94         "11010101",
 95         "1111111110",
 96         "1001",
 97         "11010100",
 98         "10000011",
 99         "100110",
100         "110010",
101         "11100000",
102         "11100001",
103         "11000010",
104         "111111111111111111",
105         "100",
106         "101",
107         "1000110",
108         "11100001",
109         "1001000",
110         "101010",
111         "1000110",
112         "100010011",
113         "110111111100",
114         "1001010111",
115         "110",
116         "111",
117         "10010000",
118         "1011011",
119         "110010",
120         "1101010",
121         "110110100",
122         "10101111111",
123         "110111110",
124         "100111011",
125         "111000",
126         "11011",
127         "1001010",
128         "10001100111",
129         "11101100",
130         "1000",
131         "11110111110",
132         "11010011",
133         "10000000",
134         "100100001",
135         "10010",
136         "101001",
137         "11111100",
138         "11101111",
139         "11010110",
140         "11011111110",
141         "11101000",
142         "10001",
143         "100001010",
144         "110110101",
145         "100100",
146         "10011",
147         "100110",
148         "1001",
149         "1111111110000",
150         "11011010",
151         "100010",
152         "1100001",
153         "11100",
154         "110111",
155         "11100",
156         "1110001",
157         "11001000",
158         "10111110111",
159         "10010",
160         "1110110",
161         "1010100",
162         "10101101011",
163         "100100110",
164         "100011",
165         "100000",
166         "11101111",
167         "11111111010",
168         "1010111",
169         "1111100",
170         "1111110",
171         "1010110",
172         "11111011",
173         "10101000",
174         "10111101",
175         "111010",
176         "1111011111",
177         "110110100",
178         "1011001101",
179         "110101110",
180         "100100",
181         "110000",
182         "100101111",
183         "110101010",
184         "11010111",
185         "11111111100",
186         "1001111",
187         "10010",
188         "100101",
189         "110101000",
190         "1110",
191         "100000110",
192         "1001011",
193         "1001100",
194         "1010111010111",
195         "110010",
196         "11101111",
197         "111000000",
198         "11001",
199         "111000010",
200         "101010",
201         "110000100",
202         "1101000101",
203         "1111111111111111110",
204         "111000011",
205         "1000"
206     };
207     public static void main(String[] args){
208         Scanner sc = new Scanner(System.in);
209         while(sc.hasNext()){
210             int n = sc.nextInt();
211             if(n == 0){break;}
212             System.out.println(list[n - 1]);
213         }
214     }
215 }

至于C++版本的代码,博主博客不再发布了,毕竟大多数人用C++,网上很多解题报告任君选择。

转载于:https://www.cnblogs.com/love-fromAtoZ/p/7551264.html

c++14 ## 目描述 小 L 给你出了一道简单数学。 对于一个长度为 $n$ 的排列 $p_n$,我们在 $i$ 与 $p_i$ 之间连无向边,设形成的连通块个数为 $C(p_n)$。 对于一个连通块 $S$,如果他包含的点的编号为 $x_1,x_2\dots x_k$,那么他的权值为 $w(S)=\sum\limits_{i=1}^k 2^{x_i}$。 如果连通块分别为 $S_1,S_2\dots S_{C(p_n)},那么$记该排列的权值为 $\sum\limits_{i=1}^{C(p_n)}2^{w(S_i)}$。 记 $F(n,m)$ 示 $C(p_n)=m$ 的 $p_n$ 个数。 记 $G(n,m)$ 为 $C(p_n)=m$ 的 $p_n$ 的不同权值数量。 这里认为 $F(0,0)=G(0,0)=1$。 现在有 $Q$ 次询问,每次给出 $op,n,m$: - 如果 $op=1$,你需要输出 $F(n,m)\bmod 2$。 - 如果 $op=2$,你需要输出 $G(n,m)\bmod 2$。 ## 输入格式 第一行一个整数 $Q$ 代询问次数。 接下来 $Q$ 行,每行三个整数 $op,n,m$ 代小 L 的一次询问。 - 如果 $op=1$,你需要输出 $F(n,m)\bmod 2$。 - 如果 $op=2$,你需要输出 $G(n,m)\bmod 2$。 ## 输出格式 一行一个长度为 $Q$ 的 $01$ 字符串代答案序列。 ## 输入输出样例 #1 ### 输入 #1 ``` 20 1 3 8 2 10 2 2 8 2 1 3 1 2 3 3 1 1 5 1 5 10 2 9 8 1 9 5 2 6 6 2 1 0 2 6 4 1 5 3 1 2 5 2 4 7 2 0 2 2 0 5 2 8 1 2 7 1 1 8 8 ``` ### 输出 #1 ``` 01101000110110000111 ``` ## 说明/提示 - 对于 $20\%$ 的数据,$0\leq n,m\leq 10$。 - 对于 $40\%$ 的数据,$0\leq n,m\leq 5000$。 - 对于另外 $15\%$ 的数据,$op=1$。 - 对于另外 $15\%$ 的数据,$op=2$。 - 对于另外 $15\%$ 的数据,$0\leq n,m\leq 10^5$。 - 对于 $100\%$ 的数据,$1\leq Q\leq 10^6,0\leq n,m\leq 10^9,1\leq op\leq 2$。
最新发布
12-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值