#include #include #include #include #include #include /* ACMプログラミング・コンテスト 国内予選 2009 Problem E カードゲーム 問題は、青、赤のカードセットから1枚ずつを取り出し、2以上の共通の約数がある場合にペアとしてカードセットから取り出し、 最も多くのペアを作成する組み合わせを見つけることである。全探索ですべてのカードの組み合わせについて調べる方法の場合、 実時間で計算を終了することができない。そこで、最もペアになる可能性のないものから順にペアを作成する方法をとった。 また、計算時間を短くするために、何度も割り算をしないようにまず素数を数多く作成しておく。ただし、カードの取りうる値以下の すべての素数を配列に保存することができなかったため、できるだけ多くの素数を保存し、それを超える場合には、奇数の数字で 1つづつ割れるかどうかをチェックするようにしている。 */ // 予め作成しておく素数の数 #define SOSU_COUNT 190000 // ペアになる可能性のあるカードが残っているかどうかを確認する。 // 1つもペアになる可能性がない場合、1を返す。 // まだ、ペアになる可能がある場合、0を返す。 int finished(char relations[][500],int m,int n) { for(int bcnt=0;bcntn?(m+1):(n+1); int min_count_index = -1; int min_count_type = -1; for(int type=1;type<=2;type++){ int loop = type==1?m:n; int t_loop = type==1?n:m; for(int cnt=0;cntcount)){ min_count = count; min_count_index = cnt; min_count_type = type; } if(min_count == 1){ break; } } } //printf("min_count=%d, min_count_index=%d, min_count_type=%d\n",min_count,min_count_index,min_count_type); int t_index = remove(min_count_type,relations,min_count_index,min_count_type==1?n:m,min_count_type==1?m:n); //printf("t_index=%d\n",t_index); if(t_index != -1){ remove(min_count_type==1?2:1,relations,t_index,min_count_type==1?m:n,0); pair++; } //printRelations(relations,m,n); } printf("%d\n",pair); } // printf("finished\n"); // getch(); return 0; }