This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#define N 1010
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mod 1000000007
#define pii pair <int, int>
using namespace std;
int t;
int n, m;
char v[N][N];
int main()
{
	cin >> t;
	
	while(t--) {
		cin >> n >> m;
		
		int ans = max(n, m);
		for(int i = 1; i <= n; i++)
			for(int h = 1; h <= m; h++)
				v[i][h] = (n > m ? '+' : '-');
		
		if(n < m) {
			priority_queue <pii, vector <pii>, greater<pii>> Q;
			
			for(int i = 1; i <= m; i++) Q.push({0, i});
			
			int li = m/2 + 1;
			int sz = n/2 - (n%2 == 0 ? 1 : 0);
			for(int i = 1; i <= n; i++) {
				vector <pii> A;
				
				for(int h = 1; h <= li; h++) {
					pii x = Q.top();
					Q.pop();
					
					if(x.ff == sz) break;
					
					A.pb(x);
					
					if(Q.empty()) break;
				}
				
				if(A.size() < li) break;
				
				ans++;
				for(auto h: A) v[i][h.ss] = '+', Q.push({h.ff+1, h.ss});
			}
		}
		else {
			priority_queue <pii, vector <pii>, greater<pii>> Q;
			
			for(int i = 1; i <= n; i++) Q.push({0, i});
			
			int li = n/2 + 1;
			int sz = m/2 - (m%2 == 0 ? 1 : 0);
			for(int i = 1; i <= m; i++) {
				vector <pii> A;
				
				for(int h = 1; h <= li; h++) {
					pii x = Q.top();
					Q.pop();
					
					if(x.ff == sz) break;
					
					A.pb(x);
					
					if(Q.empty()) break;
				}
				
				if(A.size() < li) break;
				
				ans++;
				for(auto h: A) v[h.ss][i] = '+', Q.push({h.ff+1, h.ss});
			}
		}
		
		cout << ans << "\n";
		for(int i = 1; i <= n; i++) {
			for(int h = 1; h <= m; h++)
				cout << v[i][h];
			
			cout << "\n";
		}
	}
}
Compilation message (stderr)
stones.cpp: In function 'int main()':
stones.cpp:57:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(A.size() < li) break;
        ~~~~~~~~~^~~~
stones.cpp:84:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     if(A.size() < li) break;
        ~~~~~~~~~^~~~| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |