[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1426 Find The Multiple 用深搜dfs枚举结果+习得llu

[kuangbin带你飞]专题1-23 专题一 简单搜索 POJ 1426 Find The Multiple 用深搜dfs枚举结果+习得llu

总目录详见https://blog.csdn.net/mrcrack/article/details/107590709

在线测评地址https://vjudge.net/problem/POJ-1426

Status Accepted
Time 172ms
Memory 100kB
Length 300
Lang C++

题目大意:给定一个正整数n,编写一个程序来找出n的非零倍数m,它的十进制表示只包含数字0和1。您可以假设n不大于200,并且对应的m包含不超过100个十进制数字。

基本思路:用深搜dfs枚举结果,用结果来验证是否能整除。

AC代码如下:

#include 
#define ull unsigned long long 
int f,n;
void dfs(int step,ull x){
	if(f)return;//剪枝
	if(step>=20)return;//剪枝
	if(x%n==0){
		f=1;
		printf("%llu\n",x);
		return;
	}
	dfs(step+1,x*10);//要么个位是0
	dfs(step+1,x*10+1);//要么个位是1
}
int main(){
	while(scanf("%d",&n)){
		if(!n)break;
		f=0,dfs(1,1);
	}
	return 0;
}

为什么unsigned long long够用了,题中不是说不超过100位吗,这是由打表代码得到的,结果是最多19位十进制。

打表代码如下:

#include 
#define ull unsigned long long 
int f,n;
void dfs(int step,ull x){
	if(f)return;//剪枝
	if(step>=20)return;//剪枝
	if(x%n==0){
		f=1;
		printf("%llu\n",x);
		return;
	}
	dfs(step+1,x*10);//要么个位是0
	dfs(step+1,x*10+1);//要么个位是1
}
int main(){
	for(n=1;n<=200;n++){
		f=0,dfs(1,1);
	}
	return 0;
}

打表结果如下:

1
10
1000000000000000011
100
10
1000000000000000110
1000000000000000111
1000
1000000000011111111
10
100000000000000001
1000000000000001100
1000000000000001
10000000000000010
1000000000000000110
10000
10000000000000101
1000000000111111110
1000000000000001101
100
10000000000000101
1000000000000000010
1000000000000010111
1000000000000011000
100
10000000000000010
1000000000101111111
100000000000000100
1000000000000000111
1000000000000000110
1000000000000110011
100000
1000000000000101111
100000000000001010
10000000000000010
1000000001111111100
100000000000000011
10000000000000110
10000000000000101
1000
1000000000000010111
100000000000001010
1000000000000010101
100000000000000100
1000000000111111110
100000000000001110
1000000000000101
1000000000000110000
1000000000000011101
100
10000000000000101
100000000000000100
1000000000000000011
1000000001101111110
1000000000000000010
1000000000000001000
1000000000000011
1000000000000001010
10000000000000011
1000000000000001100
1000000000000011
100000000001110010
1000000001011101111
1000000
10000000000000010
1000000000000111110
100000000000011
1000000000000010100
100000000000011
10000000000000010
1000000000000100101
1000000011111111000
1000000000001
1000000000000000110
1000000000000001100
100000000000001100
1000000000000001
100000000000001010
100000000000110001
10000
1000000000111111101
1000000000000110110
100000000000001101
1000000000000010100
100000000000001010
100000000000010010
100000000000000101
1000000000000001000
1000000000000011011
1000000000111111110
1000000000000001
1000000000000011100
1000000000001111001
10000000000001010
10000000000000110
1000000000001100000
100000000001111
1000000000000111110
1101111111111111111
100
1000000000000000001
100000000000001010
100000000000000001
1000000000000001000
100000000000001010
100000000010000110
10000000000001001
1000000001111111100
1000000000001000011
1000000000000000010
100000000000000011
100000000000110000
1000000000000111101
10000000000000110
100000000000001110
10000000000000100
1000000001101111101
100000000000000110
10000000000000101
1000000000000011000
1000000000000101111
10000000000000110
1000000001100011001
1000000000011100100
1000
1000000001111011110
1000000000000110101
10000000
10000000000001001
10000000000000010
10000000001101
1000000000010111100
1000000000001010001
1000000000000110
1000000001101111110
100000000000111000
1000000000001
1000000000000110
1000000000001001111
100000000000000100
1000000000000101
1000000000011110
1000000000000001
1000000111111110000
1000000000000001010
10000000000010
100000000000011111
1000000000000010100
10000000000001111
1000000000000001100
1000000000010111001
1000000000000011000
1000000000111111101
10000000000000010
100000000001110010
1000000000000010100
1000000000000101
1000000000001100010
1000000000000000011
100000
1000000000001101001
100000000111111110
1000000000011011101
1000000000001110100
1000000000000111110
1000000000000011010
1000000000001001101
10000000000011000
1000000000001100111
100000000000001010
1000000000111111011
1000000000000100100
1000000000000010111
1000000000000001010
100000000000000100
100000000000010000
10000000000000011
100000000000001110
1000000000000100111
1000000001111111100
10000000000000111
10000000000000010
1000000000000011
100000000000011000
1000000000000000110
10000000001101110
100000000001001001
100000000000010100
1000000010111010111
10000000000000110
1000000000011111101
1000000000011000000
1000000010000100001
1000000000011110
100000000000001010
100000000010010100
100000000000111001
1111111111111111110
1000000000101111001
1000

编后记:翻看了他人代码,收获很大。一开始想的是乘法入手,进行构造,发现工程量巨大。

你可能感兴趣的:(kuangbin带你飞)