Cod sursa(job #801414)

Utilizator mgntMarius B mgnt Data 24 octombrie 2012 11:16:47
Problema Pascal Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.55 kb
#include <fstream>
using namespace std;

size_t const maxr = 5000000; // size_t, because it is used to define the size of F
char F[1+maxr];
char C[1+maxr];

void
count
( size_t  p
, size_t  n
)
{ C[1] = 0;
  size_t s;
  for(s=2; n>=s; ++s)
  { if (0 == (s%p))
    { C[s]=1+C[s/p]; }
    else
    { C[s]=0; }
  }
}

void
filter
( int     dt
, size_t  n
)
{ int fn;
  size_t s;
  fn = 0;
  for(s=1; n>=s; ++s)
  { fn += C[(n-s+1)];
    fn -= C[s];
    if (fn < dt)
    { F[s]=0; }
  }
}

void
prune
( size_t  d
, size_t  n
)
{ if (2 == d) { count(2,n); filter(1,n); return; }
  if (3 == d) { count(3,n); filter(1,n); return; }
  if (4 == d) { count(2,n); filter(2,n); return; }
  if (5 == d) { count(5,n); filter(1,n); return; }
  if (6 == d) { count(2,n); filter(1,n);
                count(3,n); filter(1,n); return; }
}

bool
process
(
)
{ // read input
  size_t R;
  size_t D;
  ::std::ifstream is("pascal.in");
  is >> R >> D;
  bool ok;
  ok = is.good();
  if(!ok)
  { return ok; }
  ok = ((0 <= R) && (R <= maxr));
  if(!ok)
  { return ok; }
  ok = ((2 <= D) && (D <= 6));
  if(!ok)
  { return ok; }
  // solve
  size_t s;
  F[0]=0;
  for(s=1; R>=s; ++s)
  { F[s]=1; }
  prune(D,R);
  int a;
  a = 0;
  for(s=0; R>=s; ++s)
  { if(0 != F[s])
    { ++ a; }
  }
  // write output
  ::std::ofstream os("pascal.out");
  os << a << ::std::endl;
  ok = os.good();
  if(!ok)
  { return ok; }
  os.close();
  ok = os.good();
  return ok;
}

int
main
(
)
{ bool ok;
  int  status;
  try
  { ok = process();
    status = ( ok ? 0 : 1 );
  }
  catch ( ... )
  { status = 2;
  }
  return status;
}