ccpc补题1011(思维)

http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1011&cid=909

Given an n×n matrix A and a 3×3 matrix K. These two matrices are very special : they are both non-negative matrices and the sum of all elements in matrix K is 1 (In order to avoid floating-point error, we will give matrix K in a special way in input).


Now we define a function C(A,K), the value of C(A,K) is also a n×n matrix and it is calculated below(we use C to abbreviate C(A,K)):

Cx,y=min(nx+1,3)i=1min(ny+1,3)j=1  Ax+i1,y+j1Ki,j

Now we define Cm(A,K)=C(Cm1(A,K),K) and C1(A,K)=C(A,K), Kanade wants to know limtCt(A,K)

It's guaranteed that the answer exists and is an integer matrix.

 


Input
There are T test cases in this problem.

The first line has one integer T.

Then for every test case:

The first line has one integer n.

Then there are n lines and each line has n non negative integers. The j-th integer of the i-th row denotes Ai,j

Then there are 3 lines and each line has 3 non negative integers. The j-th integer of the i-th row denotes Ki,j

Then K could be derived from K by the following formula:
Ki,j=Ki,j/(x=13y=13Kx,y)


1T100

3n50

0Ai,j1000

0Ki,j1000

3x=13y=1Kx,y>0
 


Output
For each test case, output the answer matrix by using the same format as the matrix A in input.
 
Sample Input
2
3
1 2 3
4 5 6
7 8 9
3 0 0
0 0 0
0 0 0
3
1 2 3
4 5 6
7 8 9
1 0 0
0 1 0
0 0 0
 

Sample Output

1 2 3
4 5 6
7 8 9
0 0 0
0 0 0
0 0 0

题意:

定义一个函数C,参数为两个矩阵A和K,其中A是n*n矩阵,K是3*3矩阵,K是一个分数矩阵,Ki,j=K'i,j / ∑K'i,j,其中K‘是输入的整数矩阵,这意味着∑Ki,j=1

C的值由图里的公式求得

问对于同一个K,拿C套娃无穷次得到的矩阵

理解:

 对于K矩阵,只要不是该矩阵只有一个数不为0,矩阵的每个元素值就都小于1;

 Ax+i1,y+j1 × Ki,j ,无限套娃以后K的元素如果小于一结果就是0;

根据公式可以看出,超出A的部分直接被丢弃了;

如果K不满足只有左上角不为0的条件,那么结果就是全0,否则结果是A原矩阵,例如:

1 2 3 4       0 1 0

1 2 3 4  与 0 0 0  ,乘第一次会变成:

1 2 3 4      0 0 0

1 2 3 4

2 3 4 0

2 3 4 0 ,多乘几次就会全变为0,

2 3 4 0

2 3 4 0

而如果只有左上角不为0,每乘一次就是本身,无限次自然也不会变。

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int n,a[1100][1100],b[10][10];
 5 int main(){
 6     int T;  cin>>T;
 7     for(int t=1;t<=T;++t){
 8         scanf("%d",&n);
 9         for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)
10             scanf("%d",&a[i][j]);
11         int cnt=0;
12         for(int i=1;i<=3;++i)for(int j=1;j<=3;++j){
13             scanf("%d",&b[i][j]);
14             if(b[i][j])  ++cnt;
15         }
16         if(cnt==1 && b[1][1]){
17             for(int i=1;i<=n;++i){
18                 for(int j=1;j<=n;++j){
19                     printf("%d",a[i][j]);
20                     if(j!=n)  printf(" ");
21                 }
22                 printf("
");
23             }
24         }
25         else{
26             for(int i=1;i<=n;++i){
27                 for(int j=1;j<=n;++j){
28                     printf("0");
29                     if(j!=n)  printf(" ");
30                 }
31                 printf("
");
32             }
33         }
34     }
35     return 0;
36 }
View Code

参考博客:https://www.cnblogs.com/cdcq/p/13701117.html

原文地址:https://www.cnblogs.com/knightoflake/p/13708963.html