当前位置:网站首页>【AtCoder2387】+/- Rectangle

【AtCoder2387】+/- Rectangle

2022-06-11 07:37:00 CaptainHarryChen

The question

Construct a H × W H \times W H×W Matrix , Meet the following conditions

  • All elements are integers , And belongs to [ − 1 0 9 , 1 0 9 ] [-10^9,10^9] [109,109]
  • The sum of all elements is positive
  • For each h × w h \times w h×w The submatrix of , Its element sum is negative

Answer key

According to the example , The simplest idea is , Give satisfaction x   m o d   h = 0 x\ mod\ h=0 x mod h=0 And y   m o d   w = 0 y\ mod\ w=0 y mod w=0 The location of ( x , y ) (x,y) (x,y) Marked as − h w -hw hw, All the rest are marked as 1 1 1
But there are counter examples
about 1 × 4 1\times 4 1×4 Matrix , 1 × 3 1\times 3 1×3 The submatrix of , Will be marked as
1 1 − 3 1 \begin{matrix} 1&1&-3&1 \end{matrix} 1131 illegal , Can be marked as
2 2 − 5 2 \begin{matrix} 2&2&-5&2 \end{matrix} 2252

Consider improving this method , Mark the normal position as k k k, x   m o d   h = 0 x\ mod\ h=0 x mod h=0 And y   m o d   w = 0 y\ mod\ w=0 y mod w=0 The location of ( x , y ) (x,y) (x,y) Marked as − k ( h w − 1 ) − 1 -k(hw-1)-1 k(hw1)1, The sum of the submatrix is -1, If you can find k, Then there is a solution .

It can be proved that H   m o d   h = 0 H\ mod\ h=0 H mod h=0 And W   m o d   w = 0 W\ mod\ w=0 W mod w=0 when , unsolvable , At this time, you can H × W H\times W H×W The matrix of is exactly divided into integer numbers h × w h\times w h×w Submatrix , And each submatrix is negative , Then the entire primitive matrix cannot sum to a positive number .

If the above conditions are not met , Then the original matrix is cut into several submatrixes , There are still some leftovers , The value of each cell of leftover materials is k k k, Just make k k k Large enough , It must be able to offset a lot of the previous submatrix -1, therefore k The bigger the better .

When it is implemented, directly put k Card to the maximum .

Code

#include<cstdio> const int MAXN=505; int H,W,h,w; int arr[MAXN][MAXN]; int main() {
      scanf("%d%d%d%d",&H,&W,&h,&w); if(h==1&&w==1) {
      puts("No"); return 0; } long long sum=0; int k=999999999/(h*w-1); {
      sum=0; for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) {
      if(i%h==0&&j%w==0) arr[i][j]=-k*(h*w-1)-1; else arr[i][j]=k; sum+=arr[i][j]; } if(sum>0) {
      puts("Yes"); for(int i=1;i<=H;i++) {
      for(int j=1;j<=W;j++) printf("%d ",arr[i][j]); puts(""); } return 0; } } puts("No"); return 0; } 
原网站

版权声明
本文为[CaptainHarryChen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110723240174.html