Entries

スポンサーサイト (この記事を編集する[管理者用])

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

コメント

コメントの投稿

コメントの投稿
管理者にだけ表示を許可する

トラックバック

トラックバック URL
http://rsujskf.blog32.fc2.com/tb.php/1830-b35335a7
この記事にトラックバックする(FC2ブログユーザー)

Facebook Hacker Cup 2011 Round 1A [無効試合] 3問目 - First or Last (この記事を編集する[管理者用])

Source

Facebook Hacker Cup 2011 Round 1A [無効試合] 3問目
Facebook Hacker Cup 2011 Round 1A [無効試合]の参加記録

問題概要

R人でレースを行う.最初自分は最下位.
T箇所だけ,コーナーがあって,各場所について
 今の順位を維持するなら 1/p[k] の確率でクラッシュする
 1人だけ抜くなら 1/q[k] の確率でクラッシュする
という確率が与えられる.
クラッシュせずにR-1人抜く確率の最大値を有理数で厳密に求める問題.
Tは50以下,p[k],q[k]は500以下.

解法

多倍長の有理数が必要な問題.
greedyにp[k]/q[k]の値が小さい方から抜くことにする.
以下のコードは無駄にDPしているコード.

C言語のスパゲッティなコード (not verified)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

#define ll long long

#define BIG_INT_SIZE 30
#define BIG_INT_BASE 100000000LL
#define BIG_INT_DIGITS 8
#define BIG_INT_CHAR_SIZE 4000 /* BIG_INT_SIZE*BIG_INT_DIGITS+2以上 */

typedef struct rational_number{ll a,b;}rational;
typedef struct big_integer{ll a[BIG_INT_SIZE];}bigInt;
typedef struct big_rational{bigInt a,b;}bigRational;

int bigIntToChar(bigInt a,char ret[]);
void printBigInt(bigInt a);
void putBigInt(bigInt a);


ll llGCD(ll a,ll b){
  if(!a) return b; return llGCD(b%a,a);
}

rational rationalReduction(rational c){
  ll k; k=llGCD(c.a,c.b); c.a/=k; c.b/=k;
  if(c.b<0) c.a=-c.a, c.b=-c.b;
  return c;
}

int rationalSign(rational a){
  if(a.a<0) return -1; if(a.a>0) return 1; return 0;
}

int rationalIsZero(rational a){
  return (a.a==0);
}

int rationalIsPositive(rational a){
  return (a.a>0);
}

int rationalIsNegative(rational a){
  return (a.a<0);
}

rational rationalPlus(rational a,rational b){
  rational c;
  c.a=a.a*b.b+a.b*b.a; c.b=a.b*b.b;
  return rationalReduction(c);
}

rational rationalMinus(rational a,rational b){
  rational c;
  c.a=a.a*b.b-a.b*b.a; c.b=a.b*b.b;
  return rationalReduction(c);
}

rational rationalMultiple(rational a,rational b){
  rational c;
  c.a=a.a*b.a; c.b=a.b*b.b;
  return rationalReduction(c);
}

rational rationalDivision(rational a,rational b){
  rational c;
  c.a=a.a*b.b; c.b=a.b*b.a;
  return rationalReduction(c);
}

int rationalEqual(rational a,rational b){
  return (a.a*b.b-a.b*b.a==0);
}

int rationalGreaterThan(rational a,rational b){
  return (a.a*b.b-a.b*b.a>0);
}

int rationalLessThan(rational a,rational b){
  return (a.a*b.b-a.b*b.a<0);
}

rational llToRational(ll i){
  rational a; a.a=i; a.b=1; return a;
}

bigInt bigIntZero(){
  bigInt a; int i;
  rep(i,BIG_INT_SIZE) a.a[i]=0;
  return a;
}

bigInt bigIntOne(){
  bigInt a; int i;
  REP(i,1,BIG_INT_SIZE) a.a[i]=0; a.a[0]=1;
  return a;
}

bigInt bigIntOrder(bigInt a){
  int i; ll k;
  REP(i,1,BIG_INT_SIZE) if(a.a[i-1]<0 || a.a[i-1]>=BIG_INT_BASE){
    k=a.a[i-1]/BIG_INT_BASE; a.a[i-1]-=k*BIG_INT_BASE;
    if(a.a[i-1]<0) k--, a.a[i-1]+=BIG_INT_BASE; a.a[i]+=k;
  }
  if(a.a[BIG_INT_SIZE-1]<0){
    rep(i,BIG_INT_SIZE) a.a[i]=-a.a[i]; a=bigIntOrder(a);
    rep(i,BIG_INT_SIZE) a.a[i]=-a.a[i];
  }
  return a;
}

bigInt llToBigInt(ll a){
  bigInt c; int i;
  REP(i,1,BIG_INT_SIZE) c.a[i]=0; c.a[0]=a;
  return bigIntOrder(c);
}

int bigIntGreaterThan(bigInt a,bigInt b){
  int i;
  for(i=BIG_INT_SIZE-1;i>=0;i--){
    if(a.a[i]>b.a[i]) return 1;
    if(a.a[i]<b.a[i]) return 0;
  }
  return 0;
}

int bigIntIsZero(bigInt a){
  int i; rep(i,BIG_INT_SIZE) if(a.a[i]) return 0; return 1;
}

bigInt bigIntPlus(bigInt a,bigInt b){
  int i; bigInt c;
  rep(i,BIG_INT_SIZE) c.a[i]=a.a[i]+b.a[i];
  return bigIntOrder(c);
}

bigInt bigIntMinus(bigInt a,bigInt b){
  int i; bigInt c;
  rep(i,BIG_INT_SIZE) c.a[i]=a.a[i]-b.a[i];
  return bigIntOrder(c);
}

bigInt bigIntMultipleLL(bigInt a,ll b){
  int i; rep(i,BIG_INT_SIZE) a.a[i]*=b;
  return bigIntOrder(a);
}

bigInt bigIntMultiple(bigInt a,bigInt b){
  int i,j,ii,jj; bigInt c;
  for(ii=BIG_INT_SIZE-1;ii;ii--) if(a.a[ii]) break; ii++;
  if(ii==1) return bigIntMultipleLL(b,a.a[0]);
  for(jj=BIG_INT_SIZE-1;jj;jj--) if(b.a[jj]) break; jj++;
  if(jj==1) return bigIntMultipleLL(a,b.a[0]);
  rep(i,BIG_INT_SIZE) c.a[i]=0;
  rep(i,ii)if(a.a[i])for(j=0;j<jj&&i+j+1<BIG_INT_SIZE;j++) c.a[i+j]+=a.a[i]*b.a[j];
  return bigIntOrder(c);
}

void bigIntDivisionsLL(bigInt a,ll b,bigInt *c,ll *d){
  int i;
  rep(i,BIG_INT_SIZE) c->a[i]=a.a[i];
  for(i=BIG_INT_SIZE-1;i;i--)
    c->a[i-1]+=(c->a[i]%b)*BIG_INT_BASE, c->a[i]/=b;
  *d = c->a[0]%b; c->a[0]/=b;
}

/* c=a/b, d=a%b */
void bigIntDivisions(bigInt a,bigInt b,bigInt *c,bigInt *d){
  int i,j,s,sa,sb; ll ma,mb,mc; bigInt tmp;
  sa=bigIntSign(a); sb=bigIntSign(b);
  if(sa==-1) a=bigIntMultipleLL(a,-1);
  if(sb==-1) b=bigIntMultipleLL(b,-1);
  
  for(j=BIG_INT_SIZE-1;j;j--) if(b.a[j]) break;
  if(!j){
    REP(i,1,BIG_INT_SIZE) d->a[i]=0;
    bigIntDivisionsLL(a,b.a[0],c,&(d->a[0]));
  }else{
    for(i=BIG_INT_SIZE-1;i;i--) if(a.a[i]) break;
    s=i-j; if(s<0) s=0;
    rep(i,BIG_INT_SIZE) c->a[i]=0;
    while(s>=0){
      ma=0; mb=BIG_INT_BASE-1;
      while(ma!=mb){
        mc = (ma+mb)/2 + (ma+mb)%2;
        c->a[s]=mc; tmp=bigIntMultiple(*c,b);
        if(bigIntGreaterThan(tmp,a)) mb=mc-1; else ma=mc;
      }
      c->a[s]=ma; s--;
    }
    tmp = bigIntMultiple(b,*c);
    *d = bigIntMinus(a,tmp);
  }

  if(sa==-1 && sb==-1){
    *d=bigIntMultipleLL(*d,-1);
  } else if(sa==-1 && sb!=-1){
    *c=bigIntMultipleLL(*c,-1);
    *d=bigIntMultipleLL(*d,-1);
  } else if(sa!=-1 && sb==-1){
    *c=bigIntMultipleLL(*c,-1);
  }
}

bigInt bigIntDivision(bigInt a,bigInt b){
  bigInt c,d;
  bigIntDivisions(a,b,&c,&d);
  return c;
}

bigInt bigIntModular(bigInt a,bigInt b){
  bigInt c,d;
  bigIntDivisions(a,b,&c,&d);
  return d;
}

int bigIntSign(bigInt a){
  int i;
  for(i=BIG_INT_SIZE-1;i>=0;i--) if(a.a[i]){
    if(a.a[i]<0) return -1; else return 1;
  }
  return 0;
}

bigInt bigIntAbs(bigInt a){
  if(bigIntSign(a)==-1) return bigIntMultipleLL(a,-1LL); return a;
}

bigInt bigIntGCD(bigInt a,bigInt b){
  if(bigIntSign(a)==-1) a=bigIntMultipleLL(a,-1);
  if(bigIntSign(b)==-1) b=bigIntMultipleLL(b,-1);
  if(bigIntIsZero(a)) return b;
  return bigIntGCD(bigIntModular(b,a),a);
}

int bigIntToChar(bigInt a,char ret[]){
  int i,j,s=0,len=0; char ct[BIG_INT_CHAR_SIZE]; ll lt;
  if(bigIntSign(a)==-1){
    ret[0]='-'; len=bigIntToChar(bigIntMultipleLL(a,-1LL),ret+1); return len+1;
  }
  rep(i,BIG_INT_SIZE){
    lt=a.a[i]; rep(j,BIG_INT_DIGITS) ct[s++]=lt%10, lt/=10;
  }
  j=0;
  while(s--){
    if(ct[s]) j=1;
    if(j) ret[len++]=ct[s]+'0';
  }
  if(!len) ret[len++]='0';
  ret[len]='\0'; return len;
}

void printBigInt(bigInt a){
  int i,k; char tmp[BIG_INT_CHAR_SIZE];
  k=bigIntToChar(a,tmp); rep(i,k) putchar(tmp[i]);
}

void putBigInt(bigInt a){
  char tmp[BIG_INT_CHAR_SIZE];
  bigIntToChar(a,tmp); puts(tmp);
}

bigInt bigIntDivisionLL(bigInt a,ll b){
  bigInt res; ll tmp;
  bigIntDivisionsLL(a, b, &res, &tmp);
  return res;
}

bigInt bigIntSqrt(bigInt a){
  bigInt c1=bigIntZero(),c2=a,c,mul;

  while( bigIntGreaterThan(c2,c1) ){
    c = bigIntDivisionLL( bigIntPlus(bigIntPlus(c1,c2),bigIntOne()), 2);
    mul = bigIntMultiple(c,c);
    if( bigIntGreaterThan(mul,a) ) c2=bigIntMinus(c,bigIntOne()); else c1=c;
  }

  return c1;
}












int bigRationalIsZero(bigRational a){
  return bigIntIsZero(a.a);
}

bigRational bigRationalZero(){
  bigRational c; c.a=bigIntZero(); c.b=bigIntOne(); return c;
}

bigRational llToBigRational(ll a){
  bigRational c; c.a=llToBigInt(a); c.b=bigIntOne(); return c;
}

bigRational rationalToBigRational(rational a){
  bigRational c; c.a=llToBigInt(a.a); c.b=llToBigInt(a.b); return c;
}

bigRational twoLlToBigRational(ll a,ll b){
  bigRational c; c.a=llToBigInt(a); c.b=llToBigInt(b); return c;
}

bigRational bigRationalReduction(bigRational c){
  bigInt k=bigIntGCD(c.a,c.b); k=bigIntAbs(k);
  if(bigIntGreaterThan(k,bigIntOne()))
    c.a=bigIntDivision(c.a,k), c.b=bigIntDivision(c.b,k);
  return c;
}

bigRational bigRationalPlus(bigRational a,bigRational b){
  bigRational c;
  c.a=bigIntPlus( bigIntMultiple(a.a,b.b),bigIntMultiple(a.b,b.a) );
  c.b=bigIntMultiple(a.b,b.b);
  return bigRationalReduction(c);
}

bigRational bigRationalMinus(bigRational a,bigRational b){
  bigRational c;
  c.a=bigIntMinus( bigIntMultiple(a.a,b.b),bigIntMultiple(a.b,b.a) );
  c.b=bigIntMultiple(a.b,b.b);
  return bigRationalReduction(c);
}

bigRational bigRationalMultiple(bigRational a,bigRational b){
  bigRational c;
  c.a=bigIntMultiple(a.a,b.a);
  c.b=bigIntMultiple(a.b,b.b);
  return bigRationalReduction(c);
}

bigRational bigRationalDivision(bigRational a,bigRational b){
  bigRational c;
  c.a=bigIntMultiple(a.a,b.b);
  c.b=bigIntMultiple(a.b,b.a);
  return bigRationalReduction(c);
}

bigRational bigRationalTwoDimensionDeterminant(bigRational a,bigRational b,bigRational c,bigRational d){
  return bigRationalMinus(bigRationalMultiple(a,d),bigRationalMultiple(b,c));
}

void printBigRational(bigRational c){
  printBigInt(c.a); putchar('/'); printBigInt(c.b);
}




bigRational dp[60][60];

int main(){
  int i,j,k,l,m,n;
  int a[60], b[60]; bigRational A[60], B[60], nx, com;
  int size;

  scanf("%d",&size);
  while(size--){
    fprintf(stderr,"rest %d\n",size);
    scanf("%d%d",&m,&n); m--;
    rep(i,n) scanf("%d%d",a+i,b+i);
    rep(i,n){
      A[i] = twoLlToBigRational(a[i]-1,a[i]);
      B[i] = twoLlToBigRational(b[i]-1,b[i]);
    }
    rep(i,n+1) rep(j,n+1) dp[i][j] = bigRationalZero();
    dp[0][0] = llToBigRational(1);

    rep(i,n) rep(j,n){
      nx = bigRationalMultiple(dp[i][j], A[i]);
      com = bigRationalMinus(nx, dp[i+1][j+1]);
      if( bigIntSign(com.a)==1 ) dp[i+1][j+1] = nx;
      
      nx = bigRationalMultiple(dp[i][j], B[i]);
      com = bigRationalMinus(nx, dp[i+1][j]);
      if( bigIntSign(com.a)==1 ) dp[i+1][j] = nx;
    }

    printBigRational(dp[n][m]); puts("");
  }

  return 0;
}

コメント

コメントの投稿

コメントの投稿
管理者にだけ表示を許可する

トラックバック

トラックバック URL
http://rsujskf.blog32.fc2.com/tb.php/1830-b35335a7
この記事にトラックバックする(FC2ブログユーザー)

Appendix

Recent Articles

ブログ内検索

Ads


(プライバシーポリシー)
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。