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.
2 6 19 0
10 100100100100100100111111111111111111
题意:
给出一个数n,求出它的倍数x,这个倍数只由0和1组成。答案不唯一。
题解:
应用深搜,起始的数字x必须为1,每一次搜索得到的数为x=x*10或现在想x*10+1,否和只有1或0的条件,每次得到一个数都判断是否能整除n,如果能就标记,代表得到答案。因为n在200以内,经推断x的值long long int 可以存储。所以判断条件为是否标记和x的数值是否在long long int 范围内。
代码如下:
#include<stdio.h> #include<string.h> int n,flag; void dfs(int step,long long int x) { if(flag==1 || step>18) return; if(x%n==0) { flag=1; printf("%lld\n",x); return; } dfs(step+1,x*10); dfs(step+1,x*10+1); } int main() { while(~scanf("%d",&n) && n) { flag=0; dfs(0,1); } }