Ejemplo n.º 1
0
struct answer solve(int t, int k)
{
    if (t < 0) {

        /* twig */
        int ball = (t == -2);

        switch (k) {
            case 0:  return (struct answer) { 0, (ball ? 0 : 1) };
            case 1:  return (struct answer) { (ball ? 0 : 1), -1 };
            default: return (struct answer) { -1, -1 };
        }

    } else {

        /* internal node */
        struct answer a0 = solve(tree[t][0], k / 2);
        struct answer a1 = solve(tree[t][1], k / 2);

        if (k % 2 == 0) {
            return (struct answer) {
                aplus(a0.a, a1.a),
                amin(aplus(a0.a, a1.b), aplus(a0.b, a1.a)) };
        } else {
            return (struct answer) {
                amin(aplus(a0.a, a1.b), aplus(a0.b, a1.a)),
                aplus(a0.b, a1.b) };
        }

    }
}


int main(void)
{
    while (fgets(buf, sizeof(buf), stdin) != NULL) {

        bufp = 0;
        nnode = 0;
        ntwig = 0;
        nball = 0;
        int root = parse();
        assert(root == 0);

        assert(buf[bufp] == '\n' && buf[bufp+1] == '\0');
        assert(ntwig >= MINTWIGS && ntwig <= MAXTWIGS);

        struct answer answer = solve(root, nball);
        if (answer.a >= 0)
            printf("%d\n", answer.a);
        else
            printf("impossible\n");
    }

    return 0;
}
Ejemplo n.º 2
0
double SBVAR::LogLikelihood(void)
{
  double log_likelihood=log_likelihood_constant + lambda_T*LogAbsDeterminant(A0);
  TDenseVector a0(n_vars), aplus(n_predetermined);

  for (int i=n_vars-1; i >= 0; i--)
    {
      a0.RowVector(A0,i);
      aplus.RowVector(Aplus,i);
      log_likelihood+=-0.5*(InnerProduct(a0,a0,YY) - 2.0*InnerProduct(aplus,a0,XY) + InnerProduct(aplus,aplus,XX));
    }

  return log_likelihood;
}
Ejemplo n.º 3
0
double SBVAR_symmetric::LogPrior(void)
{
  if (flat_prior) return 0.0;

  double log_prior=log_prior_constant;
  TDenseVector a0(n_vars), aplus(n_predetermined);

  for (int i=n_vars-1; i >= 0; i--)
    {
      a0.RowVector(A0,i);
      aplus.RowVector(Aplus,i);
      log_prior+=-0.5*(InnerProduct(a0,a0,prior_YY) - 2.0*InnerProduct(aplus,a0,prior_XY) + InnerProduct(aplus,aplus,prior_XX));
    }

  return log_prior;
}