Submission #925568

# Submission time Handle Problem Language Result Execution time Memory
925568 2024-02-12T05:19:33 Z ttamx Ancient Machine 2 (JOI23_ancient2) C++17
100 / 100
43 ms 2196 KB
#include "ancient2.h"
#include<bits/stdc++.h>

using namespace std;

const int N=1000;
const int Q=102;

using BS = bitset<N>;
using VBS = vector<BS>;

struct Basis{
	VBS basis;
	int n,cnt;
	Basis(){}
	Basis(int _n){
		init(_n);
	}
	void init(int _n){
		n=_n;
		cnt=0;
		basis.assign(_n,BS{});
	}
	bool insert(BS v){
		for(int i=0;i<n;i++){
			if(v[i]){
				if(basis[i][i]){
					v^=basis[i];
				}else{
					basis[i]=v;
					cnt++;
					return true;
				}
			}
		}
		return false;
	}
};

void gaussian_elimation(int n,VBS &a,vector<int> &b){
	for(int i=0;i<n;i++){
		int id=i;
		while(!a[id][i])id++;
		swap(a[i],a[id]);
		swap(b[i],b[id]);
		for(int j=0;j<n;j++)if(i!=j&&a[j][i]){
			a[j]^=a[i];
			b[j]^=b[i];
		}
	}
}

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

vector<int> solve_left(int n){
	vector<int> ans(n);
	int m=n+2;
	vector<int> a(m),b(m);
	a[n]=b[n]=n;
	a[n+1]=b[n+1]=n+1;
	for(int i=0;i<n;i++){
		a[i]=n;
		b[i]=n+1;
	}
	for(int i=0;i<n;i++){
		int res=Query(m,a,b);
		ans[i]=res-n;
		a[i]=b[i]=i+1;
	}
	return ans;
}

vector<int> solve_right(int n){
	vector<int> ans;
	for(int i=0;i<n;i++){
		ans.insert(ans.begin(),1);
		int m=i+2;
		vector<vector<int>> a(2,vector<int>(m));
		for(int j=0;j<m;j++){
			for(int c=0;c<2;c++){
				int sz=min(j+1,i+1);
				while(sz>0){
					vector<int> res(ans.begin()+j-sz+1,ans.begin()+j);
					res.emplace_back(c);
					vector<int> res2(ans.begin(),ans.begin()+sz);
					if(res==res2)break;
					sz--;
				}
				a[c][j]=sz;
			}
		}
		ans[0]=Query(m,a[0],a[1])==i+1;
	}
	return ans;
}

string Solve(int n) {
	Basis basis(n);
	VBS mat;
	vector<int> ans;
	auto vl=solve_left(min(n/2,Q-2));
	for(int i=0;i<vl.size();i++){
		BS res;
		res[i]=true;
		if(basis.insert(res)){
			mat.emplace_back(res);
			ans.emplace_back(vl[i]);
			if(basis.cnt==n)break;
		}
	}
	auto vr=solve_right(min((n+1)/2,Q-1));
	for(int i=0;i<vr.size();i++){
		BS res;
		res[n-vr.size()+i]=true;
		if(basis.insert(res)){
			mat.emplace_back(res);
			ans.emplace_back(vr[i]);
			if(basis.cnt==n)break;
		}
	}
	for(int m=2;m<=Q/2;m++){
		for(int r=0;r<m;r++){
			BS res;
			for(int i=r;i<n;i+=m)res[i]=true;
			if(basis.insert(res)){
				mat.emplace_back(res);
				ans.emplace_back(ask(n,m,r));
				if(basis.cnt==n)break;
 			}
		}
		if(basis.cnt==n)break;
	}
	gaussian_elimation(n,mat,ans);
	string res="";
	for(auto x:ans)res.push_back('0'+x);
	return res;
}

Compilation message

ancient2.cpp: In function 'std::string Solve(int)':
ancient2.cpp:113:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |  for(int i=0;i<vl.size();i++){
      |              ~^~~~~~~~~~
ancient2.cpp:123:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  123 |  for(int i=0;i<vr.size();i++){
      |              ~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 38 ms 1152 KB Output is correct
2 Correct 35 ms 1120 KB Output is correct
3 Correct 39 ms 1736 KB Output is correct
4 Correct 39 ms 1484 KB Output is correct
5 Correct 34 ms 1888 KB Output is correct
6 Correct 35 ms 1432 KB Output is correct
7 Correct 35 ms 1428 KB Output is correct
8 Correct 35 ms 900 KB Output is correct
9 Correct 35 ms 1688 KB Output is correct
10 Correct 36 ms 1668 KB Output is correct
11 Correct 35 ms 1020 KB Output is correct
12 Correct 36 ms 1972 KB Output is correct
13 Correct 36 ms 1520 KB Output is correct
14 Correct 34 ms 956 KB Output is correct
15 Correct 35 ms 1340 KB Output is correct
16 Correct 35 ms 1116 KB Output is correct
17 Correct 38 ms 1408 KB Output is correct
18 Correct 41 ms 1228 KB Output is correct
19 Correct 34 ms 1112 KB Output is correct
20 Correct 35 ms 1872 KB Output is correct
21 Correct 37 ms 1104 KB Output is correct
22 Correct 35 ms 860 KB Output is correct
23 Correct 34 ms 1228 KB Output is correct
24 Correct 35 ms 1780 KB Output is correct
25 Correct 36 ms 1860 KB Output is correct
26 Correct 35 ms 1644 KB Output is correct
27 Correct 36 ms 1372 KB Output is correct
28 Correct 41 ms 1868 KB Output is correct
29 Correct 38 ms 1328 KB Output is correct
30 Correct 35 ms 1412 KB Output is correct
31 Correct 36 ms 1460 KB Output is correct
32 Correct 37 ms 1664 KB Output is correct
33 Correct 36 ms 1960 KB Output is correct
34 Correct 38 ms 1464 KB Output is correct
35 Correct 37 ms 812 KB Output is correct
36 Correct 37 ms 1656 KB Output is correct
37 Correct 38 ms 1640 KB Output is correct
38 Correct 37 ms 1904 KB Output is correct
39 Correct 35 ms 1092 KB Output is correct
40 Correct 36 ms 2008 KB Output is correct
41 Correct 35 ms 1324 KB Output is correct
42 Correct 36 ms 1400 KB Output is correct
43 Correct 34 ms 1492 KB Output is correct
44 Correct 35 ms 1500 KB Output is correct
45 Correct 37 ms 2196 KB Output is correct
46 Correct 37 ms 1356 KB Output is correct
47 Correct 40 ms 1344 KB Output is correct
48 Correct 36 ms 1448 KB Output is correct
49 Correct 39 ms 848 KB Output is correct
50 Correct 35 ms 1400 KB Output is correct
51 Correct 37 ms 1956 KB Output is correct
52 Correct 35 ms 1496 KB Output is correct
53 Correct 35 ms 1276 KB Output is correct
54 Correct 37 ms 1868 KB Output is correct
55 Correct 37 ms 1060 KB Output is correct
56 Correct 37 ms 1972 KB Output is correct
57 Correct 38 ms 1372 KB Output is correct
58 Correct 35 ms 1344 KB Output is correct
59 Correct 35 ms 1156 KB Output is correct
60 Correct 43 ms 1620 KB Output is correct
61 Correct 34 ms 860 KB Output is correct
62 Correct 35 ms 1356 KB Output is correct
63 Correct 35 ms 1144 KB Output is correct