1. 程式人生 > >(轉)POJ3101 Astronomy【素因子分解】【大數乘法】

(轉)POJ3101 Astronomy【素因子分解】【大數乘法】

Astronomy
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6107 Accepted: 1382

Description

There are n planets in the planetary system of star X. They orbit star X in circular orbits located in the same plane. Their tangent velocities are constant. Directions of orbiting of all planets are the same.

Sometimes the event happens in this planetary system which is called planet parade. It is the moment when all planets and star X are located on the same straight line.

Your task is to find the length of the time interval between two consecutive planet parades.

Input

The first line of the input file contains n

 — the number of planets (2 ≤ n ≤ 1 000).

Second line contains n integer numbers ti — the orbiting periods of planets (1 ≤ ti ≤ 10 000). Not all of ti are the same.

Output

Output the answer as a common irreducible fraction, separate numerator and denominator by a space.

Sample Input

3
6 2 3

Sample Output

3 1

Hint

Source


題目大意:

有 N 個行星繞著中心天體飛行,給你每個行星飛行的週期,問:最少執行多少時間

能讓所有的行星在同一條直線上。結果用分數表示。輸出該分數的分子和分母。

解題思路:

選擇第一個行星為參考系,其週期為 T0,則其他行星的週期為 Ti,則其他行星的相

對角速度為 Vi = (T0-Ti) * 2π / (T0*Ti)。繞過半個圓周的時間 ti = π / Vi = 

(T0*Ti) / ((T0-Ti)*2)。

那麼問題就變為了求所有 ti 的分子的最小公倍數 和 分子的最大公約數。


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 1010;

int GCD(int a,int b)
{
    if(b == 0)
        return a;
    return GCD(b,a%b);
}

int A[MAXN],B[MAXN],C[MAXN*10];

int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        for(int i = 0; i < N; ++i)
            scanf("%d",&A[i]);
        memset(B,0,sizeof(B));
        memset(C,0,sizeof(C));
        B[0] = 1;

        int a,b,gcd,fm = 0,k;
        for(int i = 1; i < N; ++i)
        {
            if(A[i] != A[0])
            {
                b = A[i]*A[0];
                a = abs(A[i]-A[0])*2;
                gcd = GCD(a,b);
                a /= gcd;
                b /= gcd;
                fm = GCD(a,fm);

                for(int j = 2; b > 1; ++j)
                {
                    if(b % j == 0)
                    {
                        k = 0;
                        while(b % j == 0)
                        {
                            b /= j;
                            k++;
                        }
                        if(k > C[j])    //C[] 陣列記錄素因子的冪 對應的最大值
                            C[j] = k;
                    }
                }
            }
        }

        int tmp;
        for(int i = 0; i < MAXN*10; ++i)
        {
            for(int j = 0; j < C[i]; ++j)
            {
                tmp = 0;
                for(int k = 0; k < MAXN; ++k)
                {
                    B[k] = B[k]*i + tmp;
                    tmp = B[k] / 10000;
                    B[k] %= 10000;
                }
            }
        }

        int i = 999;
        while(i > 0 && B[i] == 0)
            i--;

        printf("%d",B[i]);
        for(--i; i >= 0; --i)
            printf("%04d",B[i]);
        printf(" %d\n",fm);
    }

    return 0;
}