Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w=map(int,input().split()) if W%w!=0: print("Yes") s=[0 for i in range(W+1)] s[W%w]=1+W//w for i in range(w,W+1): s[i]=s[i-w]-1 a=[s[i]-s[i-1] for i in range(1,W+1)] ans=[[str(a[i]) for i in range(W)] for j in range(H)] for line in ans: print(" ".join(line)) elif H%h!=0: print("Yes") s=[0 for i in range(H+1)] s[H%h]=1+H//h for i in range(h,H+1): s[i]=s[i-h]-1 a=[s[i]-s[i-1] for i in range(1,H+1)] ans=[[str(a[i]) for j in range(W)] for i in range(H)] for line in ans: print(" ".join(line)) else: print("No")
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = [ int(v) for v in input().split() ] if H % h == 0 and W % w == 0: print("No") else: print("Yes") grid = [ [ -h*w*1000+999 if i % h == h-1 and j % w == w-1 else 1000 for j in range(W) ] for i in range(H) ] for i in grid: print(*i)
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; int main(){ int n,m,x,y; cin >> n >> m >> x >> y; int a=(n/x)*(m/y); int b=n*m-a; if(n%x==0&&m%y==0){ cout << "No" << endl; } else{ cout << "Yes" << endl; int c=1e9; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(i%x==0&&j%y==0){ cout << -c; } else{ cout << (c-1)/(x*y-1); } cout << " "; } cout << endl; } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int H, W, h, w; cin >> H >> W >> h >> w; if(H % h == 0 && W % w == 0) { cout << "No"; return 0; } cout << "Yes\n"; int rem = H * W - (H / h * h) * (W / w * w); int x = (H / h) * (W / w); x = (x + rem - 1) / rem; x++; for(int i = 1; i <= H; i++, cout << endl) for(int j = 1; j <= W; j++) cout << (i % h == 0 && j % w == 0? (-h) * w * x - 1 + x: x) << ' '; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int H, W, h, w; cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { cout << "No\n"; return 0; } cout << "Yes\n"; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (i % h == h - 1 && j % w == w - 1) { cout << -3750 * (h * w - 1) - 1 << " "; } else { cout << 3750 << " "; } } cout << "\n"; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.PrintWriter; import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int H = sc.nextInt(); int W = sc.nextInt(); int h = sc.nextInt(); int w = sc.nextInt(); if(H%h==0) { if(W%w==0) { pw.println("No"); } else { pw.println("Yes"); int[] v = getArray(w); for(int i=0; i<H; i++) { for(int j=0; j<W; j++) { pw.print(v[j%w]); pw.print(j<W-1 ? " " : "\n"); } } } } else { pw.println("Yes"); int[] v = getArray(h); for(int i=0; i<H; i++) { for(int j=0; j<W; j++) { pw.print(v[i%h]); pw.print(j<W-1 ? " " : "\n"); } } } sc.close(); pw.close(); } static int[] getArray(int N) { int BASE = 1000; int[] ans = new int[N]; Arrays.fill(ans, BASE); ans[N-1] = -(BASE*(N-1)+1); return ans; } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <algorithm> #include <iostream> #include <string> #include <vector> #define POSITIVE ((int)(1000)) #define NEGATIVE ((int)((-POSITIVE) * (h * w - 1) - 1)) using namespace std; typedef long long i64; int H, W, h, w; int main() { cin >> H >> W >> h >> w ; if(H % h == 0 && W % w == 0) { cout << "No" << endl ; return 0; } cout << "Yes" << endl ; for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(i % h > 0 || j % w > 0) cout << POSITIVE ; else cout << NEGATIVE ; cout << " " ; } cout << endl ; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int _0v0_=1000; int ma[510][510],n,m,h,w; int main(){ scanf("%d%d%d%d",&n,&m,&h,&w); if(n%h==0&&m%w==0){printf("No");return 0;} printf("Yes\n"); for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)ma[i][j]=_0v0_; for(int i=1;i<=n/h;i++)for(int j=1;j<=m/w;j++)ma[i*h][j*w]=-(h*w-1)*_0v0_-1; for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)printf("%d ",ma[i][j]);printf("\n");} return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> using namespace std; int H,W,h,w; int A[500][500]; int main() { cin>>H>>W>>h>>w; if(h*w==1) { cout<<"No"<<endl; return 0; } int cnt=0; for(int i=h-1;i<H;i+=h)for(int j=w-1;j<W;j+=w)A[i][j]=-1,cnt++; long a=(1e9-1)/(h*w-1),b=1e9; if(a*(H*W-cnt)>b*cnt&&b*cnt>a*cnt*(h*w-1)) { cout<<"Yes"<<endl; for(int i=0;i<H;i++) { for(int j=0;j<W;j++)cout<<(A[i][j]<0?-b:a)<<(j==W-1?"\n":" "); } } else cout<<"No"<<endl; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; int a[510][510]; int main() { int h,w,H,W,i,j,ans=0; scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h==0&&W%w==0) { printf("No\n"); return 0; } for(i=h;i<=H;i+=h) for(j=w;j<=W;j+=w) a[i][j]=-w*h*1000+999; for(i=1;i<=H;i++) for(j=1;j<=W;j++) { if(!a[i][j]) a[i][j]=1000; ans+=a[i][j]; } if(ans<0) { printf("No\n"); return 0; } printf("Yes\n"); for(i=1;i<=H;i++) { for(j=1;j<=W;j++) printf("%d ",a[i][j]); printf("\n"); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) throws FileNotFoundException { new Main().run(); } void run() { Scanner sc = new Scanner(System.in); int H = sc.nextInt(); int W = sc.nextInt(); int h = sc.nextInt(); int w = sc.nextInt(); if (H % h == 0 && W % w == 0) { System.out.println("No"); return; } else { System.out.println("Yes"); } int[][] a = new int[H][W]; for (int i = 0; i < H; i += h) { for (int j = 0; j < W; j += w) { a[i][j] = 1_000_000_000 - 1; if (i + h - 1 < H && j + w - 1 < W) { a[i + h - 1][j + w - 1] = -1_000_000_000; } } } for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { System.out.print(a[i][j] + (j < W - 1 ? " " : "\n")); } } } void tr(Object... objects) { System.out.println(Arrays.deepToString(objects)); } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int main() { int n,m,x,y;scanf("%d%d%d%d",&n,&m,&x,&y); if (m%y) { cout<<"Yes\n"; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { if (j%y==1) cout<<1000000*(y-1)-1<<' ';else cout<<-1000000<<' '; if (j==m) cout<<endl; } return 0; } if (n%x) { cout<<"Yes\n"; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { if (i%x==1) cout<<1000000*(x-1)-1<<' ';else cout<<-1000000<<' '; if (j==m) cout<<endl; } return 0; } cout<<"No\n"; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import sys H,W,h,w=map(int,input().split()) if H%h==0 and W%w==0: print("No") elif H%h!=0: print("Yes") p=100000*(h-1)-1 q=-100000 L=int(H/h) for l in range(L): for i in range(W): print(p, end = ' ') print("") for i in range(h-1): for i in range(W): print(q, end = ' ') print("") for i in range(W): print(p, end = ' ') print("") for i in range(H-L*h-1): for i in range(W): print(q, end = ' ') else: print("Yes") p=100000*(w-1)-1 q=-100000 L=int(W/w) s=([p]+[q]*(w-1))*L+[p]+[q]*(W-L*w-1) for j in range(H): for i in range(W): print(s[i], end= " ") print("")
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int a[510][510],H,W,h,w; int main() { scanf("%d%d%d%d",&H,&W,&h,&w); if (!(H%h)&&!(W%w)) return puts("No"),0; puts("Yes"); a[0][0]=(H/h)*(W/w)+1; a[h-1][w-1]=-a[0][0]-1; for (int i=0;i<H;i++,puts("")) for (int j=0;j<W;j++) printf("%d ",a[i%h][j%w]); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = map(int, input().split()) if H % h == 0 and W % w == 0: print("No") elif h == 1: print("Yes") coef = 999999999 // (w-1) plus, minus = coef, -coef * (w-1) - 1 for _ in range(H): print(*[minus if j%w == 0 else plus for j in range(1, W+1)]) elif w == 1: print("Yes") coef = 999999999 // (h-1) plus, minus = coef, -coef * (h-1) - 1 for i in range(1, H+1): print(*[minus if i%h == 0 else plus for _ in range(W)]) else: print("Yes") coef = min(999999999 // ((h-1) * (w-1)), 1000000000 // (h+w-1)) plus, minus = coef * (h+w-1), -coef * (h-1) * (w-1) - 1 for i in range(1, H+1): if i%h != 0: print(*[minus if j%w == 0 else plus for j in range(1, W+1)]) else: print(*[minus for _ in range(W)])
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.*; import java.util.*; public class Main implements Runnable { @SuppressWarnings("unchecked") public void run() { BetterScanner scanner = new BetterScanner(System.in); int hSize = scanner.nextInt(); int wSize = scanner.nextInt(); int h = scanner.nextInt(); int w = scanner.nextInt(); int num = (hSize / h) * (wSize / w); if (hSize * wSize - num * h * w <= 0) { System.out.println("No"); } else { System.out.println("Yes"); StringBuilder output = new StringBuilder(); int k = num / (hSize * wSize - num * h * w) + 1; for (int y = 0 ; y < hSize ; y ++) { for (int x = 0 ; x < wSize ; x ++) { if (x > 0) { output.append(" "); } if ((x + 1) % w == 0 && (y + 1) % h == 0) { output.append(- (h * w - 1) * k - 1); } else { output.append(k); } } output.append("\n"); } System.out.println(output); } } public static void main(String[] args) { Main main = new Main(); main.run(); } public static class BetterScanner { private InputStream stream; private byte[] buffer = new byte[1024]; private int pointer = 0; private int bufferLength = 0; public BetterScanner(InputStream stream) { this.stream = stream; } private boolean updateBuffer() { if (pointer >= bufferLength) { pointer = 0; try { bufferLength = stream.read(buffer); } catch (IOException exception) { exception.printStackTrace(); } return bufferLength > 0; } else { return true; } } private int read() { if (updateBuffer()) { return buffer[pointer ++]; } else { return -1; } } public boolean hasNext() { skipUnprintable(); return updateBuffer(); } private void skipUnprintable() { while (updateBuffer() && !isPrintableChar(buffer[pointer])) { pointer ++; } } public String next() { if (hasNext()) { StringBuilder builder = new StringBuilder(); int codePoint = read(); while (isPrintableChar(codePoint)) { builder.appendCodePoint(codePoint); codePoint = read(); } return builder.toString(); } else { throw new NoSuchElementException(); } } public long nextLong() { if (hasNext()) { long number = 0; boolean minus = false; int codePoint = read(); if (codePoint == '-') { minus = true; codePoint = read(); } if (codePoint >= '0' && codePoint <= '9') { while (true) { if (codePoint >= '0' && codePoint <= '9') { number *= 10; number += codePoint - '0'; } else if (codePoint < 0 || !isPrintableChar(codePoint)) { return (minus) ? -number : number; } else { throw new NumberFormatException(); } codePoint = read(); } } else { throw new NumberFormatException(); } } else { throw new NoSuchElementException(); } } public int nextInt() { long number = nextLong(); if (number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE) { return (int)number; } else { throw new NumberFormatException(); } } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0 ; i < n ; i ++) { array[i] = nextLong(); } return array; } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0 ; i < n ; i ++) { array[i] = nextInt(); } return array; } private boolean isPrintableChar(int codePoint) { return codePoint >= 33 && codePoint <= 126; } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 500 #define MAXV 1000000000 using namespace std; int M[MAXN+5][MAXN+5]; int main() { int H,W,h,w; scanf("%d %d %d %d",&H,&W,&h,&w); if(H%h==0&&W%w==0) { printf("No\n"); return 0; } int k=(MAXV-1)/(w*h-1); for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) if(i%h==0&&j%w==0) M[i][j]=-k*(h*w-1)-1; else M[i][j]=k; printf("Yes\n"); for(int i=1;i<=H;i++) { for(int j=1;j<=W;j++) if(j==1) printf("%d",M[i][j]); else printf(" %d",M[i][j]); printf("\n"); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.*; import java.util.*; import java.math.*; // import java.awt.Point; public class Main { InputStream is; PrintWriter out; String INPUT = ""; long MOD = 1_000_000_007; int inf = Integer.MAX_VALUE/2; void solve(){ int h = ni(); int w = ni(); int a = ni(); int b = ni(); if(h%a==0 && w%b==0){ out.println("No"); return; } int grid = a*b; long min = -1000000000; long res = -min/(a*b-1)-1; out.println("Yes"); for(int i = 1; i <= h; i++){ for(int j = 1; j <= w; j++){ if(i%a==0 && j%b==0) out.print(min+" "); else out.print(res+" "); } out.println(); } } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new Main().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b) && b != ' ')){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.UncheckedIOException; import java.io.Closeable; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) throws Exception { Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27); thread.start(); thread.join(); } static class TaskAdapter implements Runnable { @Override public void run() { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); CRectangle solver = new CRectangle(); solver.solve(1, in, out); out.close(); } } static class CRectangle { public void solve(int testNumber, FastInput in, FastOutput out) { int H = in.readInt(); int W = in.readInt(); int h = in.readInt(); int w = in.readInt(); if (H % h == 0 && W % w == 0) { out.println("No"); return; } out.println("Yes"); int[][] ans = null; if (H % h != 0) { ans = solve(W, H, w, h); ans = transpose(ans); } else { ans = solve(H, W, h, w); } for (int[] row : ans) { for (int x : row) { out.append(x).append(' '); } out.println(); } } public int[][] transpose(int[][] mat) { int n = mat.length; int m = mat[0].length; int[][] ans = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ans[j][i] = mat[i][j]; } } return ans; } public int[][] solve(int H, int W, int h, int w) { int r = W % w; int[] row = new int[W]; for (int i = 0; i < W; i++) { if (i % w == 0) { row[i] = (int) 1e9 - 1; } else if (i % w == r) { row[i] = (int) -1e9; } } int[][] ans = new int[H][]; for (int i = 0; i < H; i++) { ans[i] = row.clone(); } return ans; } } static class FastOutput implements AutoCloseable, Closeable, Appendable { private StringBuilder cache = new StringBuilder(10 << 20); private final Writer os; public FastOutput append(CharSequence csq) { cache.append(csq); return this; } public FastOutput append(CharSequence csq, int start, int end) { cache.append(csq, start, end); return this; } public FastOutput(Writer os) { this.os = os; } public FastOutput(OutputStream os) { this(new OutputStreamWriter(os)); } public FastOutput append(char c) { cache.append(c); return this; } public FastOutput append(int c) { cache.append(c); return this; } public FastOutput append(String c) { cache.append(c); return this; } public FastOutput println(String c) { return append(c).println(); } public FastOutput println() { cache.append(System.lineSeparator()); return this; } public FastOutput flush() { try { os.append(cache); os.flush(); cache.setLength(0); } catch (IOException e) { throw new UncheckedIOException(e); } return this; } public void close() { flush(); try { os.close(); } catch (IOException e) { throw new UncheckedIOException(e); } } public String toString() { return cache.toString(); } } static class FastInput { private final InputStream is; private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastInput(InputStream is) { this.is = is; } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { bufLen = -1; } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int H, W, P, Q; int main() { cin >> H >> W >> P >> Q; if(!(H%P) && !(W%Q)) { puts("No"); return 0; } puts("Yes"); if(H%P) { for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) printf("%d ", i%P ? H+1 : -(P-1)*(H+1)-1); puts(""); } } else if(W%Q) { for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) printf("%d ", j%Q ? W+1 : -(Q-1)*(W+1)-1); puts(""); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import sys input = sys.stdin.readline H,W,h,w = map(int,input().split()) if H%h == 0 and W%w == 0: print('No') exit() x,y = H//h, W//w s = x*y + 1 t = (h*w-1) * s + 1 import numpy as np grid = np.full((H+1, W+1), s, dtype=np.int32) grid[::h,::w] = -t grid = grid[1:,1:] print('Yes') for row in grid: print(' '.join(row.astype(str)))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Closeable; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; public class Main { static ContestScanner in; static Writer out; public static void main(String[] args) { Main main = new Main(); try { in = new ContestScanner(); out = new Writer(); main.solve(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } void solve() throws IOException { int H = in.nextInt(); int W = in.nextInt(); int h = in.nextInt(); int w = in.nextInt(); int[][] map = new int[H][W]; long cnt = 0; final int base = 1000; final int min = - (h * w) * base - 1; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { map[i][j] = base; if((i + 1) % h == 0 && (j + 1) % w == 0) map[i][j] += min; cnt += map[i][j]; } } if(cnt <= 0) { System.out.println("No"); } else { out.println("Yes"); for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { if(j > 0) out.print(" "); out.print(map[i][j]); }out.println(); } } } } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException{super(System.out);} } class ContestScanner implements Closeable{ private BufferedReader in;private int c=-2; public ContestScanner()throws IOException {in=new BufferedReader(new InputStreamReader(System.in));} public ContestScanner(String filename)throws IOException {in=new BufferedReader(new InputStreamReader(new FileInputStream(filename)));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} public void close() throws IOException {in.close();} }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; const int v=1000;int n,m,a,b,s,q[v][v]; int main() { cin>>n>>m>>a>>b; for(int i=1;i<=n;i++) for(int j=1;j<=m;s+=q[i][j]=(i%a||j%b)?v:-(a*b-1)*v-1,j++); if(s<=0)return 0&puts("No");puts("Yes"); for(int i=1;i<=n;i++){for(int j=1;j<=m;j++)cout<<q[i][j]<<" ";puts(""); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { long long H,W,h,w;cin>>H>>W>>h>>w; vector<vector<long long>>V(H,vector<long long>(W,1000)); if(H%h==0&&W%w==0)cout<<"No"<<endl; else{ cout<<"Yes"<<endl; for(int X=h-1;X<H;X+=h){ for(int Y=w-1;Y<W;Y+=w){ V[X][Y]=(h*w-h*w*2+1)*1000-1; } } for(int X=0;X<H;X++){ for(int Y=0;Y<W;Y++){ if(Y==W-1)cout<<V[X][Y]<<endl; else cout<<V[X][Y]<<" "; } } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int N=505; const int d=4000; int n,m,x,y,a[N][N]; int main() { scanf("%d%d%d%d",&n,&m,&x,&y); if (n%x==0&&m%y==0) {puts("No");return 0;} puts("Yes"); for (int i=x;i<=n;i+=x) for (int j=y;j<=m;j+=y) a[i][j]=-x*y*d+d-1; for (int i=1;i<=n;i++) { for (int j=1;j<=m;j++) printf("%d ",!a[i][j]?d:a[i][j]); puts(""); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w = [int(x) for x in input().split(' ')] if W % w != 0: r = W % w n = int(W/w) n = n+1 print('Yes') for i in range(H): for k in range(W): if (k+1) % w == 0: print(-n*(w-1) - 1, end = '') else: print(n, end = '') if k == W-1: print() else: print(end = ' ') elif H % h != 0: r = H % h n = int(H/h) n = n + 1 print('Yes') store = 0 for i in range(H): if (i+1) % h == 0: store = -n*(h-1) - 1 else: store = n for k in range(W): if k == W-1: print(store) else: print(store, end=' ') else: print('No')
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> int main() { int n, m, h, w, a[510][510]; std::cin >> n >> m >> h >> w; if (n % h == 0 && m % w == 0) { std::cout << "No"; } else { std::cout << "Yes\n"; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (i % h == 0 && j % w == 0) std::cout << 100000000 - 1; else if (i % h == h - 1 && j % w == w - 1) std::cout << -100000000; else std::cout << 0; std::cout << " "; } std::cout << "\n"; } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int H, W, h, w; int main() { cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) cout << "No" << endl; else { cout << "Yes" << endl; for (int i = 0; i < H; ++ i) { for (int j = 0; j < W; ++ j) { cout << (i % h || j % w? -1000: 1000 * (h * w - 1) - 1) << " "; } cout << endl; } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL H,W,h,w; LL ans[510][510]; int main() { scanf("%lld%lld%lld%lld",&H,&W,&h,&w); LL sum = 0; for (int i=1;i<=H;i++) for (int j=1;j<=W;j++) { if (i%h==0 && j%w==0) ans[i][j] = -(h*w-1)*2LL*max(H,W)-1; else ans[i][j] = 2LL*max(H,W); sum += ans[i][j]; } if (sum > 0) { puts("Yes"); for (int i=1;i<=H;i++) for (int j=1;j<=W;j++)printf("%lld%c",ans[i][j]," \n"[j==W]); } else puts("No"); }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; #define ll long long #define all(V) (V).begin(),(V).end() int main(){ ll H, W, h, w; cin >> H >> W >> h >> w; if (H%h == 0 && W%w == 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; vector<vector<int>> ANS(H,vector<int>(W, 501)); for (int i = 0;i < H;i++) { for (int j = 0;j < W;j++) { if (i%h == h - 1 && j%w == w - 1) ANS[i][j] = -501 * (h*w - 1) - 1; cout << ANS[i][j] << " "; } cout << endl; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public int[] solve(int len, int k) { // sum of things is positive, every k consecutive is negative // null if doesn't exist if (len % k == 0) { return null; } int[] ret = new int[len]; int plus = len % k; int minus = k - plus; int a = 1, b = -1; for (int i = 0; i < len; i++) { if (i % k == 0) { ret[i] = 999999999; } else if (i % k == (k - 1)) { ret[i] = -1000000000; } } return ret; } public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), m = in.nextInt(), r = in.nextInt(), c = in.nextInt(); int[] x = solve(n, r), y = solve(m, c); if (x == null && y == null) { out.println("No"); } else { out.println("Yes"); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (j != 0) out.print(" "); if (x != null) out.print(x[i]); else out.print(y[j]); } out.println(); } } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (this.numChars == -1) { throw new InputMismatchException(); } else { if (this.curChar >= this.numChars) { this.curChar = 0; try { this.numChars = this.stream.read(this.buf); } catch (IOException var2) { throw new InputMismatchException(); } if (this.numChars <= 0) { return -1; } } return this.buf[this.curChar++]; } } public int nextInt() { int c; for (c = this.read(); isSpaceChar(c); c = this.read()) { ; } byte sgn = 1; if (c == 45) { sgn = -1; c = this.read(); } int res = 0; while (c >= 48 && c <= 57) { res *= 10; res += c - 48; c = this.read(); if (isSpaceChar(c)) { return res * sgn; } } throw new InputMismatchException(); } public static boolean isSpaceChar(int c) { return c == 32 || c == 10 || c == 13 || c == 9 || c == -1; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println() { writer.println(); } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void print(int i) { writer.print(i); } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H,W,h,w = map(int,input().split()) if(H%h==0)&(W%w==0): print('No') exit() print('Yes') if(W%w!=0): base = [1000] * (w-1) + [-1000 * (w-1) -1] col = [] for i in range(1 + W//w): col = col + base col = col[:W] for i in range(H): print(' '.join(map(str,col))) else: base = [1000] * (h-1) + [-1000 * (h-1) -1] row = [] for i in range(1 + H//h): row = row + base row = row[:H] for i in row: col = [i] * W print(' '.join(map(str,col)))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; LL n,m,h,w; LL val[505][505] = {0}; int main(){ cin >> n >> m >> h >> w; if(!(n % h || m % w)){ cout << "No" << endl; return 0; } cout << "Yes" << endl; for(LL i = h;i <= n;i += h) for(LL j = w;j <= m;j += w) val[i][j] = - (h * w * 1000 - 999); for(LL i = 1;i <= n;i ++) for(LL j = 1;j <= m;j ++) cout << (val[i][j] ? val[i][j] : 1000) << (j == m ? '\n' : ' '); return 0; }//
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int ans[501][501]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int H,W,h,w; cin>>H>>W>>h>>w; if(H%h==0&&W%w==0) return cout<<"No\n",0; cout<<"Yes\n"; for(int i=1;i<=H;i+=h) for(int j=1;j<=W;j+=w) ans[i][j]=999999999; for(int i=h;i<=H;i+=h) for(int j=w;j<=W;j+=w) ans[i][j]=-1e9; for(int i=1;i<=H;i++,cout<<'\n') for(int j=1;j<=W;j++) cout<<ans[i][j]<<' '; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
min2 = lambda x,y: x if x < y else y def solve(H,W,h,w): if h == 1 and w == 1: return None res = [[0]*W for _ in range(H)] for i in range(0,W,w): for j in range(0,H,h): res[j][i] = 10**9-1 if i+w-1 < W and j+h-1 < H: res[j+h-1][i+w-1] = -10**9 s = sum(sum(l) for l in res) if s <= 0: return None else: return res if __name__ == '__main__': H,W,h,w = map(int,input().split()) res = solve(H,W,h,w) if res: print('Yes') print('\n'.join(' '.join(map(str,l)) for l in res)) else: print('No')
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int R,C,H,W,D; int A[505][505]; long long Sum; int main(){ int i,j; scanf("%d%d%d%d",&R,&C,&H,&W); D=(H*W>1?999999999/(H*W-1):1000000000); for(i=1;i<=R;i++) for(j=1;j<=C;j++){ A[i][j]=(i%H||j%W?D:-(H*W-1)*D-1); Sum+=A[i][j]; } if(Sum<=0) puts("No"); else{ puts("Yes"); for(i=1;i<=R;i++){ for(j=1;j<=C;j++) printf("%d ",A[i][j]); puts(""); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { const int mods = 1000; int n = 0, m = 0, h = 0, w = 0; cin >> n >> m >> h >> w; if((n * m - (n / h * h) * (m / w * w)) * mods <= (n / h) * (m / w)) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for(int i = 1; i <= n; i++, putchar('\n')) for(int j = 1; j <= m; j++, putchar(' ')) printf("%d", (i % h == 0 && j % w == 0)? mods - 1 - mods * (h * w): mods); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int H,W,h,w,a[1111][1111],sum; int main(){ cin>>H>>W>>h>>w; for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ a[i][j]=1000; } } for(int i=h;i<=H;i+=h){ for(int j=w;j<=W;j+=w){ a[i][j]=-((h*w-1)*1000+1); } } for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ sum+=a[i][j]; } } if(sum<=0){ puts("No"); return 0; } puts("Yes"); for(int i=1;i<=H;i++){ for(int j=1;j<=W;j++){ if(j!=1)cout<<" "; cout<<a[i][j]; } cout<<endl; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cstdio> int N, M, n, m, a[501] = { -1000000000, 999999999 }; int main() { scanf("%d%d%d%d", &N, &M, &n, &m); if (N % n) { puts("Yes"); for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) printf("%d%c", a[i % n], " \n"[j == M]); } else if (M % m) { puts("Yes"); for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) printf("%d%c", a[j % m], " \n"[j == M]); } else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int H, W, h, w; cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { cout << "No\n"; return 0; } cout << "Yes\n"; for (int r = 0; r < H; r++) { for (int c = 0; c < W; c++) { if (c > 0) cout << " "; if (r % h == 0 && c % w == 0) cout << (int) 1e9 - 1; else if (r % h == h - 1 && c % w == w - 1) cout << (int) -1e9; else cout << 0; } cout << '\n'; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NoSuchElementException; import java.util.Objects; import java.util.PriorityQueue; import java.util.Random; import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet; import java.util.function.BiFunction; import java.util.function.IntBinaryOperator; import java.util.function.LongBinaryOperator; import java.util.stream.*; // import static java.util.Arrays.compare; import static java.util.Arrays.fill; import static java.util.Arrays.sort; import static java.util.Collections.sort; import static java.util.Comparator.reverseOrder; // import static java.util.List.of; import static java.util.Objects.isNull; import static java.util.Objects.nonNull; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.math.BigInteger; import static java.math.BigInteger.ZERO; import static java.math.BigInteger.ONE; // import static java.math.BigInteger.TWO; import static java.math.BigInteger.TEN; import static java.lang.Math.PI; import static java.lang.Math.E; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.abs; import static java.lang.Math.sin; import static java.lang.Math.cos; import static java.lang.Math.tan; import static java.lang.Math.asin; import static java.lang.Math.acos; import static java.lang.Math.atan; import static java.lang.Math.atan2; import static java.lang.Math.hypot; import static java.lang.Math.sqrt; import static java.lang.Math.log; import static java.lang.Math.exp; import static java.lang.String.format; import static java.lang.System.exit; import static java.lang.System.currentTimeMillis; class Main { public static long MOD = 1_000_000_007; public static void main(String[] args) { FastScanner fsc=new FastScanner(); int h=fsc.nextInt(), w=fsc.nextInt(); int y=fsc.nextInt(), x=fsc.nextInt(); fsc.close(); int n=h/y, m=w/x; int rh=h%y, rw=w%x; int max=400; if(-n*m + max*(rh*w + rw*h - rh*rw) <= 0){ System.out.println("No"); } else{ StringBuilder sb=new StringBuilder("Yes\n"); for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if((i+1)%y==0 && (j+1)%x==0){ sb.append(-(y*x-1)*max-1).append(' '); } else{ sb.append(max).append(' '); } } sb.append('\n'); } System.out.print(sb); } } static class IntUtil { private static final int MAX=Integer.MAX_VALUE; private static final int MIN=Integer.MIN_VALUE; public static int min(int... a) { int ret = MAX; for (int c : a) { ret = Math.min(ret, c); } return ret; } public static int max(int... a) { int ret = MIN; for (int c : a) { ret = Math.max(ret, c); } return ret; } /** * Caluculate the ceil of a/b. Returns the smallest integer greater than or * equal to a/b while 'a/b' rounds fractional parts to ZERO. * * @param a * @param b * @return the smallest integer greater than or equal to a/b */ public static int cld(int a, int b) { if ((a > 0 && b > 0) || (a < 0 && b < 0)) { if (b > 0) { return (a + b - 1) / b; } else { return (a + b + 1) / b; } } else { return a / b; } } /** * Caluculate the floor of a/b. Returns the largest integer less than or equal * to a/b while 'a/b' rounds fractional parts to ZERO. * * @param a * @param b * @return the largest integer less than or equal to a/b */ public static int fld(int a, int b) { if ((a > 0 && b > 0) || (a < 0 && b < 0)) { return a / b; } else { if (b > 0) { return (a - b + 1) / b; } else { return (a - b - 1) / b; } } } public static int fold(IntBinaryOperator func, int a, int... b) { int ret = a; for (int c : b) { ret = func.applyAsInt(ret, c); } return ret; } } static class LongUtil { private static final long MAX=Long.MAX_VALUE; private static final long MIN=Long.MIN_VALUE; public static long min(long... a) { long ret = MAX; for (long c : a) { ret = Math.min(ret, c); } return ret; } public static long max(long... a) { long ret = MIN; for (long c : a) { ret = Math.max(ret, c); } return ret; } /** * Caluculate the ceil of a/b. Returns the smallest integer greater than or * equal to a/b while 'a/b' rounds fractional parts to ZERO. * * @param a * @param b * @return the smallest integer greater than or equal to a/b */ public static long cld(long a, long b) { if ((a > 0 && b > 0) || (a < 0 && b < 0)) { if (b > 0) { return (a + b - 1) / b; } else { return (a + b + 1) / b; } } else { return a / b; } } /** * Caluculate the floor of a/b. Returns the largest integer less than or equal * to a/b while 'a/b' rounds fractional parts to ZERO. * * @param a * @param b * @return the largest integer less than or equal to a/b */ public static long fld(long a, long b) { if ((a > 0 && b > 0) || (a < 0 && b < 0)) { return a / b; } else { if (b > 0) { return (a - b + 1) / b; } else { return (a - b - 1) / b; } } } public static long fold(LongBinaryOperator func, long a, long... b) { long ret = a; for (long c : b) { ret = func.applyAsLong(ret, c); } return ret; } } static class MathUtil{ /** * Enumarate primes less than n. * * @param n * @return {@code ArrayList} that holds primes. */ public static ArrayList<Integer> eratosthenes(int n) { int[] div = eratosthenesDivisors(n); ArrayList<Integer> result = new ArrayList<>(); for (int i = 2; i <= n; i++) { if (div[i] == i) { result.add(i); } } return result; } /** * execute eratosthenes's prime-enumerate algorithm. a[i] holds the greatest * divisor of i. if a[i]=i, i is a prime number. This arrary enables you to * prime-factorize in O(log n) time. (<O(sqrt n)). * * @param n * @return divisor array. */ public static int[] eratosthenesDivisors(int n) { int[] divisors = new int[n + 1]; if (n <= 0) { return null; } for (int i = 1; i <= n; i++) { if ((i & 1) != 0) { divisors[i] = i; } else { divisors[i] = 2; } } for (long i = 3; i <= n; i += 2) { if (divisors[(int) i] == (int) i) { for (long j = i * i; j <= n; j += (i << 1)) { divisors[(int) j] = (int) i; } } } return divisors; } /** * Caluculate prime-factorization. * * @param n * @return {@code HashMap} of {prime: index} */ public static HashMap<Long, Integer> primeFactorization(long n) { HashMap<Long, Integer> primes = new HashMap<Long, Integer>(); for (long p = 2; p * p <= n; p++) { int q = 0; while (n % p == 0) { n /= p; q++; } if (q > 0) { primes.put(p, q); } } if (n > 1) { primes.put(n, 1); } return primes; } private static HashMap<Long, Integer> lcm(HashMap<Long, Integer> amap, HashMap<Long, Integer> bmap) { if (amap.size() < bmap.size()) { return lcm(bmap, amap); } HashMap<Long, Integer> lcm = amap; for (Map.Entry<Long, Integer> e : bmap.entrySet()) { long prime = e.getKey(); if (lcm.containsKey(prime)) { lcm.put(prime, Math.max(lcm.get(prime), e.getValue())); } else { lcm.put(prime, e.getValue()); } } return lcm; } private static HashMap<Long, Integer> lcm(HashMap<Long, Integer> amap, long b) { HashMap<Long, Integer> bmap = primeFactorization(b); return lcm(amap, bmap); } public static HashMap<Long, Integer> lcm(long... a) { HashMap<Long, Integer> amap = new HashMap<>(); for (long c : a) { amap = lcm(amap, c); } return amap; } public static long unsafeLCM(long a, long b) { return a * b / gcd(a, b); } /** * Caluculate the GCD of (a, b)/ * * @param a first value * @param b second value * @return GCD(a, b) */ public static long gcd(long a, long b) { if (a < b) { return gcd(b, a); } if(b == 0) { return a; } else if(a == 0) { return b; } long r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } public static long gcd(long... a){ long gcd = 0; for(long c:a){ gcd = gcd(gcd, c); } return gcd; } /** * Return one of the solutions to {@code ax+by=gcd(a, b)}. * * @return {@code x}, {@code y}, {@code gcd(a, b)}. * @param a a of ax+by=gcd(a, b). * @param b b of ax+by=gcd(a, b). class ReferenceLong is a reference type of long. */ public static long[] extGCD(long a, long b) { ReferenceLong x = new ReferenceLong(); ReferenceLong y = new ReferenceLong(); long[] ret = new long[3]; ret[2] = extGCD(a, b, x, y); ret[0] = x.v; ret[1] = y.v; return ret; } private static long extGCD(long a, long b, ReferenceLong x, ReferenceLong y) { if (b == 0) { x.v = 1; y.v = 0; return a; } long d = extGCD(b, a % b, y, x); y.v -= a / b * x.v; return d; } private static class ReferenceLong { long v = 0; } } static class ArrayUtil{ public static int[] input(FastScanner fsc, int n){ int[] a=new int[n]; for(int i=0;i<n;i++){ a[i]=fsc.nextInt(); } return a; } public static int[][] input(FastScanner fsc, int n, int m){ int[][] a=new int[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=fsc.nextInt(); } } return a; } /** * s[i] is the sum of the range [0, i) in a. * @param a * @return cumulative sum array of a. */ public static int[] cumsum(int[] a){ int n=a.length; int[] s=new int[n+1]; for(int i=1;i<=n;i++){ s[i]=s[i-1]+a[i-1]; } return s; } public static int[][] cumsum(int[][] a){ int n=a.length; int m=a[0].length; int[][] s=new int[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i-1][j-1]; } } return s; } public static void reverse(int[] a, int begin, int end){ for(int i=begin;i<begin+(end-begin)/2;i++){ swap(a, i, begin+end-i-1); } } public static void reverse(int[] a){ reverse(a, 0, a.length); } public static void revSort(int[] a){ Arrays.sort(a); reverse(a); } public static int[] nextPermutation(int[] a){ int[] ret=a.clone(); int n=ret.length; for(int i=n-1;i>=1;i--){ if(ret[i]>ret[i-1]){ int fail=n, pass=i; while(fail-pass>1){ int mid=pass+(fail-pass)/2; if(ret[mid]>ret[i-1]) pass=mid; else fail=mid; } swap(ret, pass, i-1); reverse(ret, i, n); return ret; } } return null; } public static int[] predPermutation(int[] a){ int[] ret=a.clone(); int n=ret.length; for(int i=n-1;i>=1;i--){ if(ret[i]<ret[i-1]){ int fail=n, pass=i; while(fail-pass>1){ int mid=pass+(fail-pass)/2; if(ret[mid]<ret[i-1]) pass=mid; else fail=mid; } swap(ret, pass, i-1); reverse(ret, i, n); return ret; } } return null; } public static void swap(int[] a, int u, int v){ int tmp=a[u]; a[u]=a[v]; a[v]=tmp; } public static int compare(int[] a, int[] b){ for(int i=0;i<a.length;i++){ if(i>=b.length){ return -1; } else if(a[i]>b[i]){ return 1; } else if(a[i]<b[i]){ return -1; } } if(a.length<b.length){ return 1; } else{ return 0; } } public static boolean equals(int[] a, int[] b){ return compare(a, b)==0; } /** * IntArray * _________________________ * LongArray */ public static long[] linput(FastScanner fsc, int n){ long[] a=new long[n]; for(int i=0;i<n;i++){ a[i]=fsc.nextLong(); } return a; } public static long[][] linput(FastScanner fsc, int n, int m){ long[][] a=new long[n][m]; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ a[i][j]=fsc.nextLong(); } } return a; } /** * s[i] is the sum of the range [0, i) in a. * @param a * @return cumulative sum array of a. */ public static long[] cumsum(long[] a){ int n=a.length; long[] s=new long[n+1]; for(int i=1;i<=n;i++){ s[i]=s[i-1]+a[i-1]; } return s; } public static long[][] cumsum(long[][] a){ int n=a.length; int m=a[0].length; long[][] s=new long[n+1][m+1]; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+a[i-1][j-1]; } } return s; } public static void reverse(long[] a, int begin, int end){ for(int i=begin;i<begin+(end-begin)/2;i++){ long tmp=a[i]; a[i]=a[begin+end-i-1]; a[begin+end-i-1]=tmp; } } public static void reverse(long[] a){ reverse(a, 0, a.length); } public static void revSort(long[] a){ Arrays.sort(a); reverse(a); } public static void compress(long[] a){ int n=a.length; long[] sorted = a.clone(); Arrays.sort(sorted); for(int i=0;i<n;i++){ int l=0, r=n; while(r-l>1){ int m=l+(r-l)/2; if(sorted[m]<=a[i]) l=m; else r=m; } a[i]=l; } } public static long[] nextPermutation(long[] a){ long[] ret=a.clone(); int n=ret.length; for(int i=n-1;i>=1;i--){ if(ret[i]>ret[i-1]){ int fail=n, pass=i; while(fail-pass>1){ int mid=pass+(fail-pass)/2; if(ret[mid]>ret[i-1]) pass=mid; else fail=mid; } swap(ret, pass, i-1); reverse(ret, i, n); return ret; } } return null; } public static long[] predPermutation(long[] a){ long[] ret=a.clone(); int n=ret.length; for(int i=n-1;i>=1;i--){ if(ret[i]<ret[i-1]){ int fail=n, pass=i; while(fail-pass>1){ int mid=pass+(fail-pass)/2; if(ret[mid]<ret[i-1]) pass=mid; else fail=mid; } swap(ret, pass, i-1); reverse(ret, i, n); return ret; } } return null; } public static void swap(long[] a, int u, int v){ long tmp=a[u]; a[u]=a[v]; a[v]=tmp; } public static int compare(long[] a, long[] b){ for(int i=0;i<a.length;i++){ if(i>=b.length){ return -1; } else if(a[i]>b[i]){ return 1; } else if(a[i]<b[i]){ return -1; } } if(a.length<b.length){ return 1; } else{ return 0; } } public static boolean equals(long[] a, long[] b){ return compare(a, b)==0; } } static class ModInt{ public static long mul(long... a) { long ret = 1; for (long c : a) { ret *= c; ret %= MOD; } return ret; } public static long div(long a, long... b) { long ret = a; for (long c : b) { ret *= inv(c); ret %= MOD; } return ret; } /** * Caluculate the value b s.t. a*b mod MOD = 1. * * @param a * @return inverse of a */ public static long inv(long a) { long b = MOD; long u = 1, v = 0; while (b >= 1) { long t = a / b; a -= t * b; long tmp1 = a; a = b; b = tmp1; u -= t * v; long tmp2 = u; u = v; v = tmp2; } u %= MOD; return u >= 0 ? u : u + MOD; } /** * Caluculate the combination nCr. * * @param n left * @param r right * @return nCr */ public static long comb(long n, long r) { if (n < r) { return 0; } r = Math.min(r, n - r); long res = 1; for (long d = 1; d <= r; d++) { res = div(mul(res, n--), d); } return res; } public static long[] fact(int n){ long[] fact=new long[n+1]; for(long i=0;i<=n;i++){ if(i<=1){ fact[(int)i]=1; } else{ fact[(int)i]=mul(fact[(int)i-1], i); } } return fact; } } static class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public void close(){ try{ in.close(); } catch(Exception e){ e.printStackTrace(); } } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cstdio> #define rep(i,j,k) for (i=j;i<=k;i++) using namespace std; const int N=505; int H,W,h,w,i,j,bsc; int main() { // freopen("matrix.in","r",stdin); // freopen("matrix.out","w",stdout); scanf("%d%d%d%d",&H,&W,&h,&w); if (H%h==0 && W%w==0) printf("No\n"); else { bsc=2000; printf("Yes\n"); rep(i,1,H) { rep(j,1,W) if (i%h==0 && j%w==0) printf("%d ",-bsc*(h*w-1)-1); else printf("%d ",bsc); printf("\n"); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int k; long long sum=0; int main() { int H,W,h,w; scanf("%d%d%d%d",&H,&W,&h,&w); k=int(1e9)/h/w; for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) { if(i%h==0&&j%w==0)sum-=(h*w-1)*k+1; else sum+=k; } if(sum<=0) { puts("No"); return 0; } puts("Yes"); for(int i=1;i<=H;i++) { for(int j=1;j<=W;j++) { if(i%h==0&&j%w==0)printf("%d ",-(h*w-1)*k-1); else printf("%d ",k); } puts(""); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = map(int, input().split()) if h == w == 1 : print('No') else : a = (10 ** 9 - 1) // (h * w - 1) ret = [[a] * W for _ in range(H)] for y in range(h - 1, H, h) : for x in range(w - 1, W, w) : ret[y][x] = - a * (h * w - 1) - 1 s = 0 for y in range(H) : s += sum(ret[y]) if s > 0 : print('Yes') for r in ret : print(*r) else : print('No')
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){ int H,W,h,w,i=1,j; scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h||W%w){ for(puts("Yes");i<=H;++i,puts("")) for(j=0;j<W;++j,printf("%d ",(i%h||j%w)?1000:-1000*(h*w-1)-1)); }else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
# include <bits/stdc++.h> using namespace std; int ans[1005][1005]; int main(){ int R, C, h, w; cin >> R >> C >> h >> w; int d = 1000, sum = 0; for(int i=1; i<=R; i++){ for(int j=1; j<=C; j++){ if(i%h == 0 && j%w== 0){ ans[i][j] = -((h*w-1)*d) - 1; } else ans[i][j] = d; sum += ans[i][j]; } } if(sum < 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; for(int i=1; i<=R; i++){ for(int j=1; j<=C; j++) cout << ans[i][j] << " "; cout << endl; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cstdio> #define R register int main() { R int H, W, h, w; scanf("%d%d%d%d", &H, &W, &h, &w); if (H % h == 0 && W % w == 0) puts("No"); else { puts("Yes"); for (R int i = 1; i <= H; ++i, puts("")) for (R int j = 1; j <= W; ++j) if (i % h == 0 && j % w == 0) printf("%d ", -1000 * (h * w - 1) - 1); else printf("%d ", 1000); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, N) for (int i = 0; i < (int)N; i++) int main () { int H, W, h, w; scanf("%d %d %d %d", &H, &W, &h, &w); if(H % h == 0 && W % w == 0) { printf("No\n"); return 0; } printf("Yes\n"); int x = - 10*(h*w - 1)*(H/h)*(W/w) - 1; int y = 10*(H/h)*(W/w); rep(i,H){ rep(j,W) { if(j != 0) printf(" "); printf("%d", i % h == h - 1 && j % w == w - 1 ? x : y); } printf("\n"); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; const int N=505,inf=1e9; int n,m,h,w; int main(){ scanf("%d%d%d%d",&n,&m,&h,&w); if(n%h==0&&m%w==0)puts("No"),exit(0); puts("Yes"); for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)printf("%d%c",i%h||j%w?4000:3999-4000*w*h," \n"[j==m]); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; const int NMAX = 500 + 5; int mat[NMAX][NMAX]; int main() { int H, W, h, w; cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { cout << "No\n"; return 0; } mat[0][0] = 1000000000 - 1; mat[h - 1][w - 1] = -1000000000; for (int i = 0; i < H; ++ i) for (int j = 0; j < W; ++ j) mat[i][j] = mat[i % h][j % w]; cout << "Yes\n"; for (int i = 0; i < H; ++ i) for (int j = 0; j < W; ++ j) cout << mat[i][j] << " \n"[j + 1 == W]; return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int H, W, h, w; cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { cout << "No\n"; return 0; } cout << "Yes\n"; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (i % h == h - 1 && j % w == w - 1) { cout << -1403 * (h * w - 1) - 1 << " "; } else { cout << 1403 << " "; } } cout << "\n"; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> #include <cstdio> int main() { int H, W, h, w; scanf("%d%d%d%d", &H, &W, &h, &w); int a = 4000, b = -(h * w - 1) * a - 1; if(H % h == 0 && W % w == 0) return 0 * puts("No"); puts("Yes"); for(int i = 1; i <= H; i++, puts("")) for(int j = 1; j <= W; j++) { if(i % h == 0 && j % w == 0) printf("%d ", b); else printf("%d ", a); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> int A,B,a,b; int main(){ scanf("%d%d%d%d",&A,&B,&a,&b); if(A%a==0&&B%b==0)return puts("No"),0; puts("Yes"); for(int i=0;i<A;++i){ for(int j=0;j<B;++j){ printf("%d ",i%a||j%b?-1237:1237*(a*b-1)-1); }puts(""); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> #define ll long long using namespace std; int H,W,n,m,i,j,x; int main(){ scanf("%d%d%d%d",&H,&W,&n,&m); if(H%n==0&&W%m==0)return puts("No"),0; puts("Yes"); x=(H/n)*(W/m)+1; for(i=1;i<=H;i++){ for(j=1;j<=W;j++)if(i%n==0&&j%m==0)printf("%d ",-x*(n*m-1)-1); else printf("%d ",x); puts(""); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){ int H,W,h,w,i=1,j; scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h||W%w){ puts("Yes"); for(;i<=H;++i,puts("")) for(j=0;j<W;++j,printf("%d ",(i%h||j%w)?1000:-1000*(h*w-1)-1)); }else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> #define For(i, j, k) for(int i = j; i <= k; i++) using namespace std; const int N = 510; int H, W, h, w; int ans[N][N]; int main(){ scanf("%d%d%d%d", &H, &W, &h, &w); int cx = H / h, cy = W / w; int sum = H * W - cx * cy * h * w; int C = 1e8 / (h * w); if(sum <= 0){ puts("No"); return 0; } puts("Yes"); For(i, 1, H) For(j, 1, W) ans[i][j] = C; For(i, 1, cx) For(j, 1, cy) ans[i * h][j * w] = -(h * w - 1) * C - 1; For(i, 1, H) For(j, 1, W) printf("%d%c", ans[i][j], j == W ? '\n' : ' '); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> #include<cstdio> #define eps 4000 using namespace std; int H,W,h,w,map[505][505]; long long sum; int main() { scanf("%d%d%d%d",&H,&W,&h,&w); for(int i=h;i<=H;i+=h) for(int j=w;j<=W;j+=w) map[i][j]=-eps*(h*w-1)-1,sum+=map[i][j]; for(int i=1;i<=H;i++) for(int j=1;j<=W;j++) if(i%h||j%w) map[i][j]=eps,sum+=map[i][j]; if(sum<=0) { puts("No"); return 0; } puts("Yes"); for(int i=1;i<=H;i++) { for(int j=1;j<=W;j++) printf("%d ",map[i][j]); puts(""); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.*; import java.util.*; public class Main { static void solve() { int H = ni(), W = ni(), h = ni(), w = ni(); int[][] a = new int[H][W]; int inf = 1_000_000_000; if(h * w == 1) { out.println("No"); return; } for(int i=0;i<H;i++)for(int j=0;j<W;j++) { if((i + 1) % h == 0 && (j + 1) % w == 0) { a[i][j] = -inf; }else { a[i][j] = (inf - 1) / (h * w - 1); } } long sum = 0; for(int i=0;i<H;i++)for(int j=0;j<W;j++) sum += a[i][j]; if(sum > 0) { out.println("Yes"); Arrays.stream(a).forEach(i -> printAll(i)); }else { out.println("No"); } } //constants static final int inf = Integer.MAX_VALUE / 2; static final long linf = Long.MAX_VALUE / 3; static final double dinf = Double.MAX_VALUE / 3; static final long mod = (long) 1e9 + 7; static final int[] dx = { -1, 0, 1, 0 }, dy = { 0, -1, 0, 1 }, dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final double eps = 1e-10, pi = Math.PI; static StringBuilder sb = new StringBuilder(); //libraries static void printAll(int[] a) { Arrays.stream(a).forEach(i->out.print(i + " ")); out.println(); } static void printAll(long[] a) { Arrays.stream(a).forEach(i->out.print(i + " ")); out.println(); } static void printAll(int[] a, int add) { Arrays.stream(a).forEach(i->out.print(i + add + " ")); out.println(); } static void printAll(long[] a, long add) { Arrays.stream(a).forEach(i->out.print(i + add + " ")); out.println(); } static List<Integer>[] makeTree(List<Integer>[] graph, int vertexNum, int edgeNum) { graph = new ArrayList[vertexNum]; for(int i=0;i<vertexNum;i++)graph[i] = new ArrayList<>(); for(int i=0;i<edgeNum;i++) { int u = ni()-1, v = ni()-1; graph[u].add(v); graph[v].add(u); } return graph; } static long[] cum(int a[]) { long[] cum = new long[a.length + 1]; for(int i=0;i<a.length;i++) cum[i+1] = cum[i] + a[i]; return cum; } static long[] cum(long a[]) { long[] cum = new long[a.length + 1]; for(int i=0;i<a.length;i++) cum[i+1] = cum[i] + a[i]; return cum; } static long sum(int a[]) { long res = 0; for(int i=0;i<a.length;i++) res += a[i]; return res; } static long sum(long a[]) { long res = 0; for(int i=0;i<a.length;i++) res += a[i]; return res; } static void reverse(int ar[]) { int len = ar.length; for (int i = 0; i < len / 2; i++) { int t = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = t; } } static void reverse(long ar[]) { int len = ar.length; for (int i = 0; i < len / 2; i++) { long t = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = t; } } static void reverse(double ar[]) { int len = ar.length; for (int i = 0; i < len / 2; i++) { double t = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = t; } } static void reverse(char ar[]) { int len = ar.length; for (int i = 0; i < len / 2; i++) { char t = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = t; } } static String getReverse(String s) { char c[] = s.toCharArray(); reverse(c); s = String.valueOf(c); return s; } static <T> void reverse(List<T> ls) { int sz = ls.size(); for (int i = 0; i < sz / 2; i++) { T t = ls.get(i); ls.set(i, ls.get(sz - 1 - i)); ls.set(sz - 1 - i, t); } } static <T> void reverse(T[] ar) { int len = ar.length; for (int i = 0; i < len / 2; i++) { T t = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = t; } } static void sbnl() {//StringBuilderに改行文字をappendする sb.append("\n"); } static int lowerBound(int[] a, int x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] < x) { l = c; } else { r = c; } } return r; } static int upperBound(int[] a, int x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] <= x) { l = c; } else { r = c; } } return r; } static int rlowerBound(int[] a, int x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] > x) { l = c; } else { r = c; } } return r; } static int rupperBound(int[] a, int x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] >= x) { l = c; } else { r = c; } } return r; } static int lowerBound(long[] a, long x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] < x) { l = c; } else { r = c; } } return r; } static int upperBound(long[] a, long x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] <= x) { l = c; } else { r = c; } } return r; } static int rlowerBound(long[] a, long x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] > x) { l = c; } else { r = c; } } return r; } static int rupperBound(long[] a, long x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] >= x) { l = c; } else { r = c; } } return r; } static int lowerBound(double[] a, double x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] < x) { l = c; } else { r = c; } } return r; } static int upperBound(double[] a, double x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] <= x) { l = c; } else { r = c; } } return r; } static int rlowerBound(double[] a, double x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] > x) { l = c; } else { r = c; } } return r; } static int rupperBound(double[] a, double x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] >= x) { l = c; } else { r = c; } } return r; } static int lowerBound(char[] a, char x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] < x) { l = c; } else { r = c; } } return r; } static int upperBound(char[] a, char x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] <= x) { l = c; } else { r = c; } } return r; } static int rlowerBound(char[] a, char x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] > x) { l = c; } else { r = c; } } return r; } static int rupperBound(char[] a, char x) { int l = -1, r = a.length; while (r - l > 1) { int c = (l + r) / 2; if (a[c] >= x) { l = c; } else { r = c; } } return r; } static <T> int lowerBound(List<T> ls, T x) throws RuntimeException { if (ls.size() == 0) return -1; if (ls.get(0) instanceof Integer) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) >= 0 ? 1 : -1); } else if (ls.get(0) instanceof Long) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) >= 0 ? 1 : -1); } else if (ls.get(0) instanceof Double) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) >= 0 ? 1 : -1); } else { System.err.println( String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName())); throw new RuntimeException(); } } static <T> int upperBound(List<T> ls, T x) throws RuntimeException { if (ls.size() == 0) return -1; if (ls.get(0) instanceof Integer) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) > 0 ? 1 : -1); } else if (ls.get(0) instanceof Long) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) > 0 ? 1 : -1); } else if (ls.get(0) instanceof Double) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) > 0 ? 1 : -1); } else { System.err.println( String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName())); throw new RuntimeException(); } } static <T> int rupperBound(List<T> ls, T x) throws RuntimeException { if (ls.size() == 0) return -1; if (ls.get(0) instanceof Integer) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) < 0 ? 1 : -1); } else if (ls.get(0) instanceof Long) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) < 0 ? 1 : -1); } else if (ls.get(0) instanceof Double) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) < 0 ? 1 : -1); } else { System.err.println( String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName())); throw new RuntimeException(); } } static <T> int rlowerBound(List<T> ls, T x) { if (ls.size() == 0) return -1; if (ls.get(0) instanceof Integer) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Integer) t1).compareTo((Integer) t2) <= 0 ? 1 : -1); } else if (ls.get(0) instanceof Long) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Long) t1).compareTo((Long) t2) <= 0 ? 1 : -1); } else if (ls.get(0) instanceof Double) { return ~Collections.binarySearch(ls, x, (t1, t2) -> ((Double) t1).compareTo((Double) t2) <= 0 ? 1 : -1); } else { System.err.println( String.format("%s:数値でないリストを二分探索しています。", Thread.currentThread().getStackTrace()[1].getMethodName())); throw new RuntimeException(); } } static int[] concat(int x, int arr[]) { int ret[] = new int[arr.length + 1]; System.arraycopy(arr, 0, ret, 1, ret.length - 1); ret[0] = x; return ret; } static int[] concat(int arr[], int x) { int ret[] = new int[arr.length + 1]; System.arraycopy(arr, 0, ret, 0, ret.length - 1); ret[ret.length - 1] = x; return ret; } static long[] concat(long x, long arr[]) { long ret[] = new long[arr.length + 1]; System.arraycopy(arr, 0, ret, 1, ret.length - 1); ret[0] = x; return ret; } static long[] concat(long arr[], long x) { long ret[] = new long[arr.length + 1]; System.arraycopy(arr, 0, ret, 0, ret.length - 1); ret[ret.length - 1] = x; return ret; } static char[] concat(char x, char arr[]) { char ret[] = new char[arr.length + 1]; System.arraycopy(arr, 0, ret, 0, ret.length - 1); ret[ret.length - 1] = x; return ret; } static char[] concat(char arr[], char x) { char ret[] = new char[arr.length + 1]; System.arraycopy(arr, 0, ret, 0, ret.length - 1); ret[ret.length - 1] = x; return ret; } static int max(int x, int y) { return Math.max(x, y); } static int min(int x, int y) { return Math.min(x, y); } static int max(int x, int y, int z) { x = Math.max(x, y); x = Math.max(x, z); return x; } static int min(int x, int y, int z) { x = Math.min(x, y); x = Math.min(x, z); return x; } static long max(long x, long y) { return Math.max(x, y); } static long min(long x, long y) { return Math.min(x, y); } static long max(long x, long y, long z) { x = Math.max(x, y); x = Math.max(x, z); return x; } static long min(long x, long y, long z) { x = Math.min(x, y); x = Math.min(x, z); return x; } static double max(double x, double y) { return Math.max(x, y); } static double min(double x, double y) { return Math.min(x, y); } static double max(double x, double y, double z) { x = Math.max(x, y); x = Math.max(x, z); return x; } static double min(double x, double y, double z) { x = Math.min(x, y); x = Math.min(x, z); return x; } static void sort(int[] ar) { Arrays.sort(ar); } static void sort(long[] ar) { Arrays.sort(ar); } static void sort(double[] ar) { Arrays.sort(ar); } static void sort(char[] ar) { Arrays.sort(ar); } static void rsort(int[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { int tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void rsort(long[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { long tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void rsort(double[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { double tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void rsort(char[] ar) { Arrays.sort(ar); int len = ar.length; for (int i = 0; i < len / 2; i++) { char tmp = ar[i]; ar[i] = ar[len - 1 - i]; ar[len - 1 - i] = tmp; } } static void fill(int arr[], int x) { Arrays.fill(arr, x); } static void fill(long arr[], long x) { Arrays.fill(arr, x); } static void fill(boolean arr[], boolean x) { Arrays.fill(arr, x); } static void fill(double arr[], double x) { Arrays.fill(arr, x); } static void fill(int arr[][], int x) { for (int i = 0; i < arr.length; i++) Arrays.fill(arr[i], x); } static void fill(long arr[][], long x) { for (int i = 0; i < arr.length; i++) Arrays.fill(arr[i], x); } static void fill(double arr[][], double x) { for (int i = 0; i < arr.length; i++) Arrays.fill(arr[i], x); } static void fill(boolean arr[][], boolean x) { for (int i = 0; i < arr.length; i++) Arrays.fill(arr[i], x); } //MOD culc static long plus(long x, long y) { long res = (x + y) % mod; return res < 0 ? res + mod : res; } static long sub(long x, long y) { long res = (x - y) % mod; return res < 0 ? res + mod : res; } static long mul(long x, long y) { long res = (x * y) % mod; return res < 0 ? res + mod : res; } static long div(long x, long y) { long res = x * pow(y, mod - 2) % mod; return res < 0 ? res + mod : res; } static long pow(long x, long y) { if (y < 0) return 0; if (y == 0) return 1; if (y % 2 == 1) return (x * pow(x, y - 1)) % mod; long root = pow(x, y / 2); return root * root % mod; } public static void main(String[] args) throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); } //input static InputStream is; static PrintWriter out; static String INPUT = ""; private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private static int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } @SuppressWarnings("unused") private static double nd() { return Double.parseDouble(ns()); } @SuppressWarnings("unused") private static char nc() { return (char) skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } @SuppressWarnings("unused") private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } @SuppressWarnings("unused") private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } @SuppressWarnings("unused") private static long[] nla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } @SuppressWarnings("unused") private static int[][] na(int n, int m){ int[][] res = new int[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { res[i][j] = ni(); } } return res; } @SuppressWarnings("unused") private static long[][] nla(int n, int m){ long[][] res = new long[n][m]; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { res[i][j] = nl(); } } return res; } @SuppressWarnings("unused") private static int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } @SuppressWarnings("unused") private static long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; LL n,m,h,w; LL val[505][505] = {0}; int main(){ cin >> n >> m >> h >> w; if(!(n % h || m % w)){ cout << "No" << endl; return 0; } cout << "Yes" << endl; for(LL i = h;i <= n;i += h) for(LL j = w;j <= m;j += w) val[i][j] = - (h * w * 1000 - 999); for(LL i = 1;i <= n;i ++) for(LL j = 1;j <= m;j ++) cout << (val[i][j] ? val[i][j] : 1000) << (j == m ? '\n' : ' '); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> using namespace std; const int N=502; int ar[N]; int main(){ int n,m,a,b,i,j; cin>>n>>m>>a>>b; if(n%a==0&&m%b==0){ cout<<"No"; return 0; } cout<<"Yes\n"; if(n%a!=0){ for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ if(i%a==1){ cout<<69696*(a-1)-1<<' '; } else{ cout<<-69696<<' '; } } cout<<"\n"; } } else{ for(i=1;i<=n;i++){ for(j=1;j<=m;j++){ if(j%b==1){ cout<<69696*(b-1)-1<<' '; } else{ cout<<-69696<<' '; } } cout<<'\n'; } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> #include<algorithm> using namespace std; const int N=550; int i,j,k,H,W,h,w,fg,t; int a[N][N]; int main() { scanf("%d%d%d%d",&H,&W,&h,&w); if (H%h==0 && W%w==0) return puts("No"),0; puts("Yes"); if (H%h) { for (i=1;i<=H;i++) { for (j=1;j<=W;j++) { if (i%h==0) t=-1000000*(h-1); else t=999999; printf("%d ",t); } puts(""); } } else { for (i=1;i<=H;i++) { for (j=1;j<=W;j++) { if (j%w==0) t=-1000000*(w-1); else t=999999; printf("%d ",t); } puts(""); } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; int main(){ int H, W, h, w; cin >> H >> W >> h >> w; if(H % h == 0 && W % w == 0){ cout << "No" << endl; return 0; } cout << "Yes" << endl; int arr[H][W]; fill(arr[0], arr[H], -100000); if(H % h) for(int i = 0; i < H; i += h) for(int j = 0; j < W; j++) arr[i][j] = 100000 * h - 100001; else for(int i = 0; i < H; i++) for(int j = 0; j < W; j += w) arr[i][j] = 100000 * w - 100001; for(int i = 0; i < H; i++){ for(int j = 0; j < W; j++){ if(j) cout << ' '; cout << arr[i][j]; } cout << endl; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> using namespace std; const int maxn = 10000000; int main() { int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); if(a % c == 0 && b % d == 0) { printf("No\n"); } else { int nz = maxn / (c * d - 1); int nf = (c * d - 1) * nz + 1; printf("Yes\n"); for(int i = 1;i <= a;++i) { for(int j = 1;j <= b;++j) { if(i % c == 0 && j % d == 0) printf("-%d ",nf); else printf("%d ",nz); } putchar('\n'); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> #define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++) using namespace std; typedef long long ll; ll H, W, A, B; int main(void) { cin >> H >> W >> A >> B; if(A * B == 1) { cout << "No" << endl; return 0; } ll d = A * B - 1; ll u = (1000000000LL - 1) / d; ll m = u * d + 1; ll p = (H / A) * (W / B); ll s = (H * W - p) * u - p * m; if(s > 0) { cout << "Yes" << endl; REP(i, 0, H) REP(j, 0, W) cout << ((i + 1) % A == 0 && (j + 1) % B == 0 ? -m : u) << (j + 1 != W ? " " : "\n"); } else { cout << "No" << endl; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int H, W, h, w; cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { cout << "No\n"; return 0; } cout << "Yes\n"; for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { if (i % h == h - 1 && j % w == w - 1) { cout << -1000 * (h * w - 1) - 1 << " "; } else { cout << 1000 << " "; } } cout << "\n"; } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int H = ni(), W = ni(), h = ni(), w = ni(); int[][] a = new int[H][W]; for(int i = 0;i < H;i+=h){ for(int j = 0;j < W;j+=w){ a[i][j] = 999999999; } } for(int i = h-1;i < H;i+=h){ for(int j = w-1;j < W;j+=w){ a[i][j] = -1000000000; } } long s = 0; for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ s += a[i][j]; } } if(s > 0){ out.println("Yes"); for(int i = 0;i < H;i++){ for(int j = 0;j < W;j++){ out.print(a[i][j] + " "); } out.println(); } }else{ out.println("No"); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); tr(G-S+"ms"); } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double nd() { return Double.parseDouble(ns()); } private static char nc() { return (char)skip(); } private static String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private static int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private static int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int H = sc.nextInt(), W = sc.nextInt(), h = sc.nextInt(), w = sc.nextInt(); if (H%h == 0 && W%w == 0) { System.out.println("No"); } else { System.out.println("Yes"); for (int i = 0; i < H; ++i) { for (int j = 0; j < W; ++j) { System.out.print(i%h == h-1 && j%w == w-1 ? -4000*h*w+3999 : 4000); System.out.print(' '); } System.out.println(); } } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
h, w, hh, ww = [int(i) for i in input().split()] area = [h*w, hh*ww] m = (h//hh) * (w // ww) a_inv = (area[0] / m - area[1]) if(a_inv <= 0): print("No") else: print("Yes") a = a_inv ** -1 if(int(a) < a): a = int(a) + 1 else: a = int(a) b = (area[1] - 1) * a + 1 while(b >= (area[0]/m - 1)*a): a += 1 b = (area[1] - 1) * a + 1 a = str(a) b = str(-b) for i in range(h): ret = [None]*w for j in range(w): if((i+1) % hh == 0 and (j+1) % ww == 0): ret[j] = b else: ret[j] = a print(" ".join(ret))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
from collections import defaultdict,deque import sys,heapq,bisect,math,itertools,string,queue,datetime sys.setrecursionlimit(10**8) INF = float('inf') mod = 10**9+7 eps = 10**-7 def inpl(): return list(map(int, input().split())) def inpls(): return list(input().split()) H,W,h,w = inpl() MAP = [[0]*W for i in range(H)] pl = mn = 0 tmp = 0 for y in range(H): for x in range(W): if (y+1)%h==0 and (x+1)%w==0: MAP[y][x] -= (h*w-1)*1000 +1 mn += 1 tmp -= (h*w-1)*1000 +1 else: MAP[y][x] = 1000 pl += 1 tmp += 1000 if tmp <= 0: print('No') else: print('Yes') for m in MAP: print(' '.join(map(str,m)))
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] sa = br.readLine().split(" "); int H = Integer.parseInt(sa[0]); int W = Integer.parseInt(sa[1]); int h = Integer.parseInt(sa[2]); int w = Integer.parseInt(sa[3]); br.close(); if (H % h == 0 && W % w == 0) { System.out.println("No"); return; } System.out.println("Yes"); int p = 100000000; if (H % h != 0) { int nh = -(p / (h - 1) + 1); StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); sb1.append(p); sb2.append(nh); for (int j = 1; j < W; j++) { sb1.append(' ').append(p); sb2.append(' ').append(nh); } String s1 = sb1.toString(); String s2 = sb2.toString(); for (int i = 0; i < H; i++) { if (i % h == 0) { System.out.println(s1); } else { System.out.println(s2); } } } else { int nw = -(p / (w - 1) + 1); StringBuilder sb = new StringBuilder(); sb.append(p); for (int j = 1; j < W; j++) { if (j % w == 0) { sb.append(' ').append(p); } else { sb.append(' ').append(nw); } } String s = sb.toString(); for (int i = 0; i < H; i++) { System.out.println(s); } } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<stdio.h> int main(){ int H,W,h,w,i=1,j; scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h||W%w) for(puts("Yes");i<=H;++i,puts("")) for(j=0;j<W;++j,printf("%d ",(i%h||j%w)?1000:-1000*(h*w-1)-1)); else puts("No"); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int test; int main(){ long long H,W,h,w; cin>>H>>W>>h>>w; long long matr[H][W]; long long num=(H/h)*(W/w)+1; for(int i=0;i<H;i++){ for(int j=0;j<W;j++) matr[i][j]=num; } if(H%h==0&&W%w==0) cout<<"No"<<endl; else{ cout<<"Yes"<<endl; for(int i=h-1;i<H;i+=h){ for(int j=w-1;j<W;j+=w){ matr[i][j]=(-1)*(num*h*w-num)-1; } } for(int i=0;i<H;i++){ for(int j=0;j<W;j++) cout<<matr[i][j]<<" "; cout<<endl; } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws Exception { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(in, out); out.close(); } } class Task { public void solve(InputReader in, PrintWriter out) { int H = in.nextInt(); int W = in.nextInt(); int h = in.nextInt(); int w = in.nextInt(); if (H % h == 0 && W % w == 0) { out.println("No"); return; } int cnt = H / h * (W / w); out.println("Yes"); for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (j > 1) out.print(" "); if (i % h == 0 && j % w == 0) { out.print(-1000 * w * h + 999); } else { out.print(1000); } } out.println(); } } } class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(nextLine()); } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; const int inf=1e9,N=520; int H,W,h,w,dat,a[N][N]; int main(){ scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h==0&&W%w==0)puts("No"),exit(0); dat=(inf-1)/(h*w-1);puts("Yes"); for(int i=1;i<=H;i++,puts("")) for(int j=1;j<=W;j++) printf("%d ",i%h||j%w?dat:-inf); return 0; } // 500 499 500 299
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; #define ll long long #define f(i, x, n) for(int i = x; i < (int)(n); ++i) int g[500][500]; int main(){ int n, m, h, w; scanf("%d%d%d%d", &n, &m, &h, &w); if (n % h == 0 && m % w == 0) { printf("No\n"); return 0; } f(i, 0, n)f(j, 0, m)g[i][j] = 2000; int z = 2000 * h * w - 2000 + 1; z = -z; int a = n / h, b = m / w; f(i, 0, a)f(j, 0, b)g[(i + 1) * h - 1][(j + 1) * w - 1] = z; printf("Yes\n"); f(i, 0, n){ printf("%d", g[i][0]); f(j, 1, m)printf(" %d", g[i][j]); printf("\n"); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
def compose(W, w): A = [0]*W S = [0]*(W+1) S[W%w] = 1000 for j in range(W-w+1): S[j+w] = S[j] - 1 for j in range(W): A[j] = S[j+1] - S[j] return A H, W, h, w = map(int, input().split()) if H%h == 0 and W%w == 0: print("No") else: print("Yes") if W%w: S = compose(W, w) for _ in range(H): print(*S) else: S = compose(H, h) for i in range(H): print(*[S[i] for _ in range(W)])
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { long H, W, h, w; cin >> H >> W >> h >> w; auto t = (H / h) * (W / w), k = H * W - (H / h) * (W / w) * h * w; if(k == 0)return 0 & puts("No"); puts("Yes"); auto c = 1 + t / k, l = c * (h * w - 1) + 1; for(unsigned long i = 1; i <= H; ++i){ for(unsigned long j = 1; j <= W; ++j)cout << (i % h || j % w ? c : -l) << " "; cout << endl; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; int H, W, h, w; int main(void) { cin >> H >> W >> h >> w; if(H % h == 0 && W % w == 0){ cout << "No" << endl; return 0; } cout << "Yes" << endl; int pval = W/w * H/h * 2; int nval = -pval * (w * h - 1) - 1; for(int y = 1; y <= H; y++){ for(int x = 1; x <= W; x++){ if(x % w == 0 && y % h == 0) cout << nval; else cout << pval; if(x != W) cout << " "; } cout << endl; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int i,j,a[505][505]; int H,W,h,w; int main() { scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h==0&&W%w==0) { printf("No\n"); return 0; } printf("Yes\n"); for(i=1;i<=H;i++) { for(j=1;j<=W;j++) { if(!(i%h==0&&j%w==0)) { a[i][j]=500; } else a[i][j]=-500*h*w+499; } } for(i=1;i<=H;i++) { for(j=1;j<=W;j++) printf("%d ",a[i][j]); printf("\n"); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> using namespace std; int main (){ int H, W, h, w; cin >> H >> W >> h >> w; if (H%h == 0 && W%w == 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; int N = 1000000; if (W%w != 0) { for (int i=0; i<H; i++) { for (int j=0; j<W; j++) { cout << (j ? " " : ""); cout << (j%w == 0 ? (w-1)*N-1 : -N); } cout << endl; } } else { for (int i=0; i<H; i++) { for (int j=0; j<W; j++) { cout << (j ? " " : ""); cout << (i%h == 0 ? (h-1)*N-1 : -N); } cout << endl; } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <stdio.h> #include <stdlib.h> using namespace std; int n,m,a,b,i,j,k; int c[505][505]; int main() { scanf("%d%d%d%d",&n,&m,&a,&b); if(n%a==0&&m%b==0) { printf("No\n"); return 0; } printf("Yes\n"); for(i=1;i<=n;++i) for(j=1;j<=m;++j) c[i][j]=1000000; if(n%a) { for(i=a;i<=n;i+=a) for(j=1;j<=m;++j) c[i][j]=-(a-1)*1000000-1; } else { for(j=b;j<=m;j+=b) for(i=1;i<=n;++i) c[i][j]=-(b-1)*1000000-1; } for(i=1;i<=n;++i) for(j=1;j<=m;++j) { printf("%d",c[i][j]); if(j==m)printf("\n"); else printf(" "); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import sys # sys.stdin = open('c1.in') def read_int_list(): return list(map(int, input().split())) def read_int(): return int(input()) def read_str_list(): return input().split() def read_str(): return input() H, W, h, w = read_int_list() def solve(H, W, h, w): if H % h == 0 and W % w == 0: return False, None a = [[0] * W for _ in range(H)] m = 10 ** 9 - 1 M = m + 1 for i in range(0, H, h): for j in range(0, W, w): a[i][j] = m for i in range(h - 1, H, h): for j in range(w - 1, W, w): a[i][j] = -M return True, a possible, res = solve(H, W, h, w) if possible: print('Yes') for row in res: print(' '.join(map(str, row))) else: print('No')
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; ll H, W, h, w; int main() { scanf("%lld %lld %lld %lld", &H, &W, &h, &w); if (h == 1 && w == 1) { printf("No\n"); return 0; } ll p = 999999999 / (h*w-1), k = -p*(h*w-1)-1; ll c = (H/h) * (W/w); if (c*k + (H*W-c)*p <= 0) printf("No\n"); else { printf("Yes\n"); for (ll r = 1; r <= H; r++) { for (ll c = 1; c <= W; c++) printf("%lld ", r % h == 0 && c % w == 0 ? k:p); printf("\n"); } } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; const int inf=1e9,N=520; int H,W,h,w,dat,a[N][N]; int main(){ scanf("%d%d%d%d",&H,&W,&h,&w); if(H%h==0&&W%w==0)puts("No"),exit(0); dat=(inf-1)/(h*w-1);puts("Yes"); for(int i=1;i<=H;i++,puts("")) for(int j=1;j<=W;j++) printf("%d ",i%h||j%w?dat:-inf); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> #include<algorithm> using namespace std; int main(){ int h, w, H, W, i, j; scanf("%d%d%d%d",&H,&W,&h,&w); int t = (H/h)*(W/w); int p = (1e9-1)/(h*w-1); if(1ll*H*W*p-1ll*t*p-t*(1ll*(h*w-1)*p+1) <= 0){ printf("No\n"); return 0; } printf("Yes\n"); for(i=1;i<=H;i++){ for(j=1;j<=W;j++){ if(i%h==0&&j%w==0)printf("%lld ",-(1ll*(h*w-1)*p+1)); else printf("%d ",p); } printf("\n"); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int n,m,h,w,a[502][502],v,i,j; long long sum; int main(){ scanf("%d%d%d%d",&n,&m,&h,&w),v=(1-h*w)*1000-1; for (i=1;i<=n;i++) for (j=1;j<=m;j++) a[i][j]=(i%h==0 && j%w==0?v:1000),sum+=a[i][j]; if (sum<=0) return puts("No"),0; puts("Yes"); for (i=1;i<=n;i++,puts("")) for (j=1;j<=m;j++) printf("%d ",a[i][j]); }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int main() { int H, W, h, w, Swap = 0; cin >> H >> W >> h >> w; if(H % h == 0 && W % w == 0) { puts("No"); } else { puts("Yes"); int val = (h * w - 1) * 500 + 1; for(int i = 1; i <= H; i++) { for(int j = 1; j <= W; j++) { if(i % h == 0 && j % w == 0) printf("%d ", -val); else printf("500 "); } puts(""); } } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> #define neko 510 #define f(i,a,b) for(register int i=(a);i<=(b);i=-(~i)) int n,m,h,w,a[neko][neko],sum,p=2333; int main() { //特殊点是h*w的时候,普通点的1权重较小,当特殊点数大于后面多余的格子个数的时候就凉了 scanf("%d%d%d%d",&n,&m,&h,&w); for(register int i=h;i<=n;i+=h) for(register int j=w;j<=m;j+=w) a[i][j]=-(h*w-1)*p-1; f(i,1,n) f(j,1,m) if(!a[i][j])a[i][j]=p,sum+=p; else sum+=a[i][j]; if(sum<=0)return printf("No\n"),0; printf("Yes\n"); f(i,1,n) f(j,1,m)printf("%d%c",a[i][j],j^m?' ':'\n'); }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <iostream> #include <cstdio> #include <vector> #include <string> #include <algorithm> #include <map> #include <set> int main() { int H, W, h, w; std::cin >> H >> W >> h >> w; if (H % h == 0 && W % w == 0) { puts("No"); return 0; } constexpr int INF = 1e9; puts("Yes"); for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { if (i % h == 0 && j % w == 0) { printf("%d ", INF - 1); } else if (i % h == h - 1 && j % w == w - 1) { printf("%d ", -INF); } else { printf("%d ", 0); } } puts(""); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) using ll = long long; const int INF = 1e9; int main(){ int H, W, h, w; cin >> H >> W >> h >> w; if(H % h == 0 && W % w == 0) { cout << "No" << endl; return 0; } cout << "Yes" << endl; int a = (INF-1)/(h*w); rep(i,H) { rep(j,W) { if(i % h == h-1 && j % w == w-1) cout << -a*(h*w-1)-1 << " "; else cout << a << " "; } cout << endl; } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.IntStream; import static java.util.Arrays.*; public class Main { private static final int mod = (int)1e9+7; final Random random = new Random(0); final IOFast io = new IOFast(); /// MAIN CODE public void run() throws IOException { // int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim()); int TEST_CASE = 1; while(TEST_CASE-- != 0) { int H = io.nextInt(); int W = io.nextInt(); int h = io.nextInt(); int w = io.nextInt(); int[][] mat = new int[H][W]; // for (int[] m : mat) Arrays.fill(m, 1); for (int y = 0; y < H; y += h) { for (int x = 0; x < W; x += w) { mat[y][x] = (int)1e9-1; } } for (int y = h - 1; y < H; y += h) { for (int x = w - 1; x < W; x += w) { mat[y][x] = -(int)1e9; } } long sum = 0; for (int[] m : mat) for (int x : m) sum += x; if (sum <= 0) { io.out.println("No"); return; } io.out.println("Yes"); for (int[] m : mat) printArrayLn(m); } } /// TEMPLATE static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); } static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); } static <T> void swap(T[] x, int i, int j) { T t = x[i]; x[i] = x[j]; x[j] = t; } static void swap(int[] x, int i, int j) { int t = x[i]; x[i] = x[j]; x[j] = t; } void printArrayLn(int[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); } void printArrayLn(long[] xs) { for(int i = 0; i < xs.length; i++) io.out.print(xs[i] + (i==xs.length-1?"\n":" ")); } static void dump(Object... o) { System.err.println(Arrays.deepToString(o)); } void main() throws IOException { // IOFast.setFileIO("rle-size.in", "rle-size.out"); try { run(); } catch (EndOfFileRuntimeException e) { } io.out.flush(); } public static void main(String[] args) throws IOException { new Main().main(); } static class EndOfFileRuntimeException extends RuntimeException { private static final long serialVersionUID = -8565341110209207657L; } static public class IOFast { private BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); private PrintWriter out = new PrintWriter(System.out); void setFileIn(String ins) throws IOException { in.close(); in = new BufferedReader(new FileReader(ins)); } void setFileOut(String outs) throws IOException { out.flush(); out.close(); out = new PrintWriter(new FileWriter(outs)); } void setFileIO(String ins, String outs) throws IOException { setFileIn(ins); setFileOut(outs); } private static int pos, readLen; private static final char[] buffer = new char[1024 * 8]; private static char[] str = new char[500*8*2]; private static boolean[] isDigit = new boolean[256]; private static boolean[] isSpace = new boolean[256]; private static boolean[] isLineSep = new boolean[256]; static { for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; } isDigit['-'] = true; isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true; isLineSep['\r'] = isLineSep['\n'] = true; } public int read() throws IOException { if(pos >= readLen) { pos = 0; readLen = in.read(buffer); if(readLen <= 0) { throw new EndOfFileRuntimeException(); } } return buffer[pos++]; } public int nextInt() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; int ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; } public long nextLong() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); int i = 0; long ret = 0; if(str[0] == '-') { i = 1; } for(; i < len; i++) ret = ret * 10 + str[i] - '0'; if(str[0] == '-') { ret = -ret; } return ret; } public char nextChar() throws IOException { while(true) { final int c = read(); if(!isSpace[c]) { return (char)c; } } } int reads(int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } if(str.length == len) { char[] rep = new char[str.length * 3 / 2]; System.arraycopy(str, 0, rep, 0, str.length); str = rep; } str[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } int reads(char[] cs, int len, boolean[] accept) throws IOException { try { while(true) { final int c = read(); if(accept[c]) { break; } cs[len++] = (char)c; } } catch(EndOfFileRuntimeException e) { ; } return len; } public char[] nextLine() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isLineSep); try { if(str[len-1] == '\r') { len--; read(); } } catch(EndOfFileRuntimeException e) { ; } return Arrays.copyOf(str, len); } public String nextString() throws IOException { return new String(next()); } public char[] next() throws IOException { int len = 0; str[len++] = nextChar(); len = reads(len, isSpace); return Arrays.copyOf(str, len); } // public int next(char[] cs) throws IOException { int len = 0; cs[len++] = nextChar(); len = reads(cs, len, isSpace); return len; } public double nextDouble() throws IOException { return Double.parseDouble(nextString()); } public long[] nextLongArray(final int n) throws IOException { final long[] res = new long[n]; for(int i = 0; i < n; i++) { res[i] = nextLong(); } return res; } public int[] nextIntArray(final int n) throws IOException { final int[] res = new int[n]; for(int i = 0; i < n; i++) { res[i] = nextInt(); } return res; } public int[][] nextIntArray2D(final int n, final int k) throws IOException { final int[][] res = new int[n][]; for(int i = 0; i < n; i++) { res[i] = nextIntArray(k); } return res; } public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException { final int[][] res = new int[n][k+1]; for(int i = 0; i < n; i++) { for(int j = 0; j < k; j++) { res[i][j] = nextInt(); } res[i][k] = i; } return res; } public double[] nextDoubleArray(final int n) throws IOException { final double[] res = new double[n]; for(int i = 0; i < n; i++) { res[i] = nextDouble(); } return res; } } }
JAVA
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = map(int, input().split()) rem = H * W - (H - H % h) * (W - W % w) if rem == 0: print('No') exit() x = (H // h * W // w + rem) // rem if abs(x) > 10**9 or abs(-(h * w - 1) * x - 1) > 10**9: print('No') exit() print('Yes') a = [[x] * W for _ in range(H)] for i in range(h-1, H, h): for j in range(w-1, W, w): a[i][j] = -(h * w - 1) * x - 1 for row in a: print(*row)
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<cstdio> #include<algorithm> #define MAX 1000000000 #define MAXN 506 using namespace std; int n,m,a,b,s[MAXN][MAXN]; int main() { scanf("%d%d%d%d",&n,&m,&a,&b); if(n%a==0&&m%b==0) { printf("No"); return 0; } int k=MAX/(a*b); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(i%a!=0||j%b!=0) s[i][j]=k; else s[i][j]=-(a*b-1)*k-1; printf("Yes"); for(int i=1;i<=n;i++) { printf("\n"); for(int j=1;j<=m;j++) printf("%d ",s[i][j]); } }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int H, W, h, w, a[501][501]; long long sum; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> H >> W >> h >> w; for(int k1 = 1; h*k1 <= H; k1++) for(int k2 = 1; w*k2 <= W; k2++) a[h*k1][w*k2] = -1000*(h*w-1)-1, sum += a[h*k1][w*k2]; for(int i = 1; i <= H; i++) for(int j = 1; j <= W; j++) if(!a[i][j]) { a[i][j] = 1000; sum += 1000; } if(sum <= 0) cout << "No", exit(0); cout << "Yes\n"; for(int i = 1; i <= H; i++, cout << '\n') for(int j = 1; j <= W; j++) cout << a[i][j] << ' '; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
# coding:utf-8 import sys input = sys.stdin.readline def inpl(): return list(map(int, input().split())) def solve(): H, W, h, w = inpl() if H % h == 0 and W % w == 0: print('No') return -1 ans = [[1000] * W for _ in range(H)] cnt = 0 for i in range(h - 1, H, h): for j in range(w - 1, W, w): ans[i][j] = -(h * w - 1) * 1000 - 1 cnt += 1 print('Yes') for row in ans: print(' '.join(map(str, row))) return 0 solve()
PYTHON3
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> #include <cstdio> using namespace std; int a[1000][1000], H, W, h, w; int main(void) { scanf("%d%d%d%d", &H, &W, &h, &w); if ((H%h == 0) && (W%w == 0)) {printf("No\n"); return 0;} printf("Yes\n"); a[0][0]= (H/h)*(W/w) + 1; a[h - 1][w - 1] = -a[0][0] - 1; for (int i =0; i < H; ++i) { for (int j = 0; j < W; ++j) { printf("%d ",a[i%h][j%w]); } printf("\n"); } return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<iostream> #include<algorithm> #include<cstdio> #define mx 500 using namespace std; long long n,m,h,w; int main() { long long i,j,u,v; cin>>n>>m>>h>>w; if(n%h==0 && m%w==0) { cout<<"No"<<endl; } else { cout<<"Yes"<<endl; u=(h*w-1)*mx+1; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(i%h==0 && j%w==0) { cout<<-u<<" "; } else { cout<<mx<<" "; } } cout<<endl; } } return(0); }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include <bits/stdc++.h> using namespace std; int H,W,w,h; int a[505][505]; const int INF =2333; #define GG return printf("No"),0 int main() { scanf("%d%d%d%d",&H,&W,&h,&w); if (H%h==0&&W%w==0) GG; printf("Yes\n"); for (int i=1;i<=H;++i) for (int j=1;j<=W;++j) printf("%d%c",(i%h==0&&j%w==0?(1-h*w)*INF-1:INF),j==W?'\n':' '); return 0; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
#include<bits/stdc++.h> using namespace std; int main(){ int n,m,a,b;cin>>n>>m>>a>>b; vector<vector<int>>v(n, vector<int>(m)); for(int i=a-1;i<n;i+=a){ for(int j=b-1;j<m;j+=b){ v[i][j]=-1000000; } } for(int i=0;i<n;i+=a){ for(int j=0;j<m;j+=b)v[i][j]=999999; } if(n%a||m%b){ cout<<"Yes"<<endl; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(j)cout<<' '; cout<<v[i][j]; } cout<<endl; } } else cout<<"No"<<endl; }
CPP
p03689 AtCoder Grand Contest 016 - +/- Rectangle
You are given four integers: H, W, h and w (1 ≤ h ≤ H, 1 ≤ w ≤ W). Determine whether there exists a matrix such that all of the following conditions are held, and construct one such matrix if the answer is positive: * The matrix has H rows and W columns. * Each element of the matrix is an integer between -10^9 and 10^9 (inclusive). * The sum of all the elements of the matrix is positive. * The sum of all the elements within every subrectangle with h rows and w columns in the matrix is negative. Constraints * 1 ≤ h ≤ H ≤ 500 * 1 ≤ w ≤ W ≤ 500 Input Input is given from Standard Input in the following format: H W h w Output If there does not exist a matrix that satisfies all of the conditions, print `No`. Otherwise, print `Yes` in the first line, and print a matrix in the subsequent lines in the following format: a_{11} ... a_{1W} : a_{H1} ... a_{HW} Here, a_{ij} represents the (i,\ j) element of the matrix. Examples Input 3 3 2 2 Output Yes 1 1 1 1 -4 1 1 1 1 Input 2 4 1 2 Output No Input 3 4 2 3 Output Yes 2 -5 8 7 3 -5 -4 -5 2 1 -1 7
6
0
H, W, h, w = map(int, input().split()) grid = [[0] * W for _ in range(H)] inf = 10**9 for i in range(H): for j in range(W): if i%h == h-1 and j%w == w-1: grid[i][j] = -inf elif i%h == 0 and j%w == 0: grid[i][j] = inf-1 if sum([sum(line) for line in grid]) > 0: print('Yes') for i in range(H): print(*grid[i]) else: print('No')
PYTHON3