146173 Gold Rush(map,pair,dfs)

146173 Gold Rush(map,pair,dfs)_第1张图片

146173 Gold Rush(map,pair,dfs)_第2张图片 

#include
using namespace std;
using ll = long long;
map,int>mp;
bool dfs(int n,int m){
	if(n == m)return true;
	if(n < m) return false;
	if(n % 3)return false;
	if(mp.count({n,m}))return mp[{n,m}]; 
	bool flag = dfs(n/3,m) | dfs(n / 3 * 2,m);
	return mp[{n,m}] = flag;
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n,m;
	int t;
	cin >> t;
	while(t--){
		cin >> n >> m;
		if(dfs(n,m)) cout << "Yes\n";
		else
			cout << "No" << '\n';
	}
	return 0;
}

你可能感兴趣的:(c++,算法,深度优先)