2013年5月23日 星期四

UVa -- GCD 之夜

UVa 題庫:

 GCD and/or LCM (6/11)
  106 - Fermat vs. PythagorasdiscussLev 2 --- ? ---
  332 - Rational Numbers from Repeating FractionsdiscussLev 3 --- ? ---
  408 - Uniform GeneratordiscussLev 2 ✔ 0.015s/1683
  412 - PidiscussLev 2 --- ? ---
  10407 - Simple division discussLev 3 ✔ 0.022s/1035
  10892 - LCM Cardinality discussLev 4 --- ? ---
  11388 - GCD LCMdiscussLev 3 ✔ 0.015s/1838
  11417 - GCDdiscussLev 2 ✔ 0.332s/2409(1)
  11774 - Doom's DaydiscussLev 5 --- ? ---
  11827 - Maximum GCD discussLev 3 ✔ 0.022s/1029
  12068 - Harmonic MeandiscussLev 5 ✔ 0.025s/283(2)

照著練習就可以了

好用的演算法就是輾轉相除法

// Euclidean Algorithm
long long gcd(long long a, long long b)
        while (b)
        {
                long long t = b; 
                b = a % t; 
                a = t; 
        }
        return a;
}



理論上能用 non-recursive 版本的計算方式就用 non-recursive,比較快。

原則上沒太大問題,有問題的就先跳過,改天有時間再慢慢做。

沒有留言:

張貼留言