## 【Codeforces 788C】The Great Mixing

### Code

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 2009;
const int M = 2000009;
const int BAS = 1000;

int nxt[M],to[M],_hash[M];
queue<int> que;

inline int read() {
char c=getchar(); int f=1,ret=0;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret * f;
}

int main() {
for (int i=1;i<=m;i++) _hash[++tot] = read() - n;
sort(_hash+1, _hash+1+tot);
tot = unique(_hash+1, _hash+1+tot) - _hash - 1;
for (int i=1;i<=tot;i++) {
dis[_hash[i] + BAS] = 1;
que.push(_hash[i] + BAS);
}
while (!que.empty()) {
int w = que.front(); que.pop();
for (int i=1,t;t=w+_hash[i],i<=tot;i++) {
if (t < 0 || t > BAS*2 || dis[t]) continue;
dis[t] = dis[w] + 1; que.push(t);
}
}
cout<<(dis[BAS]?dis[BAS]:-1);
return 0;
}


## 【NOIP十连测】[D1T3] Walk

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 1300000;
const int M = 200000*2+300000+9;

deque<int> que;

char c=getchar(); int ret=0,f=1;
while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
return ret*f;
}

inline void Add_Edge(int u, int v) {
static int T = 0;
to[++T] = v; nxt[T] = head[u]; head[u] = T;
}

inline void BFS() {
memset(dis,-1,sizeof(dis));
dis[1] = 0; que.push_back(1);

while (!que.empty()) {
int w = que.front(); que.pop_front();
if (w <= n) {
if (!~dis[to[i]] || dis[to[i]] > dis[w] + 1) dis[to[i]] = dis[w] + 1, que.push_back(to[i]);
} else {
if (!~dis[to[i]] || dis[to[i]] > dis[w]) dis[to[i]] = dis[w], que.push_front(to[i]);
}
if (w > n) for (int i=0;i<=20;i++) if ((w-n)&(1<<i)) {
int v = ((w-n)^(1<<i)) + n; if (v <= n) continue;
if (!~dis[v] || dis[v] > dis[w]) dis[v] = dis[w], que.push_front(v);
}
}
}

int main(){
freopen("walk.in","r",stdin);
freopen("walk.out","w",stdout);