How to find the smallest number with just 0 and 1 which is divided by a given number?

后端 未结 11 1330
情话喂你
情话喂你 2020-11-29 22:44

Every positive integer divide some number whose representation (base 10) contains only zeroes and ones.

One can prove that:

Consider the numbers 1, 11, 111,

11条回答
  •  -上瘾入骨i
    2020-11-29 23:16

    The question equals to using 10i mod n (for each i, it can be used at most once) to get a sum m of n. It's like a knapsack problem or subset sum problem. In this way, dynamic programming will do the task.

    In dynamic programming the complexity is O(k*n). k is the number of digits in answer. For n<105, this code works perfectly.

    Code:

    #include 
    #define NUM 2000
    
    int main(int argc, char* argv[])
    {
        signed long pow[NUM],val[NUM],x,num,ten;
        int i,j,count;
        for(num=2; numpow[x%num]-1)printf("0");
                    count=pow[x%num]-1;
                    printf("1");
                    x=(num+x-val[pow[x%num]])%num;
                }
                while(count-->0)printf("0");
            }
            printf("\n");
        }
    }
    

    PS: This sequence in OEIS.

提交回复
热议问题