POJ 1426 Find The Multiple

来源:互联网 发布:哈尔滨淘宝培训学校 编辑:程序博客网 时间:2024/06/04 20:05
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 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


题意:求出由1和0组成的N的倍数M,N是不大于200的数,M是小于100位的数

只要用深搜遍历即可,因为用long long只能存下19位数字,并且在long long以内可以找到符合条件的目标值。而n不大于200,所以只要判断在19位数字内是否存在目标值即可。

用f标记是否找到符合条件的m,s标记m的第几位。若f为真或s>18,return。只需要dfs(m*10,s+1)(m的第s位为0)和dfs(m*10+1,s+1)(m的第s位为1)就可以列举出所有状态,若m%n==0,标记f=1,return。


#include<stdio.h>int n,f;void dfs(long long int m,int s){    if(f||s>18)        return ;    if(m%n==0)    {        f=1;        printf("%lld\n",m);    }    dfs(m*10,s+1);  //第s位为0    dfs(m*10+1,s+1);  //第s位为1}int main(){    while(~scanf("%d",&n)&&n)    {        f=0;        dfs(1,0);    }    return 0;}


原创粉丝点击