Submission #376484

#TimeUsernameProblemLanguageResultExecution timeMemory
376484astoriaSnake Escaping (JOI18_snake_escaping)C++14
12 / 100
2084 ms12908 KiB
#include "bits/stdc++.h" using namespace std; int l,q,n; int pw[25]; int fast_mod(const int input, const int ceil) { // apply the modulo operator only when needed // (i.e. when the input is greater than the ceiling) return input >= ceil ? input % ceil : input; // NB: the assumption here is that the numbers are positive } bool comp(int a, int b){ vector<int> x,y; int cntx=0,cnty=0; for(int j=0; j<l; j++){ int cr = fast_mod(a,3); a/=3; if(cr==2) cntx++; } for(int j=0; j<l; j++){ int cr = fast_mod(b,3); b/=3; if(cr==2) cnty++; } return cntx<cnty; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin>>l>>q; n=1<<l; string s; cin>>s; int tox[n]; for(int i=0; i<n; i++) tox[i] = s[i]-'0'; pw[0]=1; int k=1; for(int i=1; i<=l; i++){ k*=3; pw[i]=k;} int ans[k]; memset(ans,0,sizeof(ans)); int ord[k]; for(int i=0; i<k; i++) ord[i]=i; //sort(ord,ord+k,comp); if(l<=10) goto acn; for(int i=0; i<n; i++){ int v = ord[i], vk=v; int tot = 0; for(int j=0; j<l; j++){ int cr = fast_mod(v,3); v/=3; tot += ((1<<j)*cr); } ans[vk] = tox[tot]; } for(int ahc=n; ahc<k; ahc++){ int v = ord[ahc], tk = ord[ahc], tt = ord[ahc], vk = ord[ahc]; for(int i=0; i<l; i++){ int cr = fast_mod(v,3); v/=3; if(cr==2){ tk-=(2*pw[i]); tt-=(1*pw[i]); break; } } ans[vk] = ans[tk]+ans[tt]; } acn : for(int ahc=0; ahc<k; ahc++){ vector<int> v; int cr=ahc; for(int j=0; j<l; j++){ v.push_back(cr%3); cr/=3; } for(int i=0; i<n; i++){ bitset<13> b(i); bool f=1; for(int j=0; j<l; j++){ if(v[j]!=2 && (b[j]!=v[j])){ f=0; break;} } if(f) ans[ahc] += tox[i]; } } for(int ahc=0; ahc<q; ahc++){ string s; cin>>s; int qr=0; for(int i=0; i<l; i++){ int pwer = l-i-1; if(s[i]=='1') qr += (pw[pwer]); if(s[i]=='?') qr += (2*pw[pwer]); } cout<<ans[qr]<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...