Submission #923649

# Submission time Handle Problem Language Result Execution time Memory
923649 2024-02-07T14:17:09 Z GrindMachine Red-blue table (IZhO19_stones) C++17
69 / 100
19 ms 7004 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*



*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

void solve(int test_case)
{
    ll n,m; cin >> n >> m;
 
    if(n == 1){
        cout << m << endl;
        rep1(j,m) cout << "-";
        cout << endl;
        return;
    }
 
    if(m == 1){
        cout << n << endl;
        rep1(i,n) cout << "+" << endl;
        return;
    }
 
    if((n&1) and (m&1)){
        ll a[n+5][m+5];
        memset(a,0,sizeof a);
        rep1(i,n) a[i][1] = 1;
        rep1(i,n-1){
            if(i&1){
                for(int j = 2; j <= ceil2(m,2); ++j){
                    a[i][j] = 1;
                }
            }
            else{
                for(int j = ceil2(m,2)+1; j <= m; ++j){
                    a[i][j] = 1;
                }
            }
        }
 
        ll ans = n+m-2;
        cout << ans << endl;
 
        rep1(i,n){
            rep1(j,m){
                if(a[i][j] == 1) cout << "+";
                else cout << "-";
            }
            cout << endl;
        }

        return;
    }

    if(min(n,m) <= 7){
        bool swapped = false;
        if(n < m){
            swap(n,m);
            swapped = true;
        }

        ll a[n+5][m+5];
        memset(a,0,sizeof a);

        if(m == 2){
            rep1(i,n){
                rep1(j,2){
                    a[i][j] = 1;
                }
            }
        }

        if(m == 3 or m == 4){
            rep1(i,n){
                rep1(j,m/2+1){
                    a[i][j] = 1;
                }
            }
        }

        if(m == 5 or m == 6 or m == 7){
            rep1(i,n){
                rep1(j,m/2){
                    a[i][j] = 1;
                }
            }

            ll ptr = m/2+1;
            ll cnt = 0;

            rep1(i,n){
                if((cnt+1)*2 >= n){
                    ptr++;
                    cnt = 0;
                }

                a[i][ptr] = 1;
                cnt++;
            }
        }

        ll ans = 0;
        rep1(i,n){
            ll cnt = 0;
            rep1(j,m){
                cnt += a[i][j];
            }
            if(cnt*2 > m) ans++;
        }
        rep1(j,m){
            ll cnt = 0;
            rep1(i,n){
                cnt += a[i][j]^1;
            }
            if(cnt*2 > n) ans++;
        }

        cout << ans << endl;

        if(!swapped){
            rep1(i,n){
                rep1(j,m){
                    if(a[i][j] == 1) cout << "+";
                    else cout << "-";
                }
                cout << endl;
            }
        }
        else{
            ll a2[m+5][n+5];
            rep1(i,n) rep1(j,m) a2[j][i] = a[i][j]^1;
            swap(n,m);

            rep1(i,n){
                rep1(j,m){
                    if(a2[i][j] == 1) cout << "+";
                    else cout << "-";
                }
                cout << endl;
            }   
        }

        return;
    }

    ll a[n+5][m+5];
    memset(a,0,sizeof a);
    ll subn = 0, subm = 0;
    if(!(n&1)) subn = 1, n--;
    if(!(m&1)) subm = 1, m--;

    rep1(i,n) a[i][1] = 1;
    rep1(i,n-1){
        if(i&1){
            for(int j = 2; j <= ceil2(m,2); ++j){
                a[i][j] = 1;
            }
        }
        else{
            for(int j = ceil2(m,2)+1; j <= m; ++j){
                a[i][j] = 1;
            }
        }
    }

    if(subn){
        // add extra row
        n++;
    }

    if(subm){
        // add extra col
        m++;
        rep1(i,n) a[i][m] = 1;
    }

    ll ans = 0;
    rep1(i,n){
        ll cnt = 0;
        rep1(j,m){
            cnt += a[i][j];
        }
        if(cnt*2 > m) ans++;
    }
    rep1(j,m){
        ll cnt = 0;
        rep1(i,n){
            cnt += a[i][j]^1;
        }
        if(cnt*2 > n) ans++;
    }

    cout << ans << endl;

    rep1(i,n){
        rep1(j,m){
            if(a[i][j] == 1) cout << "+";
            else cout << "-";
        }
        cout << endl;
    }

    /*
 
    int n,m; cin >> n >> m;
    int a[n][m];
 
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    int ans = 0;
 
    rep(iter,1e7){
        rep(i,n){
            rep(j,m){
                a[i][j] = rng()&1;
            }
        }
 
        int val = 0;
 
        rep(i,n){
            int cnt_one = 0;
            rep(j,m){
                cnt_one += a[i][j];
            }
 
            if(cnt_one*2 > m){
                val++;
            }
        }
 
        rep(j,m){
            int cnt_zero = 0;
            rep(i,n){
                cnt_zero += a[i][j]^1;
            }
 
            if(cnt_zero*2 > n){
                val++;
            }
        }
 
        amax(ans,val);
 
        // if(val == 10){
        //     rep(i,n){
        //         rep(j,m){
        //             cout << a[i][j];
        //         }
        //         cout << endl;
        //     }
        //     cout << endl;
        // }
    }
 
    cout << ans << endl;
 
    */
}

int main()
{
    fastio;

    int t = 1;
    cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1372 KB Output is correct
2 Correct 18 ms 6492 KB Output is correct
3 Correct 17 ms 7004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 1628 KB Output is correct
2 Correct 16 ms 5212 KB Output is correct
3 Correct 19 ms 3612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 17 ms 1372 KB Output is correct
6 Correct 18 ms 6492 KB Output is correct
7 Correct 17 ms 7004 KB Output is correct
8 Correct 17 ms 1628 KB Output is correct
9 Correct 16 ms 5212 KB Output is correct
10 Correct 19 ms 3612 KB Output is correct
11 Incorrect 5 ms 484 KB Wrong answer in test 26 8: 30 < 31
12 Halted 0 ms 0 KB -