1. 程式人生 > 實用技巧 >ccpc補題1011

ccpc補題1011

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

Given ann×nmatrixAand a3×3matrixK. These two matrices are very special : they are both non-negative matrices and the sum of all elements in matrixKis 1 (In order to avoid floating-point error, we will give matrixKin a special way in input).


Now we define a functionC(A,K), the value ofC(A,K)is also an×nmatrix and it is calculated below(we useCto abbreviateC(A,K)):

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

Now we defineCm(A,K)=C(Cm1(A,K),K)andC1(A,K)=C(A,K), Kanade wants to knowlimtCt(A,K)

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


Input There areTtest cases in this problem.

The first line has one integerT.

Then for every test case:

The first line has one integern.

Then there arenlines and each line hasnnon negative integers. The j-th integer of the i-th row denotesAi,j

Then there are3lines and each line has3non negative integers. The j-th integer of the i-th row denotesKi,j


ThenKcould be derived fromKby 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 matrixAin 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("\n");
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("\n");
32             }
33         }
34     }
35     return 0;
36 }
View Code

參考部落格:https://www.cnblogs.com/cdcq/p/13701117.html