import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int i = 0, j = 0, k = 0,l = 0;
while (read.hasNext()) {
int s=0;
int n = read.nextInt();
if(n==0){
return;
}
int a[][]=new int[n][n];
int b[][]=new int[n][n];
int c[][]=new int[n][n];
for(i = 0;i<n;i++){
for(j = 0;j<n;j++){
a[i][j]=read.nextInt();
}
}
int m = read.nextInt();
for(l=0;l<m;l++){
int x = read.nextInt();
System.out.println(String.format("关系R的%d次幂:",x));
Relation rel = new Relation(a,n);
for(i=0;i<x-1;i++){
rel.poap(a,x);
}
Set set = new Set(rel,n);
set.serprint();
}
}
}
}
class Relation{
int data[][];
int n;
public Relation(int data[][],int n){
this.data=data;
this.n=n;
}
public void poap(int a[][],int x){
int i = 0,j = 0,k = 0;
int s = 0;
int c[][]=new int[n][n];
for(i = 0;i<n;i++){
for(j = 0;j<n;j++){
s=0;
for(k = 0;k<n;k++){
s+=a[i][k]*data[k][j];
}
c[i][j]=s;
}
}
for(i = 0;i<n;i++){
for(j = 0;j<n;j++){
data[i][j]=c[i][j];
}
}
if(x==0){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j){
data[i][j]=1;
}
else{
data[i][j]=0;
}
}
}
}
}
}
class Set{
Relation a;
int n;
public Set(Relation a,int n){
this.a=a;
this.n=n;
}
public void serprint(){
int i = 0,j=0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (a.data[i][j] > 1) {
a.data[i][j] = 1;
}
System.out.print(a.data[i][j]);
if (j != n - 1) {
System.out.print(" ");
}
}
System.out.println();
}
}
} |
|