QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#189316 | #2906. XOR Island | FuelTheBurn | AC ✓ | 6ms | 3560kb | C++17 | 14.4kb | 2023-09-27 10:34:32 | 2023-09-27 10:34:33 |
Judging History
answer
#include <bits/stdc++.h>
#include <algorithm> //__gcd(a,b);
using namespace std;
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
using ll = long long;
using ld = long double;
using db = double;
using str = string;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<pair<ll,ll>> vpl;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x) //will not work for bool
#define travBool(a,x) for (auto a: x)
#define print(x) trav(a,x){cout<<a<<" ";}cout<<endl; //will not work for bool
#define printPair(x) trav(a,x){cout<<"{"<<a.first<<" "<<a.second<<"} ";}cout<<endl; //will not work for bool
#define printBool(x) travBool(a,x){cout<<a<<" ";}cout<<endl;
#define print2d(x) trav(a,x){trav(b,a){cout<<b<<" ";}cout<<endl;}
#define print2dBool(x) trav(a,x){travBool(b,a){cout<<b<<" ";}cout<<endl;}
#define doPrefixSum2d(data, prefixSum,x,y){prefixSum.resize(x,vector<ll>(y,0));F0R(i,x){prefixSum[i][0]=0;}F0R(j,y){prefixSum[0][j]=0;}FOR(i,1,x){FOR(j,1,y){prefixSum[i][j]=data[i][j]+prefixSum[i-1][j]+prefixSum[i][j-1]-prefixSum[i-1][j-1];}}}//x and y are the size of prefixSum and data including the first row of 0s
#define printQueue(q){auto copyOfQ=q;if(copyOfQ.empty()){cout<<"empty";}while(!copyOfQ.empty()){cout<<copyOfQ.front()<<" ";copyOfQ.pop();}cout<<endl;}
#define printDeque(q){auto copyOfQ=q;if(copyOfQ.empty()){cout<<"empty";}while(!copyOfQ.empty()){cout<<copyOfQ.front()<<" ";copyOfQ.pop_front();}cout<<endl;}
#define printDequePair(q){auto copyOfQ=q;if(copyOfQ.empty()){cout<<"empty";}while(!copyOfQ.empty()){cout<<"{"<<copyOfQ.front().first<<" "<<copyOfQ.front().second<<"} ";copyOfQ.pop_front();}cout<<endl;}
#define printStack(q){auto copyOfQ=q;if(copyOfQ.empty()){cout<<"empty";}while(!copyOfQ.empty()){cout<<copyOfQ.top()<<" ";copyOfQ.pop();}cout<<endl;} //printed backwards, last in first out occurs first in the print
#define pb push_back
#define pf push_front //for deques
#define rsz resize
#define sz(x) int(x.size())
#define all(x) begin(x), end(x)
#define f first
#define s second
const int MOD = 1e9+7; // 998244353; //this is a completely different unrelated prime number
//const __int128 MOD = 13000000000000019; prime number, for hashing shit
const int MX = 2e5+5;
const ll INF = 1e18; // not too close to LLONG_MAX
const ld PI = acos((ld)-1);
bool debug=false;
bool ysort(const pair<ll,ll>& x, const pair<ll,ll>& y){
return x.s<y.s;
}
ll findSum2d(vector<vector<ll>>& prefixSum,ll bx,ll by,ll ex,ll ey){
return(prefixSum[ex][ey]-prefixSum[bx-1][ey]-prefixSum[ex][by-1]+prefixSum[bx-1][by-1]);
}//all 4 cords are inclusive
vector<bool> prime;// 0 and 1 are counted as prime
vector<ll> primeList;//2 is smallest prime
void precomputePrimes(ll a){
prime.rsz(a+1,true);
FOR(i,2,sqrt(a+1)+2){
if(prime[i]==false) continue;
ll k=2;
while(k*i<=a){
prime[k*i]=false;
k++;
}
}
FOR(i,2,prime.size()){
if(prime[i]){
primeList.pb(i);
}
}
}
struct DSU {
vector<int> e;
DSU(int N) { e = vector<int>(N, -1); }
// get representive component (uses path compression)
int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(int a, int b) { return get(a) == get(b); }
int size(int x) { return -e[get(x)]; }
bool unite(int x, int y) { // union by size, you can change this to return the new leader
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y]; e[y] = x; return true;
}
void p(){
print(e);
}
};
struct BIT {//binary indexed tree AKA fenwick tree
int size;
//bit is vector that includes index 0 to n
vector<int> bit;
BIT(int n) : size(n+1), bit(n + 2) {}
void rsz(ll n){
size=n+1;
bit.rsz(n+2);
}
void update(int x, int v) {
x++;
for (; x <= size; x += x & (-x)) {
bit[x] += v;
}
}
/** @return sum of the values in [0,b] */
int query(int b) {
b++;
int result = 0;
for (; b > 0; b -= b & (-b)) {
result += bit[b];
}
return result;
}
void p(){//first index is -1, last is N
print(bit);
}
void clear(){
size=0;
bit.clear();
}
};
ll modInverse(ll a,ll m){//(euclidean algorithm) can also be done with fermat's little theorem if m is a prime
//inverse of a mod m
vector<pair<ll,pair<ll,ll>>> v;
v.pb({m,{1,0}});
v.pb({a,{0,1}});
while(v[v.size()-1].f!=1){
//cout<<"yay"<<endl;
//cout<<v[v.size()-2].f<<" "<<v[v.size()-2].s.f<<" "<<v[v.size()-2].s.s<<endl;
ll a=v[v.size()-2].f/v[v.size()-1].f;
v.pb({v[v.size()-2].f-v[v.size()-1].f*a,
{v[v.size()-2].s.f-v[v.size()-1].s.f*a,
v[v.size()-2].s.s-v[v.size()-1].s.s*a}});
// cout<<v[v.size()-2].s.f<<endl;
}
//v[v.size()-1].s.s/m=x;
ll ans=v[v.size()-1].s.s;
ans%=m;
ans+=m;
ans%=m;
//cout<<"mod inverse "<<ans<<endl;
return ans;
}
ll fastExponentiation(ll a,ll b,ll m){
ll ans=1;
a%=m;
ll temp=a;
while(b>0){
if(b%2==1){
ans*=temp;
ans%=m;
}
temp*=temp;
temp%=m;
b/=2;
}
ans%=m;
return ans;
}
vector<ll> factorial;
vector<ll> factorialInverse;//factorialInverse[0]=1
ll modInversePrime(ll a,ll p){
if(debug){
cout<<"( 1/"<<a<<" )"<<endl;
}
return fastExponentiation(a,p-2,p);
}
void precomputeFactorial(ll a,ll m){//also computes factorialInverse for prime MOD
factorial.pb(1);
ll ans=1;
FOR(i,1,a+1){
ans*=i;
ans%=m;
factorial.pb(ans);
}
factorialInverse.rsz(a+1);
ans=modInversePrime(factorial[a],MOD);
factorialInverse[a]=ans;
R0F(i,a){
ans*=(i+1);
ans%=MOD;
factorialInverse[i]=ans;
}
}
ll choose(ll a, ll b, ll m){//a choose b mod m
if(debug){
cout<<"( "<<a<<" c "<<b<<" )"<<endl;
}
return ((((factorial[a]*factorialInverse[b])%m)*factorialInverse[a-b])%m);
}
vector<ll> in;//resize to N+1
vector<ll> ot;//resize to N+1
vector<vector<ll>> par; //resize to N+1,vector<ll>(log base 2 MAX (usually about 22))
ll timer=1;//starts at 1
vector<vector<ll>> adjList;
void dfs(int v=1,int p=1)//dfs for euler tours, sets up par (log 2) for lowest common ancestor, as well as creates a list of first and final entry times
{
in[v] = timer++;
par[v][0] = p;
for(int i = 1; i < 22; i++){
par[v][i] = par[par[v][i-1]][i-1];
}
for(int x : adjList[v])
{
if(x == p) continue;
dfs(x,v);
}
ot[v] = timer++;
}
bool anc(int u, int v)
{
return (in[u] <= in[v] && ot[u] >= ot[v]);
}
// method finding all lca's
int lca(int u, int v)
{
if(anc(u,v))
return u;
for(int i = 21; i >= 0; i--)
{
if(par[u][i] >= 0 && !anc(par[u][i], v))
u = par[u][i];
}
return par[u][0];
}
set<ll> powerHash;
vector<ll> prePower;
void precomputePower(ll a,ll m){
ll x=1;
powerHash.insert(x);
F0R(i,1000002){
x*=a;
x%=MOD;
powerHash.insert(x);
}
}
set<ll> powerHash2;
void precomputePower2(ll a,ll m){
ll x=1;
powerHash2.insert(x);
F0R(i,1000002){
x*=a;
x%=MOD;
powerHash2.insert(x);
}
}
set<ll> powerHash3;
void precomputePower3(ll a,ll m){
ll x=1;
powerHash3.insert(x);
F0R(i,1000002){
x*=a;
x%=MOD;
powerHash3.insert(x);
}
}
struct segTree{//0 indexed, has MOD for hashing
vector<ll> v;
ll roundUp;
ll power;
segTree(vector<ll> b){
ll a=b.size();
if(a==1){
power=0;
}
else{
power=log2(a-1);
power++;
}
roundUp=pow(2,power);
v.rsz(2*roundUp,0);
F0R(i,a){
v[i+roundUp]=b[i];
}
ROF(i,1,roundUp){
v[i]=v[2*i]+v[2*i+1];
v[i]%=MOD;
}
}
ll queryRange(ll a,ll b){//a and b both inclusive
ll ans=0;
a+=roundUp;
b+=roundUp;
while(a<=b){
if(a%2==1){
ans+=v[a];
a++;
ans%=MOD;
}
if(b%2==0){
ans+=v[b];
b--;
ans%=MOD;
}
a/=2;
b/=2;
}
return ans;
}
ll queryValue(ll a){
return v[a+roundUp];
}
void updateValue(ll a,ll k){//k gets added to index a
a+=roundUp;
while(a>0){
v[a]+=k;
v[a]%=MOD;
a/=2;
}
}
void p(){
cout<<"segTree"<<endl;
print(v);
}
};
pair<vector<ll>,vector<ll>> factor(ll a){
/*factor(84) will return
{{2 3 7},
{2 1 1}}*/
if(a<=1){
cout<<"Can't factor numbers <=1"<<endl;
}
ll index=0;
vector<ll> pri;//prime
vector<ll> pow;//power
while(primeList[index]*primeList[index]<=a){
if(a%primeList[index]==0){
a/=primeList[index];
if(pri.empty()||pri[pri.size()-1]!=primeList[index]){
pri.pb(primeList[index]);
pow.pb(1);
}
else{
pow[pri.size()-1]++;
}
}
else{
index++;
}
}
if(a!=1){
if(!pri.empty()&&a==pri[pri.size()-1]){
pow[pri.size()-1]++;
}
else{
pri.pb(a);
pow.pb(1);
}
}
pair<vector<ll>,vector<ll>> ret={pri,pow};
return ret;
}
//up down right left by 1
vector<ll> cordChanger1={0,1,0,-1};
vector<ll> cordChanger2={1,0,-1,0};
vector<vector<bool>> fFVisited;//needs to be reset before every flood fill
vector<vector<ll>> grid;
vector<vector<pair<ll,ll>>> fFQ;//resize it by 1 dimension to equal the max distance, the pair is the coordinate
vector<vector<ll>> adjMatrix;//set all values max
void floodFill(ll tempIndex, ll iBorder, ll jBorder){//tempIndex is just a useful variable, can be set to the number of the starting coordinate
//before calling floodFill, points need to be added into fFQ
//remember to clear values like fFVisited before each use, fFQ should auto clear because all this function exits when all points are processed
//currently, negative numbers act as walls, 0 on grid is the movement that increases d, positive numbers can be passed through on the grid do not increase d
//cout<<"grid"<<endl;print2d(grid);
//cout<<"visited"<<endl;print2dBool(fFVisited);
F0R(d,fFQ.size()){//uses a 2d vector to sort; I still named it a Q though.
//cout<<"distance processing "<<d<<endl;
ll fFTempIndex=0;//flood fill temporary index
while(fFQ[d].size()>fFTempIndex){
ll i=fFQ[d][fFTempIndex].f;
ll j=fFQ[d][fFTempIndex].s;
F0R(k,4){
if(i+cordChanger1[k]>=0&&j+cordChanger2[k]>=0&&i+cordChanger1[k]<iBorder&&j+cordChanger2[k]<jBorder){
if(grid[i+cordChanger1[k]][j+cordChanger2[k]]>=0&&fFVisited[i+cordChanger1[k]][j+cordChanger2[k]]==false){
if(grid[i+cordChanger1[k]][j+cordChanger2[k]]==0){//0 on grid is the movement that increases d
fFQ[d+1].pb({i+cordChanger1[k],j+cordChanger2[k]});
}
else{//the movement does not increase d
fFQ[d].pb({i+cordChanger1[k],j+cordChanger2[k]});
}
if(grid[i+cordChanger1[k]][j+cordChanger2[k]]>=1){//this updates whatever condition
adjMatrix[grid[i+cordChanger1[k]][j+cordChanger2[k]]][tempIndex]=min(adjMatrix[grid[i+cordChanger1[k]][j+cordChanger2[k]]][tempIndex],(ll)d);
}
fFVisited[i+cordChanger1[k]][j+cordChanger2[k]]=true;
}
}
}
fFTempIndex++;
}
}
//cout<<visited<<endl;print2dBool(fFVisited);
}
//a is 97
//adjList, adjMatrix and grid are already used variable
ll T=0;//dont forget to clear global arrays after every t
ll N=0;
ll Q=0;
ll M=0;
vector<vector<ll>> modFine;
vector<long double> mnt;
vector<set<pair<long double,ll>>> slopes;
vector<ll> cowEnd;
vector<ll> h;
vector<ll> g;
vector<bool> shaded;
ll contestant=0;
ll best=MOD;
ll tripletSize=0;
vector<vector<ll>> triplet;
void tripletDfs(ll i){
//cout<<i<<endl;
//printBool(shaded);
if(i==tripletSize){
best=min(best,contestant);
//cout<<"reached end"<<endl;
return;
}
if(shaded[triplet[i][0]]==true || shaded[triplet[i][1]]==true || shaded[triplet[i][2]]==true){
tripletDfs(i+1);
}
else{
if(contestant==best){
return;
}
shaded[triplet[i][0]]=true;
contestant++;
tripletDfs(i+1);
contestant--;
shaded[triplet[i][0]]=false;
shaded[triplet[i][1]]=true;
contestant++;
tripletDfs(i+1);
contestant--;
shaded[triplet[i][1]]=false;
shaded[triplet[i][2]]=true;
contestant++;
tripletDfs(i+1);
contestant--;
shaded[triplet[i][2]]=false;
}
return;
}
int main(){
cin>>N;
vector<ll> islanders;
F0R(i,N){
ll a=0;
cin>>a;
islanders.pb(a);
}
shaded.rsz(N,false);
sort(islanders.begin(),islanders.end());
//print(islanders);
F0R(i,N){
FOR(j,i+1,N){
FOR(k,j+1,N){
if((islanders[i]^islanders[j])==islanders[k]){
triplet.pb({i,j,k});
}
}
}
}
tripletSize=triplet.size();
//cout<<triplet.size()<<endl;
//print2d(triplet);
tripletDfs(0);
cout<<best<<endl;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3456kb
input:
3 1 2 3
output:
1
result:
ok single line: '1'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3480kb
input:
11 9 1 14 2 11 7 6 7 6 5 3
output:
3
result:
ok single line: '3'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
6 18053353 18053353 31735788 31735788 16205573 16205573
output:
2
result:
ok single line: '2'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3452kb
input:
9 8324820 8324820 8324820 21703420 21703420 21703420 20196392 20196392 20196392
output:
3
result:
ok single line: '3'
Test #5:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
12 13845321 13845321 13845321 13845321 15244518 15244518 15244518 15244518 3923887 3923887 3923887 3923887
output:
4
result:
ok single line: '4'
Test #6:
score: 0
Accepted
time: 0ms
memory: 3500kb
input:
15 22226925 22226925 22226925 22226925 22226925 11291487 11291487 11291487 11291487 11291487 33516722 33516722 33516722 33516722 33516722
output:
5
result:
ok single line: '5'
Test #7:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
18 15216629 15216629 15216629 15216629 15216629 15216629 9508343 9508343 9508343 9508343 9508343 9508343 7944706 7944706 7944706 7944706 7944706 7944706
output:
6
result:
ok single line: '6'
Test #8:
score: 0
Accepted
time: 1ms
memory: 3484kb
input:
21 13356922 13356922 13356922 13356922 13356922 13356922 13356922 32063243 32063243 32063243 32063243 32063243 32063243 32063243 19066993 19066993 19066993 19066993 19066993 19066993 19066993
output:
7
result:
ok single line: '7'
Test #9:
score: 0
Accepted
time: 1ms
memory: 3508kb
input:
24 5624463 5624463 5624463 5624463 5624463 5624463 5624463 5624463 15098761 15098761 15098761 15098761 15098761 15098761 15098761 15098761 11776262 11776262 11776262 11776262 11776262 11776262 11776262 11776262
output:
8
result:
ok single line: '8'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3524kb
input:
21 10 1 5 1 6 12 14 16 1 13 2 10 16 9 14 2 12 16 13 11 1
output:
3
result:
ok single line: '3'
Test #11:
score: 0
Accepted
time: 0ms
memory: 3468kb
input:
22 9 5 1 2 6 15 8 10 2 1 13 10 10 4 13 16 8 5 7 4 9 3
output:
7
result:
ok single line: '7'
Test #12:
score: 0
Accepted
time: 1ms
memory: 3496kb
input:
23 2 12 8 5 6 15 3 14 3 4 11 2 6 13 1 10 13 6 1 8 1 5 15
output:
7
result:
ok single line: '7'
Test #13:
score: 0
Accepted
time: 1ms
memory: 3448kb
input:
24 4 13 10 8 4 12 10 1 13 16 1 10 5 3 6 3 9 6 14 4 7 4 9 2
output:
7
result:
ok single line: '7'
Test #14:
score: 0
Accepted
time: 0ms
memory: 3528kb
input:
25 13 8 5 2 12 6 12 1 16 14 2 10 2 8 9 3 4 6 9 6 6 7 14 1 9
output:
7
result:
ok single line: '7'
Test #15:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
23 15925942 11051838 20346967 676939 779282 29980798 24484952 27328228 25636253 911824 30521853 23148136 24386971 30992542 4736184 8716393 26656418 1571228 10680642 25754292 7030770 9364603 4593643
output:
1
result:
ok single line: '1'
Test #16:
score: 0
Accepted
time: 0ms
memory: 3440kb
input:
24 14308376 17302931 4737216 25548643 20448845 30163626 24015986 16874513 1363340 25740273 26055670 2065166 22649380 27657047 20575156 5465887 28406514 2855073 29537258 24129214 23999010 30352648 4252441 13105059
output:
1
result:
ok single line: '1'
Test #17:
score: 0
Accepted
time: 0ms
memory: 3560kb
input:
25 7278802 5576962 9887589 28410596 6241564 4736865 29583733 13328658 24907097 21930886 31443909 25339020 20142713 32796255 4539481 28575359 13110054 13738964 14418498 10152417 4040993 5615463 16606164 25405528 15179433
output:
1
result:
ok single line: '1'
Test #18:
score: 0
Accepted
time: 0ms
memory: 3548kb
input:
23 1 131072 1 131072 131073 1 131073 131072 131072 131073 131072 131072 131073 131073 1 131072 131073 131073 1 131072 131073 131072 131073
output:
5
result:
ok single line: '5'
Test #19:
score: 0
Accepted
time: 0ms
memory: 3540kb
input:
23 1050688 1048576 2048 1048576 2048 1048640 1048576 1050624 1050688 1048640 2112 1048640 1048640 1048640 1050624 2048 1048640 1050688 1050624 1048640 1048576 1048576 1048640
output:
4
result:
ok single line: '4'
Test #20:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
24 66560 1024 65536 66560 65536 1024 66560 66560 1024 66560 66560 66560 66560 66560 1024 65536 1024 1024 66560 65536 1024 1024 66560 66560
output:
4
result:
ok single line: '4'
Test #21:
score: 0
Accepted
time: 0ms
memory: 3492kb
input:
24 32 256 32 800 800 512 512 256 256 800 512 544 544 544 512 288 800 800 768 256 512 32 256 256
output:
5
result:
ok single line: '5'
Test #22:
score: 0
Accepted
time: 1ms
memory: 3560kb
input:
25 1056 1024 32 1024 32 1024 32 1056 1056 32 32 1056 32 32 1056 1056 1024 32 1056 1056 1024 32 32 1024 1024
output:
7
result:
ok single line: '7'
Test #23:
score: 0
Accepted
time: 1ms
memory: 3492kb
input:
25 131328 131136 131136 64 131136 64 131072 131392 131136 131392 64 64 320 131136 320 131072 256 131328 131392 131072 131136 131136 131136 320 256
output:
7
result:
ok single line: '7'
Test #24:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
output:
7
result:
ok single line: '7'
Test #25:
score: 0
Accepted
time: 0ms
memory: 3496kb
input:
25 27882881 30145540 6457733 28446363 30443294 7991967 5008288 31792673 3072549 5705914 33449275 27061438 3512639 25153864 14066889 18700493 23374418 13492179 11490902 17179607 20167400 10145641 16271084 14892534 21645431
output:
10
result:
ok single line: '10'
Test #26:
score: 0
Accepted
time: 6ms
memory: 3464kb
input:
25 972439 2867411 2423978 50417 1961336 2373723 2430532 77343 2813644 3655493 1310494 125678 998521 922214 3790115 1885031 3734332 1836950 1233153 3686349 1185264 3740114 1911177 2478773 2763325
output:
11
result:
ok single line: '11'
Test #27:
score: 0
Accepted
time: 1ms
memory: 3500kb
input:
25 2700755 2567664 923683 3765664 1288823 256516 1988688 1946196 969910 2809815 1277666 1086694 212625 913959 54421 3776821 2744646 2755394 1926849 1065075 2364257 2410484 861874 2619749 2002117
output:
9
result:
ok single line: '9'