题目大意:给定H,W,h,w四个数,求是否满足矩阵的全部数之和和正数,h行w列之和为负数
如果h和w恰好是H,W的约数,则肯定不存在
否则肯定存在
只需要把h,w内每个元素填的足够大,然后小矩形的最后一个元素为负,且保持整个小矩形为负即可(可用不等式证明)
#include <iostream> #include <cstring> #include <cstdio> using namespace std; long long Mat[505][505]; long long n, m, r, c; int main() { cin>>r>>c>>n>>m; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) Mat[i][j] = 1000; for(int i = n; i <= r; i += n) for(int j = m; j <= c; j += m) Mat[i][j] = -n*m*1000 + 999; long long sum = 0; for(int i = 1; i <= r; i++) for(int j = 1; j <= c; j++) sum += Mat[i][j]; if(sum < 0) { cout<<"No"<<endl; } else { cout<<"Yes"<<endl; for(int i = 1; i <= r; i++){ for(int j = 1; j <= c; j++) cout<<Mat[i][j]<<" "; cout<<endl; } } }
时间: 2024-11-05 05:50:29