题解:P13028 [GCJ 2021 #1A] Hacked Exam

· · 题解

P13028 [GCJ 2021 #1A] Hacked Exam

想法很简单,代码很诗山。

前情提要

出题人不会取模,不然 Q 可开 2 \times 10^5。

思路

注意到 n 最多是 3 所以对每种 n 单独考虑。

当 n = 1 时,是简单的。如果这个人对的多于一半那么听他的,否则不听他的。

当 n = 2 时,也是简单的。计算出来两个人答案相同多少,那么剩下的那些题一定有一个人对一个人错。具体的,设 cnt 是两个人相同的个数,那么可以计算出来两个人相同的部分对的题数是 sc = \frac{s_1 + s_2 - (q - cnt)}{2}。自证不难。于是如果 sc > \lfloor \frac{cnt}{2} \rfloor 那么最后的作答中这些题和前两个人保持一致,否则就回答相反的答案。对于剩下那些两个人答案不一样的题,那么听总共对的多的那个人的,因为他这部分对的一定也多,自证不难。

当 n = 3 时,并不简单了。这时每道题四种情况:

设为 c_{123}, c_{12}, c_{23}, c_{13}。这时候你不知道这每一类里面多数人都对了几个,但你发现一个很好的性质,设 a, b, c, d 为这四类里面多数人对几个。则有 s_1 = a + b + c + c_{23} - d,s_2 = a + b + c_{13} - c + d,s_3 = a + c_{12} - b + c + d。

注意到四个未知数三个式子,所以我们枚举一个未知数,这里枚举 a,于是可以解出来 b, c, d,式子自推不难(都是一次方程能难?)。

对于每一类问题都有两种情况,即和多数人相同和和多数人相反。可以 2^4 枚举每种情况计算对的个数,最后除掉所有情况方案数 \sum \operatorname{C}_{c_{123}}^{a} \times \operatorname{C}_{c_{12}}^{b} \times \operatorname{C}_{c_{13}}^{c} \times \operatorname{C}_{c_{23}}^{d}。

于是做完了。

Code

代码写了快三百行。

#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <cstdio>
#define int __int128
#define rd() read128()
#define pr(a) print128(a)
#define pc(b) putchar(b)
#define pel(c) pr(c), pc('\n')
#define psp(d) pr(d), pc(' ')
using namespace std;
int read128()
{
    int ttpttp = 0, flgflg = 1;
    char chrchr = getchar();
    if(chrchr == '-') flgflg = -1;
    while((chrchr < '0' || chrchr > '9') && chrchr != '-') chrchr = getchar();
    if(chrchr == '-') flgflg = -1, chrchr = getchar();
    while(chrchr >= '0' && chrchr <= '9') ttpttp = ttpttp * 10 + chrchr - '0', chrchr = getchar();
    return ttpttp * flgflg;
}
void print128(int ttpttp)
{
    char char_inserted_list[38];
    if(ttpttp < 0) putchar('-'), ttpttp *= -1;
    int iiiiii = 0;
    while(ttpttp > 0)
    {
        char_inserted_list[++iiiiii] = (char)(ttpttp % 10 + '0');
        ttpttp /= 10;
    }
    if(iiiiii == 0) putchar('0');
    else
    {
        for(int i = iiiiii; i >= 1; i--) putchar(char_inserted_list[i]);
    }
}
int n, q, sc1, sc2, sc3;
string st1, st2, st3;
char ans[120];
int fenzi[16]; // 2^4 枚举情况
namespace NT_algo
{
    struct R
    {
        int aa, mm;
    };
    struct Re
    {
        int xx, yy, gcdd;
    };
    Re exgcd(int a, int b)
    {
        if(b == 0) return (Re){1, 0, a};
        Re res = exgcd(b, a % b);
        int t = res.xx;
        res.xx = res.yy;
        res.yy = t - (a / b) * res.yy;
        return res; 
    }
    int lcm(int a, int b)
    {
        Re res = exgcd(a, b);
        return a * b / res.gcdd;
    }
    R excrt(int a1, int m1, int a2, int m2)
    {
        R res;
        Re res1 = exgcd(m1, -m2);
        res.mm = lcm(m1, m2);
        if((a2 - a1 + res.mm) % res1.gcdd != 0) return {-1, -1};
        int t = (a2 - a1) / res1.gcdd;
        res1.xx = res1.xx * t;
        res1.yy = res1.yy * t;
        res.aa = m1 * res1.xx + a1;
        res.aa += 1000000000000000 * res.mm;
        res.aa %= res.mm;
        return res;
    }
    int QP(int a, int b, int p)
    {
        if(b == 0) return 1;
        int t = QP(a, b / 2, p);
        t = (t * t) % p;
        if(b & 1) return (t * a) % p;
        else return t % p;
    }
    int inv(int num, int p)
    {
        return QP(num, p - 2, p);
    }
}
using namespace NT_algo;
int C[210][210];
signed main()
{
    int T = rd();
    int task = 0;
    for(int i = 0; i <= 200; i++) {
        for(int j = 0; j <= i; j++) {
            if(j == 0) C[i][j] = 1;
            else C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
        }
    }
    while(T--) {
        task++;
        n = rd();
        q = rd();
        if(n == 1) {
            cin >> st1;
            sc1 = rd();
            if(sc1 > q / 2) {
                cout << "Case #";
                pr(task);
                cout << ": ";
                cout << st1 << ' ';
                pr(sc1);
                cout << "/1\n";
            }
            else {
                for(int i = 0; i < q; i++) {
                    if(st1[i] == 'T') st1[i] = 'F';
                    else st1[i] = 'T';
                }
                cout << "Case #";
                pr(task);
                cout << ": ";
                cout << st1 << ' ';
                pr(q - sc1);
                cout << "/1\n";
            }
        }
        else if(n == 2) {
            cin >> st1;
            sc1 = rd();
            cin >> st2;
            sc2 = rd();
            int eqc = 0;
            for(int i = 0; i < q; i++) {
                if(st1[i] == st2[i]) eqc++;
            }
            int eqsc = sc1 + sc2 - (q - eqc);
            //pel(eqsc);
            eqsc /= 2;
            int ansc = 0;
            if(eqsc * 2 < eqc) {
                ansc += eqc - eqsc;
                for(int i = 0; i < q; i++) {
                    if(st1[i] == st2[i] && st1[i] == 'T') ans[i] = 'F';
                    else if(st1[i] == st2[i]) ans[i] = 'T';
                    else if(sc1 > sc2) ans[i] = st1[i];
                    else ans[i] = st2[i];
                }
            }
            else {
                ansc += eqsc;
                for(int i = 0; i < q; i++) {
                    if(st1[i] == st2[i] && st1[i] == 'T') ans[i] = 'T';
                    else if(st1[i] == st2[i]) ans[i] = 'F';
                    else if(sc1 > sc2) ans[i] = st1[i];
                    else ans[i] = st2[i];
                }
            }
            ansc += max(sc1 - eqsc, sc2 - eqsc);
            cout << "Case #";
            pr(task);
            cout << ": ";
            for(int i = 0; i < q; i++) cout << ans[i];
            cout << ' ';
            pr(ansc);
            cout << "/1\n";
        }
        else {
            cin >> st1;
            sc1 = rd();
            cin >> st2;
            sc2 = rd();
            cin >> st3;
            sc3 = rd();
            int c123, c12, c23, c13;
            c123 = c12 = c13 = c23 = 0;
            for(int i = 0; i < q; i++) {
                if(st1[i] == st2[i] && st1[i] == st3[i]) c123++;
                else if(st1[i] == st2[i]) c12++;
                else if(st1[i] == st3[i]) c13++;
                else c23++;
            }
            for(int i = 0; i < 16; i++) fenzi[i] = 0;
            int fenmu = 0;
            for(int i = 0; i <= min(min(min(c123, sc1), sc2), sc3); i++) {
                sc1 -= i + c23;
                sc2 -= i + c13;
                sc3 -= i + c12;
                int sm = sc1 + sc2 + sc3;
                int cor2 = (sm - sc1) / 2, cor3 = (sm - sc2) / 2, cor4 = (sm - sc3) / 2;
                sc1 += i + c23;
                sc2 += i + c13;
                sc3 += i + c12;
                //psp(sm), psp(c123), psp(i), psp(c23), psp(cor2), psp(c13), psp(cor3), psp(c12), pel(cor4);
                //cerr << "foo\n";
                if(cor2 >= 0 && cor2 <= c23 && cor3 >= 0 && cor3 <= c13 && cor4 >= 0 && cor4 <= c12 && sc1 == i + c23 - cor2 + cor3 + cor4 && sc2 == i + c13 - cor3 + cor2 + cor4 && sc3 == i + c12 - cor4 + cor3 + cor2) {
                    //psp(c123), psp(i), psp(c12), psp(cor2), psp(c13), psp(cor3), psp(c23), pel(cor4);
                    int cnt = C[c123][i] * C[c23][cor2] * C[c13][cor3] * C[c12][cor4];
                    //psp(cnt), pel(i);
                    fenmu += cnt;
                    for(int j = 0; j < 16; j++) { // 1 rev 0 keep
                        int tmp = 0;
                        if(j & 1) tmp += c123 - i;
                        else tmp += i;
                        if((j >> 1) & 1) tmp += c23 - cor2;
                        else tmp += cor2;
                        if((j >> 2) & 1) tmp += c13 - cor3;
                        else tmp += cor3;
                        if((j >> 3) & 1) tmp += c12 - cor4;
                        else tmp += cor4;
                        fenzi[j] += cnt * tmp;
                    }
                }
                //cerr << "goo\n";
            }
            //cerr << "oog";
            int mxp = 0;
            for(int i = 1; i < 16; i++)
                if(fenzi[i] > fenzi[mxp]) mxp = i;
            //cerr << "gog";
            for(int i = 0; i < q; i++) {
                if(st1[i] == st2[i] && st1[i] == st3[i]) {
                    if(mxp & 1) {
                        if(st1[i] == 'T') ans[i] = 'F';
                        else ans[i] = 'T';
                    }
                    else {
                        if(st1[i] == 'T') ans[i] = 'T';
                        else ans[i] = 'F';
                    }
                }
                else if(st2[i] == st3[i]) {
                    if((mxp >> 1) & 1) {
                        if(st2[i] == 'T') ans[i] = 'F';
                        else ans[i] = 'T';
                    }
                    else {
                        if(st2[i] == 'T') ans[i] = 'T';
                        else ans[i] = 'F';
                    }
                }
                else if(st1[i] == st3[i]) {
                    if((mxp >> 2) & 1) {
                        if(st1[i] == 'T') ans[i] = 'F';
                        else ans[i] = 'T';
                    }
                    else {
                        if(st1[i] == 'T') ans[i] = 'T';
                        else ans[i] = 'F';
                    }
                }
                else {
                    if((mxp >> 3) & 1) {
                        if(st1[i] == 'T') ans[i] = 'F';
                        else ans[i] = 'T';
                    }
                    else {
                        if(st1[i] == 'T') ans[i] = 'T';
                        else ans[i] = 'F';
                    }
                }
            }
            //cerr << "ogo";
            int ttt = exgcd(fenzi[mxp], fenmu).gcdd;
            fenzi[mxp] /= ttt, fenmu /= ttt;
            cout << "Case #";
            pr(task);
            cout << ": ";
            for(int i = 0; i < q; i++) cout << ans[i];
            cout << ' ' ;
            pr(fenzi[mxp]);
            cout << '/';
            pr(fenmu);
            cout << '\n';
        }
    }
    return 0;
}