CSES - Datatähti 2019 alku - Results
Submission details
Task:Ruudukko
Sender:Katajisto
Submission time:2018-10-07 21:37:47 +0300
Language:C++
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'long long int aa(long long int)':
input/code.cpp:13:6: error: 'c' was not declared in this scope
   if(c.count(d) != 0) return c[d];
      ^
input/code.cpp:15:3: error: 'c' was not declared in this scope
   c[d] = b;
   ^

Code

// Los_toteutus.cpp

#include<bits/stdc++.h>
using namespace std;

long m = 1e9+7;
long mod = m;

long long aa(long long d) {
  if(d == 0) return 1;
  if(d == 1) return 0;
  if(c.count(d) != 0) return c[d];
  long long b = (d-1)*((aa(d-1)%mod)+(aa(d-2)%mod)); b %= mod;
  c[d] = b;
  return b;
}

long f(long a) {
  long ans = 1;
  for(long i = 1; i <= a; i++) {
    ans *= i; ans %= m;
  }
  return ans;
}

long nc(long n, long k) {
  if(k == 0) return 1;
  return ((n * nc(n-1,k-1))/k)%m;
}



int main() {
  int n; cin >> n;
  string s[n]; for(int i = 0; i < n; i++) cin >> s[i];
  bool xa[500], xb[500], ya[500], yb[500];
  int ac = 0; int bc = 0; int acc = 0; int bcc = 0;
  for(int i = 0; i < 500; i++) { xa[i] = false; xb[i] = false; ya[i] = false; yb[i] = false; }
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      if(s[i][j] == 'B') {
	xb[j] = true;
	yb[i] = true;
	bc++; bcc++;
      }
      if(s[i][j] == 'A') {
	xa[j] = true;
	ya[i] = true;
	ac++; acc++;
      }
    }
  }
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      if(s[i][j] == 'B') {
	if(xa[j] || ya[i]) bcc--;
      }
      if(s[i][j] == 'A') {
	if(xb[j] || yb[i]) acc--;
      }
    }
  }
  long balku = f(n-bc);
  bool flip = false;
  for(int i = 1; i <= acc; i++) {
    if(!flip) balku -= f(n-bc-i)*nc(acc,i);
    else balku += nc(acc,i)*f(n-bc-i);
    flip = !flip;
    if(balku < 0) balku += m;
    balku %= m;
  }
  long aalku = f(n-ac);
  flip = false;
  for(int i = 1; i <= bcc; i++) {
    if(!flip) aalku -= f(n-ac-i)*nc(bcc,i);
    else aalku += nc(bcc,i)*f(n-ac-i);
    flip = !flip;
    if(aalku < 0) aalku += m;
    aalku %= m;
  }
  if(aalku == 0) {
    cout << balku*aa(n);
  }
  else {
    cout << aalku*aa(n);
  }
}