题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3
题目大意:给你a,b两个数,问当b由约数1到100组成时,a能否由其它约数也在1到100的组成
就是dfs先枚举b的乘积组合,再看a有没有组合能够乘出来。。
代码:
1 #include <cstdio> 2 #include <cstdlib> 3 #include <string> 4 #include <iostream> 5 #include <cstring> 6 #include <algorithm> 7 #include <cctype> 8 #include <vector> 9 #include <map> 10 #include <set> 11 #include <iterator> 12 #include <functional> 13 #include <cmath> 14 #include <numeric> 15 using namespace std; 16 typedef long long LL; 17 typedef pair<int,int> PII; 18 typedef vector<int> VI; 19 #define PB push_back 20 #define MP make_pair 21 #define SZ size() 22 #define CL clear() 23 #define AA first 24 #define BB second 25 #define EPS 1e-8 26 #define ZERO(x) memset((x),0,sizeof(x)) 27 const int INF = ~0U>>1; 28 const double PI = acos(-1.0); 29 30 int a,b; 31 VI aa,bb; 32 bool vis[111]; 33 bool ok; 34 bool cansmall; 35 36 bool dfsa(int cur,int now=1){ 37 bool res = false; 38 if(now==a){ 39 return true; 40 } 41 if( now>a ) return false; 42 if( cur<aa.SZ ) res = res || dfsa(cur+1,now); 43 if( cur<aa.SZ&&!vis[aa[cur]] ){ 44 vis[aa[cur]] = true; 45 res = res||dfsa(cur+1,now*aa[cur]); 46 vis[aa[cur]] = false; 47 } 48 return res; 49 } 50 51 void dfsb(int cur,int now=1){ 52 // printf("now=%d\n",now); 53 if( ok ) return; 54 if(now==b){ 55 cansmall = true; 56 bool flag = dfsa(0); 57 if( flag ){ 58 ok = true; 59 } 60 return; 61 } 62 if( now>a ) return; 63 if( cur<bb.SZ ) dfsb(cur+1,now); 64 if( cur<bb.SZ&&!vis[bb[cur]] ){ 65 vis[bb[cur]] = 1; 66 dfsb(cur+1,now*bb[cur]); 67 vis[bb[cur]] = 0; 68 } 69 } 70 71 int main(){ 72 while(scanf("%d%d",&a,&b)!=EOF){ 73 if(a<b) swap(a,b); 74 aa.clear(); 75 bb.clear(); 76 for(int i=1;i<=100;i++){ 77 if( a%i==0 ) aa.PB(i); 78 if( b%i==0 ) bb.PB(i); 79 } 80 81 ZERO(vis); 82 83 ok = cansmall = false; 84 85 dfsb(0); 86 87 if( ok||!cansmall ) { 88 printf("%d\n",a); 89 } else { 90 printf("%d\n",b); 91 } 92 } 93 return 0; 94 }
浙公网安备 33010602011771号