ZOJ 1530 - Find The Multiple
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZOJ 1530 - 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 <= n <= 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
大致题意:
给个n,找n的十进制倍数,仅有 0 和 1 组成,找一个就行,随便哪一个。
解题思路:
观察以下0,1串:
1
10 11
100 101 110 111
从n=1开始, 重复 n*10 与 n*10+1 ,由此遍历所有01串。
BFS,DFS皆可,以下采用DFS。
1 #include <iostream> 2 #include <queue> 3 #include <cstdio> 4 using namespace std; 5 queue<unsigned long long> s; 6 int n;//输入 7 unsigned long long temp,p; 8 void bfs() 9 { 10 while(!s.empty()) 11 s.pop(); 12 temp=1; 13 s.push(temp); 14 while(!s.empty()) 15 { 16 p=s.front(); 17 s.pop(); 18 if(p%n==0) 19 { 20 printf("%lld\n",p);//lld! 21 break; 22 } 23 s.push(p*10); 24 s.push(p*10+1); 25 } 26 } 27 int main(){ 28 while(scanf("%d",&n),n) 29 { 30 bfs(); 31 } 32 return 0; 33 }
以上是关于ZOJ 1530 - Find The Multiple的主要内容,如果未能解决你的问题,请参考以下文章
zoj3605 Find the Marble --- 概率dp
2019 Multi-University Training Contest 3 Find the answer (离散化+二分+树状数组)