F.Find The Multiple
题意: 给定一个整数 n n n,找到一个只有 0 0 0和 1 1 1组成的数 m m m,使得 m m m整除 n n n
分析: D F S DFS DFS,每次 ∗ 10 *10 ∗10或 ∗ 10 + 1 *10+1 ∗10+1,这样保证搜索的所有数字都是由 0 0 0或 1 1 1组成。
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
int n,flag;
void dfs(LL x,int m){
if(m>=20||flag) return ;
if(x%n==0){
cout<<x<<endl;
flag=1;
return ;
}
dfs(x*10,m+1);
dfs(x*10+1,m+1);
}
int main(){
while(cin>>n){
if(!n) break;
flag=0;
dfs(1,1);
}
}