【POJ 3101】Astronomy(GCD+LCM+公式)

来源:互联网 发布:吉利知豆d2多少钱 编辑:程序博客网 时间:2024/06/14 08:09

【POJ 3101】Astronomy(GCD+LCM+公式)

Astronomy
Time Limit: 2000MSMemory Limit: 65536KTotal Submissions: 5706Accepted: 1270

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

36 2 3

Sample Output

3 1

Hint

Source

Northeastern Europe 2005, Northern Subregion

给出n个星球。
以及他们围绕太阳运转周期ti。
问最少经过多久他们以及太阳能共线
用分数形式表示。

首先共线情况下,一定要么差几周,或者几周半。
以星球i为标准。
假设j星球与i差半周。
那么就有 Tij=0.5L|LtiLtj|=titj2|titj|
这样可以求出所有星球跟星球1差半周需要的时间T1j
那么求出LCMmj=2{T1j}
分数形式求LCM后 分子是所有分子的GCD 分母是所有分母的LCM
求一下就行了
因为可能会爆LL
用拆块相乘

代码如下:

#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define Pr pair<int,int>#define fread(ch) freopen(ch,"r",stdin)#define fwrite(ch) freopen(ch,"w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int msz = 10000;const int mod = 1e9+7;const double eps = 1e-8;int t[1111];int cnt[11111];int p[1111];int main(){    //fread("");    //fwrite("");    int n;    int q,g;    int a,b;    int mx,tmp;    while(~scanf("%d",&n))    {        memset(cnt,0,sizeof(cnt));        for(int i = 0; i < n; ++i)        {            scanf("%d",&t[i]);            if(!i) continue;            a = abs(t[i]-t[i-1])<<1;            b = t[i]*t[i-1];            g = __gcd(a,b);            a /= g;            b /= g;            if(i == 1)            {                q = a;            }            else q = __gcd(q,a);            for(int j = 2; j*j <= b; ++j)            {                mx = 0;                while(b%j == 0)                {                    b /= j;                    mx++;                }                cnt[j] = max(cnt[j],mx);            }            cnt[b] = max(cnt[b],1);        }        int per = 100000;        memset(p,0,sizeof(p));        p[0] = 1;        int pos = 0;        for(int i = 2; i <= 10000; ++i)        {            while(cnt[i]--)            {//              printf("%d\n",i);                tmp = 0;                for(int j = 0; j <= pos || tmp; ++j)                {                    p[j] = p[j]*i+tmp;                    tmp = p[j]/per;                    p[j] %= per;                    pos = max(pos,j);                }            }        }        printf("%d",p[pos--]);        while(pos >= 0)        {            printf("%05d",p[pos--]);        }        printf(" %d\n",q);    }    return 0;}
0 0
原创粉丝点击