Hatena::Grouptopcoder

tsubosakaの日記

2009-01-19

SRM 320 Div1 Easy

| 18:59

"12!"とか"3!!"などの形式で数字が二つ与えられるので大小比較しろという問題.

!の数が等しければ係数の比較をすればよく,そうでないときは!の数が大きい方を一段階計算して再帰的に比較する.ただ"10000!"とかを計算すると途中でオーバフローするので他方の係数よりも大きくなったときに打ち切る必要があるのと,"0!!"の扱いに気をつける必要がある.

public class ExtraordinarilyLarge {
  class Fact{
    long val;
    long fnum;
    Fact(String str){
      int i = str.indexOf('!');
      if(i < 0){
        val = Long.parseLong(str);
        fnum = 0;
      }else{
        val = Long.parseLong(str.substring(0, i));
        fnum = str.length() - i;
      }
      if(val == 0 && fnum > 0){
        val = 1;
        fnum = 0;
      }
    }
    Fact(long v  , long n){
      val = v; fnum = n;
    }
    long cmp(Fact f){
      if(fnum == f.fnum)
        return val - f.val;     
      if(fnum < f.fnum)return - f.cmp(this);
      long r = 1;
      for(long v = 1 ; v <= val ; v++){
        r *= v;
        if(r > f.val)return 1;
      }
      Fact a = new Fact(r , fnum - 1);
      return a.cmp(f);
    }
  }
  public String compare(String x, String y) {
    Fact fx = new Fact(x);
    Fact fy = new Fact(y);
    long c = fx.cmp(fy);
    System.err.println(c);
    if(c > 0)return "x>y";
    else if(c < 0)return "x<y";    
    return "x=y";
  }
}

WilliamTagWilliamTag2019/02/02 11:26cialis over night
medicus group buy cialis
<a href=https://greatwinesgrandhouses.com>Cheap cialis buy online</a>
does cialis get you hard
cheapest cialis 20mg
https://kellyannehulme.com
buy cialis montreal
generic cialis in 2.5mg daily tablets
<a href=https://kellyannehulme.com>20 mg cialis</a>
lilly cialis without prescription
cialis brand name usa price
https://greatwinesgrandhouses.com

WilliamTagWilliamTag2019/02/02 11:26cialis ads
buying cialis in canada online
<a href=https://greatwinesgrandhouses.com>buy Cialis 20 mg</a>
liquid cialis does it work
cialis and avodart
https://kellyannehulme.com
what does cialis cost in mexico
cialis venezuela
<a href=https://greatwinesgrandhouses.com>Cheap cialis</a>
the cheapest cialis online
headache after cialis
https://greatwinesgrandhouses.com

ArnoldNizArnoldNiz2019/02/05 20:50canada drug generic cialis
buy generic cialis from canada
<a href="http://xcialisxx.com">Cheap Cialis</a>
snort cialis
cialis online con ricetta
<a href="http://cialistlm.com">Cheap Cialis buy</a>
brand cialis online mastercard
quick ship cialis
<a href="http://xcialisxx.com">Cheap Cialis</a>