#include <cstdio> #include <cstring> #include <algorithm> #define MAX 110 using namespace std ; int sg[MAX] ; int num1[200] , num2[200] ; bool graph[MAX][MAX],visited[MAX] ; int get(char ch[] , int num[]) { gets(ch); int len = strlen(ch) , index = 1; for(int i = 0 ; i < len ; ++i) { if(ch[i] == ' ') { ++index ; } else { num[index] = num[index]*10+ch[i]-'0' ; } } return index+1 ; } int len1 , len2 ; int getSG(int index) { bool hash[MAX] , flag = false; memset(hash,false,sizeof(hash)); for(int i = 0 ; i < len1 ; ++i) { if(!visited[i] && graph[index][i]) { flag = true; visited[i] = true ; hash[getSG(i)] = true ; visited[i] = false ; } } for(int i = 0 ; i < MAX ; ++i) { if(!hash[i]) { //if(sg[index] == 0) return sg[index]=i ; } } } int Bsearch(int x) { int i = 1 , j = len1-1 ; while(i<=j) { int mid = (i+j)>>1 ; if(num1[mid]>x) { j = mid-1 ; } else if(num1[mid]<x) { i = mid+1 ; } else { return mid ; } } return i ; } bool cmp(const int a , const int b) { return a<b ; } int main() { char line1[200],line2[200]; len1 = get(line1 , num1) ; len2 = get(line2 , num2) ; sort(num1+1,num1+len1,cmp); sort(num2+1,num2+len2,cmp); for(int i = 1 ; i < len1 ; ++i) { for(int j = 1 ; j < i ; ++j) { if(num1[i]%num1[j] == 0 || num1[j]%num1[i]==0) { graph[i][j] = graph[j][i] = true ; } } } for(int i = 1 ; i < len2 ; ++i) { int x = Bsearch(num2[i]) , j = x; while(num1[--j] == num2[i]) { graph[0][j] = true ; } j = x ; while(num1[j++] == num2[i]) { graph[0][j] = true; } } memset(sg,-1,sizeof(sg)) ; visited[0] = true ; getSG(0) ; if(sg[0] == 0) { puts("-1") ; } else { for(int i = 1 ; i < len2 ; ++i) { int x = Bsearch(num2[i]) ; if(sg[x]==0) { printf("%d\n",num2[i]) ; break ; } } } return 0 ; }
#include <cstdio> #include <cstring> #include <algorithm> #define MAX 110 using namespace std ; int sg[MAX] ; int num1[200] , num2[200] ; bool graph[MAX][MAX],visited[MAX] ; int get(char ch[] , int num[]) { gets(ch); int len = strlen(ch) , index = 1; for(int i = 0 ; i < len ; ++i) { if(ch[i] == ' ') { ++index ; } else { num[index] = num[index]*10+ch[i]-'0' ; } } return index+1 ; } int len1 , len2 ; int getSG(int index) { bool hash[MAX] , flag = false; memset(hash,false,sizeof(hash)); for(int i = 0 ; i < len1 ; ++i) { if(!visited[i] && graph[index][i]) { flag = true; visited[i] = true ; hash[getSG(i)] = true ; visited[i] = false ; } } for(int i = 0 ; i < MAX ; ++i) { if(!hash[i]) { //if(sg[index] == 0) return sg[index]=i ; } } } int Bsearch(int x) { int i = 1 , j = len1-1 ; while(i<=j) { int mid = (i+j)>>1 ; if(num1[mid]>x) { j = mid-1 ; } else if(num1[mid]<x) { i = mid+1 ; } else { return mid ; } } return i ; } bool cmp(const int a , const int b) { return a<b ; } int main() { char line1[200],line2[200]; len1 = get(line1 , num1) ; len2 = get(line2 , num2) ; sort(num1+1,num1+len1,cmp); sort(num2+1,num2+len2,cmp); for(int i = 1 ; i < len1 ; ++i) { for(int j = 1 ; j < i ; ++j) { if(num1[i]%num1[j] == 0 || num1[j]%num1[i]==0) { graph[i][j] = graph[j][i] = true ; } } } for(int i = 1 ; i < len2 ; ++i) { int x = Bsearch(num2[i]) , j = x; while(num1[--j] == num2[i]) { graph[0][j] = true ; } j = x ; while(num1[j++] == num2[i]) { graph[0][j] = true; } } memset(sg,-1,sizeof(sg)) ; visited[0] = true ; getSG(0) ; if(sg[0] == 0) { puts("-1") ; } else { for(int i = 1 ; i < len2 ; ++i) { int x = Bsearch(num2[i]) ; if(sg[x]==0) { printf("%d\n",num2[i]) ; break ; } } } return 0 ; }
蓝桥杯 历届试题 约数倍数选卡片 求大神指点 首先声明,我的代码有问题,!不喜勿进,若有意向,可以讨论,我百度不到这道题的题解
原文地址:http://blog.csdn.net/lionel_d/article/details/44019937