Cod sursa(job #14565)

Utilizator vlad_popaVlad Popa vlad_popa Data 9 februarie 2007 12:23:01
Problema Pascal Scor 60
Compilator c Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <stdio.h>

int p, r, d, c, i, ct1, ct2, ct3, a, b, ct, a2, b2, a3, b3, lim, verif;
//char s[5000001][1];

int fact (int n, int p)
{
  int i, sol = 0, t = p;
  while (p <= n)
   { sol += n / p;
     p *= t;
   }
  return sol;
}

int
 main ()
{
  freopen ("pascal.in", "rt", stdin);
  freopen ("pascal.out", "wt", stdout);
  scanf ("%d%d", &r, &d);
  if (r == 2 && d == 2)
   { printf ("1\n"); return 0; }
  lim = r / 2;
  if (d == 6)
   { a = fact (r, 2);
     b = fact (r, 3);
     for (c = 0; c <= lim; c++)
      { a2 = fact (r - c, 2);
        b2 = fact (r - c, 3);
        a3 = fact (c, 2);
        b3 = fact (c, 3);
        if (a > a2 + a3 && b > b2 + b3)
          ct++;
        if (c == lim && r % 2 == 0 && a > a2 + a3 && b > b2 + b3)
          verif = 1;
      }
   }
  else if (d == 4)
   { ct1 = fact (r, 2);
     for (c = 0; c <= lim; c++)
      { ct2 = fact (r - c, 2);
        ct3 = fact (c, 2);
        if (ct1 > ct2 + ct3 + 1)
          ct++;
        if (c == lim && r % 2 == 0 && ct1 > ct2 + ct3 + 1)
          verif = 1;
      }
    }
  else
   { ct1 = fact (r, d);
     for (c = 0; c <= lim; c++)
      { ct2 = fact (r - c, d);
        ct3 = fact (c, d);
        if (ct1 > ct2 + ct3)
          ct++;
        if (c == lim && r % 2 == 0 && ct1 > ct2 + ct3)
          verif = 1;
      }
   }
  if (r % 2 == 1)
    ct <<= 1;
  else
    ct += (ct - verif);
  printf ("%d\n", ct);
  return 0;
}