(转)POJ3101 Astronomy【素因子分解】【大数乘法】
2017-08-11 17:07
316 查看
Astronomy
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
Sample Output
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;
}
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6107 | Accepted: 1382 |
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;
}
相关文章推荐
- POJ3101 Astronomy【素因子分解】【大数乘法】
- 【世界数学难题】素数判定与大数因子分解问题(下)
- GCD & LCM Inverse POJ - 2429 Pollard_rho大数因子分解
- POJ3101 Astronomy——高精度+分解质因子法求GCD和LCM——Pku3101
- poj2429 GCD & LCM Inverse 数论 大数分解以及找所有因子
- POJ 2429 GCD & LCM Inverse Pollard_rho大数因子分解
- HDU1452-积性函数-素因子分解-乘法逆元
- POJ 3101 Astronomy 解题报告(大数乘法+分数最小公倍数)
- poj 1811 Prime Test 大数素数测试+大数因子分解
- poj 1845(快速幂+二分计算等比数列和+大数因子分解+因子和计算+模除溢出)
- 【世界数学难题】素数判定与大数因子分解问题(上)
- 大数加法和大数乘法
- 大数加法、大数乘法、大数减法。Swift。
- 大数乘法
- 【POJ1811】Prime Test-Miller-Rabin素数测试+Pollard-rho大数分解
- 大数乘法 分治法
- UVa 10392 Factoring Large Numbers (素因子分解)
- bLue 的乘法算术题--Java大数
- 哈理工OJ 1159 MAGI System(大数乘法)
- 【题目24】大数乘法