题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3
题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其它约数也在1到100的组成
就是dfs先枚举b的乘积组合,再看a有没有组合能够乘出来。。
代码:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <functional>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define PB push_back
#define MP make_pair
#define SZ size()
#define CL clear()
#define AA first
#define BB second
#define EPS 1e-8
#define ZERO(x) memset((x),0,sizeof(x))
const int INF = ~0U>>;
const double PI = acos(-1.0); int a,b;
VI aa,bb;
bool vis[];
bool ok;
bool cansmall; bool dfsa(int cur,int now=){
bool res = false;
if(now==a){
return true;
}
if( now>a ) return false;
if( cur<aa.SZ ) res = res || dfsa(cur+,now);
if( cur<aa.SZ&&!vis[aa[cur]] ){
vis[aa[cur]] = true;
res = res||dfsa(cur+,now*aa[cur]);
vis[aa[cur]] = false;
}
return res;
} void dfsb(int cur,int now=){
// printf("now=%d\n",now);
if( ok ) return;
if(now==b){
cansmall = true;
bool flag = dfsa();
if( flag ){
ok = true;
}
return;
}
if( now>a ) return;
if( cur<bb.SZ ) dfsb(cur+,now);
if( cur<bb.SZ&&!vis[bb[cur]] ){
vis[bb[cur]] = ;
dfsb(cur+,now*bb[cur]);
vis[bb[cur]] = ;
}
} int main(){
while(scanf("%d%d",&a,&b)!=EOF){
if(a<b) swap(a,b);
aa.clear();
bb.clear();
for(int i=;i<=;i++){
if( a%i== ) aa.PB(i);
if( b%i== ) bb.PB(i);
} ZERO(vis); ok = cansmall = false; dfsb(); if( ok||!cansmall ) {
printf("%d\n",a);
} else {
printf("%d\n",b);
}
}
return ;
}