您的位置:首页 > 其它

(转)POJ3101 Astronomy【素因子分解】【大数乘法】

2017-08-11 17:07 316 查看
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

Northeastern Europe 2005, Northern Subregion

题目大意:

有 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  拓展欧几里得