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(n−x+1,3)i=1∑min(n−y+1,3)j=1Ax+i−1,y+j−1Ki,j
Now we defineCm(A,K)=C(Cm−1(A,K),K)andC1(A,K)=C(A,K), Kanade wants to knowlimt→∞Ct(A,K)
It's guaranteed that the answer exists and is an integer matrix.
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 denotesK′i,j
ThenKcould be derived fromK′by the following formula: Ki,j=K′i,j/(∑x=13∑y=13K′x,y)
1≤T≤100
3≤n≤50
0≤Ai,j≤1000
0≤K′i,j≤1000
∑3x=1∑3y=1K′x,y>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+i−1,y+j−1 ×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