【POJ 3101】Astronomy(GCD+LCM+公式)
来源:互联网 发布:吉利知豆d2多少钱 编辑:程序博客网 时间:2024/06/14 08:09
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
给出n个星球。
以及他们围绕太阳运转周期ti。
问最少经过多久他们以及太阳能共线
用分数形式表示。
首先共线情况下,一定要么差几周,或者几周半。
以星球i为标准。
假设j星球与i差半周。
那么就有
这样可以求出所有星球跟星球1差半周需要的时间
那么求出
分数形式求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;}
- 【POJ 3101】Astronomy(GCD+LCM+公式)
- POJ 3101 Astronomy (大数+LCM)
- POj-3101-Astronomy(分数GCD+BigInteger)
- poj 3101 Astronomy 分数lcm+大数java
- POJ 3101 Astronomy (数学)
- poj 3101 Astronomy (大数java)
- POJ - 3101 Astronomy(有理数的公倍数)
- poj 3101 astronomy
- POJ 3101 Astronomy
- POJ 3101 Astronomy
- POJ 3101 Astronomy
- poj 3101 Astronomy
- POJ 3101 Astronomy
- POJ 3101 Astronomy .
- POJ 3101 Astronomy 可能会
- POJ 3101 Astronomy 笔记
- GCD & LCM Inverse POJ
- GCD & LCM Inverse POJ
- iOS 获取启动图
- [LeetCode] 83. Remove Duplicates from Sorted List
- SPOJ Two Paths(树形dp,最大不相交路径长度乘积)
- JSON.parse与JSON.stringify
- offsetwidth/offsetheight的区别
- 【POJ 3101】Astronomy(GCD+LCM+公式)
- 个人对单例设计模式理解
- AppStore上架应用被拒原因以及解决方式
- Spring事务管理学习笔记
- 百度之星题目
- 如何禁止双击标题栏不改变窗口的大小和拖动标题栏窗口不移动的问题
- MySQL存储过程中的3种循环
- poj1364 King
- Spring之单元测试