844B - Rectangles - CodeForces Solution


combinatorics math *1300

Please click on ads to support us..

Python Code:

from operator import mul    from fractions import Fraction
from functools import reduce

sol_super_comb = {i:0 for i in range(51)}

def retangles():
  n, m = map(int, input().split())
  vectors = [[[0]*n, [0]*m], [[0]*n, [0]*m]]
  maior = max(n,m)

  [[summs(vectors, row, col, 1) if x=='1' else summs(vectors, row, col, 0) for col,x in enumerate(input().split())] for row in range(n)]

  soma=n*m
  for elem_one_zero in vectors:
    for row_or_col in elem_one_zero:
      for i in row_or_col:
        if(i>1):
          soma+=super_comb(i)
  print(soma)

def summs(v, r, c, e):
  v[e][0][r]+=1
  v[e][1][c]+=1
  return 0

def super_comb(n):
  if(sol_super_comb[n]>0):
    return sol_super_comb[n]
  soma = 0
  for i in range(n):
    if(i>=1):
      soma+=comb(n,i+1)
  sol_super_comb[n] = soma
  return soma

  
def comb(n,k): 
  return int( reduce(mul, (Fraction(n-i, i+1) for i in range(k)), 1) )
retangles()
			   				   	 	 	 					 		  		

C++ Code:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    vector<vector<int>> a(n, vector<int>(m));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> a[i][j];
        }
    }

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        int cntBlack = 0;
        for (int j = 0; j < m; j++) {
            cntBlack += a[i][j];
        }
        ans += (1ll << cntBlack) - 1;
        ans += (1ll << (m - cntBlack)) - 1;
    }

    for (int j = 0; j < m; j++) {
        int cntBlack = 0;
        for (int i = 0; i < n; i++) {
            cntBlack += a[i][j];
        }
        ans += (1ll << cntBlack) - 1;
        ans += (1ll << (n - cntBlack)) - 1;
    }

    cout << ans - n * m;
}


Comments

Submit
0 Comments
More Questions

1523B - Lord of the Values
1406C - Link Cut Centroids
2409. Count Days Spent Together
2410. Maximum Matching of Players With Trainers
1604C - Di-visible Confusion
997A - Convert to Ones
218A - Mountain Scenery
486B - OR in Matrix
1405A - Permutation Forgery
1733A - Consecutive Sum
1733B - Rule of League
1733C - Parity Shuffle Sorting
1264A - Beautiful Regional Contest
1695A - Subrectangle Guess
467B - Fedor and New Game
252C - Points on Line
735C - Tennis Championship
992A - Nastya and an Array
554A - Kyoya and Photobooks
79B - Colorful Field
265B - Roadside Trees (Simplified Edition)
1362C - Johnny and Another Rating Drop
1214C - Bad Sequence
1091B - New Year and the Treasure Geolocation
244A - Dividing Orange
1061C - Multiplicity
1312A - Two Regular Polygons
801A - Vicious Keyboard
510B - Fox And Two Dots
616D - Longest k-Good Segment