77 算法理论可过该题,但蒟蒻的我却 TLE 了七个,有任何办法可以优化我的代码使得在不开 O2 的情况下 AC 吗?
代码在下面
#include <bits/stdc++.h>
using namespace std;
int n;
vector <int> p[30];
bool k[30][10];
long long cnt;
long long cou[30][10];
long long ans;
int main(){
// freopen("bgm.in","r",stdin);
// freopen("bgm.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
char opt;
int x;
cin>>opt>>x;
x%=7;
if(k[opt-'@'][x]==0){
p[opt-'@'].push_back(x);
k[opt-'@'][x]=true;
cou[opt-'@'][x]++;
}
else{
cou[opt-'@'][x]++;
}
}
for(int i=1;i<=29;i++){
for(int j=1;j<=9;j++){
if(cou[i][j]!=0){
continue;
}
cou[i][j]=1;
}
}
for(int a=0;a<p['B'-'@'].size();a++){
for(int b=0;b<p['E'-'@'].size();b++){
for(int c=0;c<p['S'-'@'].size();c++){
for(int d=0;d<p['I'-'@'].size();d++){
for(int e=0;e<p['G'-'@'].size();e++){
for(int f=0;f<p['O'-'@'].size();f++){
for(int g=0;g<p['M'-'@'].size();g++){
ans=(p['B'-'@'][a]+p['E'-'@'][b]+p['S'-'@'][c]+p['S'-'@'][c]+p['I'-'@'][d]+p['E'-'@'][b])*(p['G'-'@'][e]+p['O'-'@'][f]+p['E'-'@'][b]+p['S'-'@'][c])*(p['M'-'@'][g]+p['O'-'@'][f]+p['O'-'@'][f]);
if(ans%7==0){
// cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<g<<" "<<ans<<endl;
cnt+=cou['B'-'@'][p['B'-'@'][a]]*cou['E'-'@'][p['E'-'@'][b]]*cou['S'-'@'][p['S'-'@'][c]]*cou['I'-'@'][p['I'-'@'][d]]*cou['G'-'@'][p['G'-'@'][e]]*cou['O'-'@'][p['O'-'@'][f]]*cou['M'-'@'][p['M'-'@'][g]];
// cout<<cou[''-'@'][p['I'-'@'][d]]<<endl;
}
}
}
}
}
}
}
}
cout<<cnt<<endl;
return 0;
}