forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
int N; | ||
char bit[1002][1002]; | ||
|
||
void update(int x, int y){ | ||
++x; ++y; | ||
|
||
for(int qx = x;qx <= N + 1;qx += qx & -qx) | ||
for(int qy = y;qy <= N + 1;qy += qy & -qy) | ||
bit[qx][qy] ^= 1; | ||
} | ||
|
||
int query(int x, int y){ | ||
++x; ++y; | ||
|
||
int ret = 0; | ||
|
||
for(int qx = x;qx > 0;qx -= qx & -qx) | ||
for(int qy = y;qy > 0;qy -= qy & -qy) | ||
ret ^= bit[qx][qy]; | ||
|
||
return ret; | ||
} | ||
|
||
int main(){ | ||
ios::sync_with_stdio(false); | ||
|
||
int T,Q; | ||
int x1,y1,x2,y2,x,y; | ||
char op; | ||
bool first = true; | ||
|
||
cin >> T; | ||
|
||
while(T--){ | ||
cin >> N >> Q; | ||
|
||
memset(bit,0,sizeof bit); | ||
|
||
if(!first) cout << '\n'; | ||
first = false; | ||
|
||
while(Q--){ | ||
cin >> op; | ||
|
||
if(op == 'C'){ | ||
cin >> x1 >> y1 >> x2 >> y2; | ||
|
||
update(x2,y2); | ||
update(x2,y1 - 1); | ||
update(x1 - 1,y2); | ||
update(x1 - 1,y1 - 1); | ||
}else{ | ||
cin >> x >> y; | ||
|
||
cout << (query(N,N) ^ query(x - 1,N) ^ query(N,y - 1) ^ query(x - 1,y - 1)) << '\n'; | ||
} | ||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
int exp(int n){ | ||
int ret = 0; | ||
|
||
while(n){ | ||
ret += (n >> 1); | ||
n >>= 1; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int main(){ | ||
int n,k; | ||
|
||
while(~scanf("%d %d",&n,&k)){ | ||
if(exp(n) - exp(k) - exp(n - k) > 0) puts("0"); | ||
else puts("1"); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
#define MAXN 20001 | ||
#define MAXE 200000 | ||
|
||
int E = 0,to[MAXE],nxt[MAXE],last[MAXN]; | ||
|
||
void add_edge(int u, int v){ | ||
to[E] = v; nxt[E] = last[u]; last[u] = E++; | ||
to[E] = u; nxt[E] = last[v]; last[v] = E++; | ||
} | ||
|
||
int parent[MAXN],level[MAXN]; | ||
vector<int> in[MAXN]; | ||
|
||
void dfs(int pos, int lvl){ | ||
level[pos] = lvl; | ||
bool back = true; | ||
|
||
for(int e = last[pos];e != -1;e = nxt[e]){ | ||
int x = to[e]; | ||
|
||
if(x == parent[pos]){ | ||
if(!back) in[x].push_back(pos); | ||
back = false; | ||
}else if(parent[x] != -1){ | ||
if(level[x] < lvl) | ||
in[x].push_back(pos); | ||
}else{ | ||
parent[x] = pos; | ||
|
||
dfs(x,lvl + 1); | ||
} | ||
} | ||
|
||
int sz = in[pos].size(); | ||
bool odd = false; | ||
|
||
for(int i = 0;i < sz;i += 2){ | ||
if(i + 1 < sz){ | ||
printf("%d %d %d\n",in[pos][i],pos,in[pos][i + 1]); | ||
}else{ | ||
odd = true; | ||
printf("%d %d %d\n",in[pos][i],pos,parent[pos]); | ||
} | ||
} | ||
|
||
if(!odd) | ||
in[ parent[pos] ].push_back(pos); | ||
} | ||
|
||
int main(){ | ||
int n,m; | ||
|
||
scanf("%d %d",&n,&m); | ||
|
||
memset(last,-1,sizeof last); | ||
|
||
for(int i = 0,u,v;i < m;++i){ | ||
scanf("%d %d",&u,&v); | ||
add_edge(u,v); | ||
} | ||
|
||
memset(parent,-1,sizeof parent); | ||
parent[1] = 0; | ||
|
||
dfs(1,0); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#include <cstdio> | ||
#include <cmath> | ||
#include <algorithm> | ||
#include <map> | ||
|
||
using namespace std; | ||
|
||
int pow(long long a, int b, int c){ | ||
long long ret = 1; | ||
|
||
while(b){ | ||
if(b & 1) ret = ret * a % c; | ||
a = a * a % c; | ||
b >>= 1; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int r; | ||
pair<int, int> baby[31622],giant[31622]; | ||
|
||
bool cmp(pair<int, int> a, pair<int, int> b){ | ||
if(a.first != b.first) return a.first < b.first; | ||
return a.second > b.second; | ||
} | ||
|
||
int main(){ | ||
int x,z,m; | ||
|
||
while(true){ | ||
scanf("%d %d %d",&x,&m,&z); | ||
if(x == 0) break; | ||
|
||
z %= m; | ||
|
||
if(m == 1 || z == 1) puts("0"); | ||
else{ | ||
int ans = -1; | ||
|
||
if(pow(x,31,m) == 0){ | ||
long long aux = 1; | ||
|
||
for(int i = 0;i < 32;++i){ | ||
if(aux == z){ | ||
ans = i; | ||
break; | ||
} | ||
|
||
aux = x * aux % m; | ||
} | ||
}else{ | ||
r = (int)floor(sqrt(m)); | ||
int max_val = (m + r - 1) / r; | ||
long long p = z; | ||
|
||
for(int i = 0;i < r;++i){ | ||
baby[i] = make_pair(p % m,i); | ||
p = p * x % m; | ||
} | ||
|
||
sort(baby,baby + r,cmp); | ||
|
||
int aux = pow(x,r,m); | ||
p = aux; | ||
|
||
for(int i = 0,e = r;i < max_val;++i, e += r){ | ||
giant[i] = make_pair(p,e); | ||
p = p * aux % m; | ||
} | ||
|
||
sort(giant,giant + max_val); | ||
|
||
for(int i = 0,j = 0;i < max_val && j < r;++i){ | ||
while(j < r && baby[j].first < giant[i].first) ++j; | ||
|
||
if(j < r && baby[j].first == giant[i].first) | ||
if(ans == -1 || giant[i].second - baby[j].second < ans) | ||
ans = giant[i].second - baby[j].second; | ||
} | ||
} | ||
|
||
if(ans == -1) puts("No Solution"); | ||
else printf("%d\n",ans); | ||
|
||
} | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
int main(){ | ||
int N; | ||
|
||
while(scanf("%d",&N) == 1){ | ||
if(N & (N - 1)) puts("NO"); | ||
else puts("YES"); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
#define MAXV 100000 | ||
#define MAXE 400000 | ||
|
||
int to[MAXE],nxt[MAXE],last[MAXV],E; | ||
int level[MAXV],par[MAXV],low[MAXV]; | ||
bool visited[MAXV],bridge[MAXV]; | ||
|
||
void add_edge(int u, int v){ | ||
to[E] = v; nxt[E] = last[u]; last[u] = E++; | ||
to[E] = u; nxt[E] = last[v]; last[v] = E++; | ||
} | ||
|
||
int bridges; | ||
|
||
void dfs(int cur, int lvl){ | ||
visited[cur] = true; | ||
level[cur] = low[cur] = lvl; | ||
bool rep = false; | ||
|
||
for(int e = last[cur];e != -1;e = nxt[e]){ | ||
if(to[e] == par[cur] && !rep) rep = true; | ||
else if(visited[ to[e] ]) low[cur] = min(low[cur],level[ to[e] ]); | ||
else{ | ||
par[ to[e] ] = cur; | ||
dfs(to[e],lvl + 1); | ||
low[cur] = min(low[cur],low[ to[e] ]); | ||
|
||
if(low[ to[e] ] > lvl){ | ||
bridge[ to[e] ] = true; | ||
++bridges; | ||
} | ||
} | ||
} | ||
} | ||
|
||
void update(int u, int v){ | ||
if(level[u] < level[v]) swap(u,v); | ||
|
||
while(level[u] != level[v]){ | ||
if(bridge[u]){ | ||
--bridges; | ||
bridge[u] = false; | ||
} | ||
|
||
u = par[u]; | ||
} | ||
|
||
while(u != v){ | ||
if(bridge[u]){ | ||
--bridges; | ||
bridge[u] = false; | ||
} | ||
|
||
if(bridge[v]){ | ||
--bridges; | ||
bridge[v] = false; | ||
} | ||
|
||
u = par[u]; | ||
v = par[v]; | ||
} | ||
} | ||
|
||
int main(){ | ||
int tc = 1,N,M,Q; | ||
|
||
while(true){ | ||
scanf("%d %d",&N,&M); | ||
if(N == 0) break; | ||
|
||
memset(last,-1,sizeof last); | ||
E = 0; | ||
|
||
for(int i = 0,u,v;i < M;++i){ | ||
scanf("%d %d",&u,&v); | ||
add_edge(u - 1,v - 1); | ||
} | ||
|
||
memset(visited,false,sizeof visited); | ||
memset(bridge,false,sizeof bridge); | ||
par[0] = -1; | ||
bridges = 0; | ||
dfs(0,0); | ||
|
||
printf("Case %d:\n",tc++); | ||
scanf("%d",&Q); | ||
|
||
for(int q = 0,u,v;q < Q;++q){ | ||
scanf("%d %d",&u,&v); | ||
update(u - 1,v - 1); | ||
|
||
printf("%d\n",bridges); | ||
} | ||
|
||
printf("\n"); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.