【E - Find The Multiple】

本文探讨了解决一道寻找最小特殊数问题的三种方法:深度优先搜索(DFS)、利用数学公式迭代和广度优先搜索(BFS)。通过对比不同算法的时间和空间复杂度,深入分析了每种方法的优缺点。

思路:

  • 毫无思路
  • 第一版:dfs。看了mars167的BLOG,原来题面是吓唬人的,用 unsigned long long 可以过。
  • 第二版:居然还有用循环代替搜索的手段:
    注意,用 long long 就可以过,因为是从小到大解题。
	mod[i] = mod[i/2] * 10 + i%2 ;
  • 第三版:bfs。因为不用结构体,广搜比深搜好写(long long WA;unsigned long long AC。为什么呢?广搜一定是从小到大啊)。

代码:

  • dfs:125ms 660kB
//125ms		660kB


#include <iostream>

using namespace std;

typedef unsigned long long LL ;

LL ans ;
bool ok;
int N;

void dfs(LL n , int zeros){
	if(ok || zeros > 20)
		return ;
	if(n % N == 0){
		ans = n;
		ok = true;
		return ;
	}
	dfs(n*10 , zeros+1);
	dfs(n*10+1 , zeros+1); 
}

int main(){
	while(true){
		cin>>N;
		if(!N)
			break;
		ans = 0;
		ok  = false;
		dfs(1 , 0);
		cout<<ans<<endl;
	}
	return 0;
}
  • 公式,循环:125ms 4776kB
//125ms		4776kB
//输出的是满足题意的最小解 


#include <iostream>

using namespace std;

const int maxn = 1048576+5 ;//2**20

int N;
long long mod[maxn];
long long ans;

int main(){
	while(cin>>N && N){
		mod[0] = 0;
		for(int i=1; ;i++){
			mod[i] = mod[i/2]*10 + i%2;
			if(mod[i] % N == 0){
				ans = mod[i];
				break;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}
  • bfs:969ms 4824kB
//969ms		4824kB


#include <iostream>
#include <queue>

using namespace std;

typedef unsigned long long LL;

int N ;
LL ans;
priority_queue<LL , vector<LL> , greater<LL> > Q;

void bfs(){
	Q.push(1);
	while(!Q.empty()){
		ans = Q.top() ; Q.pop();
		if(ans % N == 0)
			return ;
		Q.push(ans * 10);
		Q.push(ans * 10 + 1);
	}
	return ;
}

int main(){
	while(cin>>N && N){
		while(!Q.empty())
			Q.pop();
		bfs();
		cout<<ans<<endl;
	}
	return 0;
}
OPTIONS: --Werror - If set, changes formatting warnings to errors --assume-filename=<string> - Override filename used to determine the language. When reading from stdin, clang-format assumes this filename to determine the language. --cursor=<uint> - The position of the cursor when invoking clang-format from an editor integration --dry-run - If set, do not actually make the formatting changes --dump-config - Dump configuration options to stdout and exit. Can be used with -style option. --fallback-style=<string> - The name of the predefined style used as a fallback in case clang-format is invoked with -style=file, but can not find the .clang-format file to use. Use -fallback-style=none to skip formatting. --ferror-limit=<uint> - Set the maximum number of clang-format errors to emit before stopping (0 = no limit). Used only with --dry-run or -n --help - Display available options (--help-hidden for more) -i - Inplace edit <file>s, if specified. --length=<uint> - Format a range of this length (in bytes). Multiple ranges can be formatted by specifying several -offset and -length pairs. When only a single -offset is specified without -length, clang-format will format up to the end of the file. Can only be used with one input file. --lines=<string> - <start line>:<end line> - format a range of lines (both 1-based). Multiple ranges can be formatted by specifying several -lines arguments. Can't be used with -offset and -length. Can only be used with one input file. -n - Alias for --dry-run --offset=<uint> - Format a range starting at this byte offset. Multiple ranges can be formatted by specifying several -offset and -length pairs. Can only be used with one input file. --output-replacements-xml - Output replacements as XML. --sort-includes - If set, overrides the include sorting behavior determined by the SortIncludes style flag --style=<string> - Coding style, currently supports: LLVM, Google, Chromium, Mozilla, WebKit. Use -style=file to load style configuration from .clang-format file located in one of the parent directories of the source file (or current directory for stdin). Use -style="{key: value, ...}" to set specific parameters, e.g.: -style="{BasedOnStyle: llvm, IndentWidth: 8}" --verbose - If set, shows the list of processed files --version - Display the version of this program
最新发布
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值