Cod sursa(job #381897)

Utilizator mgntMarius B mgnt Data 11 ianuarie 2010 22:40:40
Problema Cifre Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <iostream>
#include <fstream>
using namespace std;

// Given A,B,C,K.
// 0<=A<=B<=1 * 1000 * 1000 * 1000
// 0<=C,K<=9
//
// Count how many numbers in [A..B]
// have at least K digits C, return
// count/(B-A+1) with 4 decimals
// after dot.

typedef long long int bnum;

// Returns greatest power of b
// less or equal t.
// PRE: 0<n
bnum
lowerb(bnum t, bnum b)
{
  bnum p=1, q;
  while ( ((q=b*p)<=t) ) { p=q; }
  return p;
}

bnum
hexuprec(bnum n)
{
  if ( 0xa > n ) { return 1+n; }
  bnum x=hexuprec(n/0xa),
      y=1+(n%0xa), z=y+(0x10*x);
  return z;
}

bnum
hexup(bnum n)
{
  if ( 0 == n ) { return 1; }
  return hexuprec(n);
}

bnum
hexdnrec(bnum n)
{
  if ( 0x10 > n ) { return n-1; }
  bnum x=hexdnrec(n/0x10),
       y=(n%0x10)-1, z=y+(0xa*x);
  return z;
}

bnum
hexdn(bnum n)
{
  if ( 1 == n ) { return 0; }
  return hexdnrec(n);
}

bnum
aaaa(bnum z)
{
  bnum p=0xa,q;
  while ( (q=0x10*p+0xa)<z ) { p=q; }
  return p;
}

bnum
atleasthfree(int c, bnum t, int s)
{
  // Each n in {0<=n<=hexdn(t)} has
  // at least 0 digits c.
  if ( 0 == s ) { return 1+hexdn(t); }

  // ... single digit
  if ( t<0x10 ) {
    return ((c<=t)&&(1==s))?(1):(0);
  }
  
  bnum z=lowerb(t,0x10), w=0, r=t%z, d=t/z;
  if ( 1<d )
  {
    bnum zz=aaaa(z), x, y;
    x = atleasthfree(c,zz,s);
    y = atleasthfree(c,zz,s-1);
    w += ((1==c)?y:x);
    int i;
    for ( i=2; d>i; ++i )
      { w += ((i==c)?y:x); }

  }
  
  w += atleasthfree(c,r,s-((c==d)?1:0));
  return w;
}

// Returns how many n in {0<=n<=hexdn(t)}
// have at least s digits c-1 in decimal
// representation.
// prec: t=hexup(tt) for some tt,
// prec: 1<=c<=0xa
// 1==f <--> first digit can be cut to 1
bnum
atleasth(int c, bnum t, int s)
{
  // Each n in {0<=n<=hexdn(t)} has
  // at least digits c.
  if ( 0 == s ) { return 1+hexdn(t); }
  
  // t is representable by a single digit.
  if ( t<0x10 ) {
    return ((c<=t)&&(1==s))?(1):(0);
  }

  // t is representable by a single digit.
  if ( t<0x10 ) {
    return ((c<=t)&&(1==s))?(1):(0);
  }

  bnum z=lowerb(t,0x10), w=0, r=t%z, d=t/z,
      zz=aaaa(z),
      x=atleasthfree(c,zz,s),
      y=atleasthfree(c,zz,s-1);
  // 1<d is true
  w += atleasth(c,zz,s); // digit 1
  int i;
  for ( i=2; d>i; ++i )
    { w += ((c==i)?(y):(x)); } // digit i

  w += atleasthfree(c,r,s - ((c==d)?(1):(0))); // rest
  return w; // count
}

// Returns how many n in {0<=n<=t}
// have at least s digits C in decimal
// representation.
// prec: 0<=s, 0<=c<10
// !!! c may be 0
bnum
atleastd(int c, int t, int s)
{
  bnum x = hexup(t);
  return atleasth(1+c,x,s);
}

void
pdt(ostream & os, bnum d)
{
  os << "0123456789"[d];
}

void
pnt (ostream & os, bnum a, int b)
{
  if ( a==b ) {
    os << "1.0000" << endl;
    return;
  }
  os << "0.";
  a *= 10; pdt(os,a/b); a %= b;
  a *= 10; pdt(os,a/b); a %= b;
  a *= 10; pdt(os,a/b); a %= b;
  a *= 10; pdt(os,a/b); a %= b;
  os << endl;
}

int
main ( )
{
  ifstream is("cifre.in");
  ofstream os("cifre.out");
  int A,B,C,K;
  is >> A >> B >> C >> K;
  bnum count = atleastd(C,B,K)-atleastd(C,A-1,K);
  pnt(os,count,B-A+1);
  return 0;
}