#25475: C++求救..為什麼會TLE?


www.abc35938454@gmail.com (閃電a)

學校 : 國立臺中第一高級中學
編號 : 152583
來源 : [180.217.74.218]
最後登入時間 :
2022-05-24 09:51:27
f866. DD排磁磚 -- 2005美國AMC10 | From: [1.200.147.206] | 發表日期 : 2021-05-23 20:19

#include<iostream>

#include<sstream>

using namespace std;

 

int t,m,qsum;

int child,mother;

string s;//string

struct make {

    char c;

    int q;

};

make l[11];

 

int main() {

    ios::sync_with_stdio(0);

    cin.tie(0);

    cin>>t;

    for(int i=0;i<t;i++){

        qsum=0;

        child=1;

        mother=1;

        cin>>m>>s;

        for(int j=0;j<m;j++) {

            int a;

            cin>>l[j].c>>a;

            l[j].q=a;

            qsum+=a;

        }

        for(int j=0;j<s.length();j++) {

            int k=0;

            while(s[j]!=l[k].c) k++;

            child*=l[k].q;

            mother*=qsum;

            l[k].q--;

            qsum--;

        }

        if(child==1 && mother==1) cout<<"0 DD真幸運!!我要下注!\n";

        if(child==0) cout<<"1\n";

        else {

            int maxx=1;

            for(int j=2;j<=mother/2;j++)

                if(child%j==0 && mother%j==0) maxx=j;

            child/=maxx;

            mother/=maxx;

            cout<<mother-child<<"/"<<mother;

            if(child*2-mother>=0) cout<<" DD真幸運!!我要下注!";

            cout<<'\n';

        }

    }

 

}

 

 

 

 

 
ZeroJudge Forum