2015
07-16

# Bit Magic

Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I generated a number table a[N], and wrote a program to calculate the matrix table b[N][N] using three kinds of bit operator. I thought my achievement would get teacher’s attention.
The key function is the code showed below.

There is no doubt that my teacher raised lots of interests in my work and was surprised to my talented programming skills. After deeply thinking, he came up with another problem: if we have the matrix table b[N][N] at first, can you check whether corresponding number table a[N] exists?

There are multiple test cases.
For each test case, the first line contains an integer N, indicating the size of the matrix. (1 <= N <= 500).
The next N lines, each line contains N integers, the jth integer in ith line indicating the element b[i][j] of matrix. (0 <= b[i][j] <= 2 31 – 1)

There are multiple test cases.
For each test case, the first line contains an integer N, indicating the size of the matrix. (1 <= N <= 500).
The next N lines, each line contains N integers, the jth integer in ith line indicating the element b[i][j] of matrix. (0 <= b[i][j] <= 2 31 – 1)

2
0 4
4 0
3
0 1 24
1 0 86
24 86 0

YES
NO

2012长春现场赛的题

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 510
#define NN 1010
#define M 3000100
using namespace std;
int n,b[N][N];

int dfn[NN],low[NN],instack[NN],sstack[NN],belong[NN];
struct Edge{
int v,next;
}edge[M];

edge[cnt].v=v;
}

void init(){
memset(instack,0,sizeof(instack));
memset(dfn,0,sizeof(dfn));
cnt=Index=top=scc=0;
}

void tarjan(int u){
dfn[u]=low[u]=++Index;
sstack[++top]=u;
instack[u]=1;
int v=edge[i].v;
if(dfn[v]==0){
tarjan(v);
low[u]=min(low[v],low[u]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u]){
scc++;
while(1){
int tmp=sstack[top--];
instack[tmp]=0;
belong[tmp]=scc;
if(tmp==u)break;
}
}
}

int main(){
int i,j,k;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&b[i][j]);
int tem=n;
bool ok=1;
for(k=0;k<31;k++){
init();
for(i=0;i<n;i++)
for(j=i+1;j<n;j++){
if(i%2==1 && j%2==1){
if(b[i][j] & (1<<k)){
}
else{
}
}
else if(i%2==0 && j%2==0){
if(b[i][j] & (1<<k)){
}
else{
}
}
else{
if(b[i][j] & (1<<k)){
}
else{
}
}
}
for(i=0;i<tem*2;i++)
if(!dfn[i])
tarjan(i);
bool flag=1;
for(i=0;i<tem;i++)
if(belong[i]==belong[i+tem]){
flag=0;
break;
}
if(flag==0){
ok=0;break;
}
}
if(ok) printf("YES\n");
else printf("NO\n");
}
return 0;
}


,