@@ -25,9 +25,10 @@ using namespace std;
2525#define INF 1000000000 ;
2626typedef long long LL ;
2727
28- int mm[105 ][105 ][105 ];
2928int N, H, C1 , C2 ;
3029
30+ vector<pair<int , int > > ans;
31+
3132void showtime () {
3233 time_t now = time (0 );
3334
@@ -42,10 +43,7 @@ void showtime() {
4243 cout << " The UTC date and time is:" << dt << endl;
4344}
4445
45- int doit (int n, int c1, int c2) {
46- int & ret = mm[n][c1][c2];
47- if (ret != -1 ) return ret;
48-
46+ int isok (int n, int c1, int c2) {
4947 LL sum = 0 ;
5048 int s = c1 + c2;
5149 FOR (b1,1 ,n) {
@@ -77,26 +75,51 @@ int doit(int n, int c1, int c2) {
7775 REP (i,6 ) total *= (n - 2 - i);
7876 total /= 6 * 8 ;
7977
80- if (sum * 4 > total) ret = 1 ;
81- else ret = 0 ;
78+ if (sum * 4 > total) return 1 ;
79+ else return 0 ;
80+ }
81+
82+ void doit (int n) {
83+ if (ans[n].first != -1 ) return ;
84+
85+ vector<pair<int , int > > all;
86+ FOR (s,3 ,n+n-1 ) {
87+ RFOR (i,(s-1 )/2 ,max (1 ,s-n)) {
88+ all.push_back (make_pair (i, s - i));
89+ }
90+ }
91+
92+ int lt = 0 , rt = all.size () - 1 ;
93+ while (lt < rt) {
94+ int mt = (lt + rt) >> 1 ;
95+ if (isok (N, all[mt].first , all[mt].second )) rt = mt;
96+ else lt = mt + 1 ;
97+ }
98+
99+ ans[n] = make_pair (all[lt].first , all[lt].second );
100+ }
82101
83- return ret;
102+ bool cmp (int a, int b, int c, int d) {
103+ if (a + b > c + d) return true ;
104+ if (a + b < c + d) return false ;
105+ return b >= d;
84106}
85107
86108void run () {
87109 scanf (" %d %d" , &N, &H);
110+ doit (N);
88111 REP (i,H) {
89112 scanf (" %d %d" , &C1 , &C2 );
90113 if (C1 > C2 ) swap (C1 , C2 );
91- if (doit (N, C1 , C2 )) printf (" B" );
114+ if (cmp ( C1 , C2 , ans[N]. first , ans[N]. second )) printf (" B" );
92115 else printf (" F" );
93116 }
94117 printf (" \n " );
95118}
96119
97120int main () {
98121 // showtime();
99- memset (mm, -1 , sizeof (mm ));
122+ ans. assign ( 105 , make_pair ( -1 , - 1 ));
100123 int kase;
101124 scanf (" %d" , &kase);
102125 FOR (k,1 ,kase) {
0 commit comments