답안 #899247

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
899247 2024-01-05T16:17:36 Z jamjanek Ancient Machine 2 (JOI23_ancient2) C++17
100 / 100
38 ms 1900 KB
#include "ancient2.h"
#include<bits/stdc++.h>
using namespace std;
const int prefixL = 100, sufixL = 101; 
const int R = 1001;
bool prefix(int k){
	int m = k+1+2;
	vector<int>a(m), b(m);
	for(int i=0;i<k;i++){
		a[i] = i+1;
		b[i] = i+1;
	}
	a[k] = k+1;
	b[k] = k+2;
	a[k+2] = k+2;
	b[k+2] = k+2;
	a[k+1] = k+1;
	b[k+1] = k+1;
	return Query(m,a,b)==(k+2);
}

string PoznajSufix(int k){
	string s="2";
	while((int)s.size()-1<k){
		s[0]='0';
		s = '2' + s;
		int m = s.size();
		vector<int>a(m), b(m);
		vector<int>kmp(m);
		kmp[0] = 0;
		kmp[1] = 0;
		for(int i=2;i<m;i++){
			int j = kmp[i-1];
			while(j!=0 && s[j+1]!=s[i])
				j = kmp[j];
			if(s[j+1]==s[i])
				j++;
			kmp[i] = j;
		}
		for(int i=0;i<m-1;i++){
			if(s[i+1]=='0'){
				a[i] = i+1;
				int j = kmp[i];
				while(s[j+1]!='1' && j!=0)j = kmp[j];
				if(s[j+1]=='1')j++;
				b[i] = j;
			}
			else if(s[i+1]=='1'){
				int j = kmp[i];
				while(s[j+1]!='0' && j!=0)j = kmp[j];
				if(s[j+1]=='0')j++;
				a[i] = j;
				b[i] = i+1;
			}
		}
		int j = kmp[m-1];
		while(s[j+1]!='0' && j!=0)j = kmp[j];
		if(s[j+1]=='0')j++;
		a[m-1] = j;

		j = kmp[m-1];
		while(s[j+1]!='1' && j!=0)j = kmp[j];
		if(s[j+1]=='1')j++;
		b[m-1] = j;
		if(Query(m, a, b)!=m-1)
			s[1] = '1';
	}
	reverse(s.begin(), s.end());s.pop_back();reverse(s.begin(), s.end());
	return s;
}

bool parity(int mod, int r){
	int m = 2*mod;
	vector<int>a(m),b(m);
	for(int i=0;i<mod;i++){
		if(i==r){
			a[i] = (i+1)%mod;
			b[i] = mod+(i+1)%mod;
		}
		else{
			a[i] = (i+1)%mod;
			b[i] = (i+1)%mod;
		}
	}
	for(int i=0;i<mod;i++){
		if(i==r){
			a[i+mod] = mod+(i+1)%mod;
			b[i+mod] = (i+1)%mod;
		}
		else{
			a[i+mod] = mod+(i+1)%mod;
			b[i+mod] = mod+(i+1)%mod;
		}
	}
	return Query(m, a, b)>=mod;
}

bitset<R>gauss[R-1];
int przekotna = 0;
void dodaj(bitset<R>X){
	for(int i=0;i<R-1;i++){
		if(X[i]){
			if(gauss[i][i]==0){
				gauss[i] = X;
				przekotna++;
				break;
			}
			X^=gauss[i];
		}
	}
}
bool LiniowoNiezalezne(bitset<R>X){
	for(int i=0;i<R-1;i++){
		if(X[i]){
			if(gauss[i][i]==0)return 1;
			X^=gauss[i];
		}
	}
	return 0;
}

bool odczytaj(bitset<R>X){
	for(int i=0;i<R-1;i++){
		if(X[i]){
			X^=gauss[i];
		}
	}
	return X[R-1];	
}

string Solve(int N) {
	
	string s;
	for(int i=0;i<prefixL;i++){
		bitset<R>pom;
		pom[R-1] = prefix(i);
		pom[i] = 1;
		dodaj(pom);
		//printf("%d\n", (int)pom[R-1]);
//		s.push_back('0'+prefix(s.size()));
	}
	string sufix;
	sufix = PoznajSufix(sufixL);
	for(int i=N-1;i>= N-sufixL;i--){
		bitset<R>pom;
		pom[i] = 1;
		pom[R-1] = (sufix[i-N+sufixL]-'0');
		dodaj(pom);
		//printf(" %d\n", (int)pom[R-1]);
	}
	for(int mod=1;;mod++){
		for(int reszta=0;reszta<mod;reszta++){
			bitset<R>pom;
			for(int i=0;i<R-1;i++)
				if(i%mod==reszta)
					pom[i] = 1;
			if(LiniowoNiezalezne(pom)){
				pom[R-1] = parity(mod, reszta);
				//printf("%d\n", parity(mod, reszta));
				dodaj(pom);
				//printf("%d %d   %d\n", mod, reszta, przekotna);
				//cout<<pom<<"\n";
			}
			if(przekotna==R-1)break;
		}
		if(przekotna==R-1)break;
	}
	for(int i=0;i<R-1;i++){
		bitset<R>pom;
		pom[i] = 1;
		s+='0'+odczytaj(pom);
	}
	//cout<<s<<"\n";
	return s;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 1732 KB Output is correct
2 Correct 30 ms 1232 KB Output is correct
3 Correct 30 ms 1044 KB Output is correct
4 Correct 31 ms 1764 KB Output is correct
5 Correct 30 ms 1296 KB Output is correct
6 Correct 30 ms 1528 KB Output is correct
7 Correct 30 ms 1232 KB Output is correct
8 Correct 31 ms 1044 KB Output is correct
9 Correct 32 ms 1228 KB Output is correct
10 Correct 31 ms 1276 KB Output is correct
11 Correct 30 ms 756 KB Output is correct
12 Correct 30 ms 1488 KB Output is correct
13 Correct 30 ms 1236 KB Output is correct
14 Correct 30 ms 1272 KB Output is correct
15 Correct 30 ms 1528 KB Output is correct
16 Correct 30 ms 1748 KB Output is correct
17 Correct 30 ms 1504 KB Output is correct
18 Correct 31 ms 1484 KB Output is correct
19 Correct 30 ms 1236 KB Output is correct
20 Correct 30 ms 768 KB Output is correct
21 Correct 30 ms 1248 KB Output is correct
22 Correct 32 ms 1236 KB Output is correct
23 Correct 34 ms 1900 KB Output is correct
24 Correct 33 ms 980 KB Output is correct
25 Correct 33 ms 1488 KB Output is correct
26 Correct 30 ms 996 KB Output is correct
27 Correct 34 ms 1492 KB Output is correct
28 Correct 35 ms 972 KB Output is correct
29 Correct 35 ms 1248 KB Output is correct
30 Correct 36 ms 972 KB Output is correct
31 Correct 33 ms 1504 KB Output is correct
32 Correct 38 ms 1480 KB Output is correct
33 Correct 30 ms 1280 KB Output is correct
34 Correct 31 ms 1228 KB Output is correct
35 Correct 33 ms 1552 KB Output is correct
36 Correct 32 ms 1064 KB Output is correct
37 Correct 30 ms 980 KB Output is correct
38 Correct 32 ms 1040 KB Output is correct
39 Correct 31 ms 1048 KB Output is correct
40 Correct 31 ms 1508 KB Output is correct
41 Correct 30 ms 1228 KB Output is correct
42 Correct 32 ms 1472 KB Output is correct
43 Correct 33 ms 1272 KB Output is correct
44 Correct 31 ms 1500 KB Output is correct
45 Correct 31 ms 1792 KB Output is correct
46 Correct 32 ms 1300 KB Output is correct
47 Correct 35 ms 1144 KB Output is correct
48 Correct 32 ms 980 KB Output is correct
49 Correct 32 ms 1488 KB Output is correct
50 Correct 32 ms 1320 KB Output is correct
51 Correct 32 ms 936 KB Output is correct
52 Correct 30 ms 784 KB Output is correct
53 Correct 31 ms 1044 KB Output is correct
54 Correct 31 ms 976 KB Output is correct
55 Correct 30 ms 988 KB Output is correct
56 Correct 30 ms 1240 KB Output is correct
57 Correct 30 ms 1004 KB Output is correct
58 Correct 30 ms 972 KB Output is correct
59 Correct 30 ms 1520 KB Output is correct
60 Correct 31 ms 1060 KB Output is correct
61 Correct 30 ms 1752 KB Output is correct
62 Correct 30 ms 1276 KB Output is correct
63 Correct 30 ms 1492 KB Output is correct