当前位置: 代码迷 >> 综合 >> Contest3190 - 2021级新生个人训练赛第43场_G: Multiple Clocks
  详细解决方案

Contest3190 - 2021级新生个人训练赛第43场_G: Multiple Clocks

热度:95   发布时间:2023-12-06 05:29:20.0

//
问题 G: Multiple Clocks
时间限制: 1.000 Sec  内存限制: 128 MB题目描述
We have N clocks. The hand of the i-th clock (1≤i≤N) rotates through 360° in exactly Ti seconds.
Initially, the hand of every clock stands still, pointing directly upward.
Now, Dolphin starts all the clocks simultaneously.
In how many seconds will the hand of every clock point directly upward again?Constraints
1≤N≤100
1≤Ti≤10e18
All input values are integers.
The correct answer is at most 10e18 seconds.
输入
Input is given from Standard Input in the following format:
N
T1
:  
TN
输出
Print the number of seconds after which the hand of every clock point directly upward again.
样例输入 Copy
2
2
3
样例输出 Copy
6
提示
We have two clocks. The time when the hand of each clock points upward is as follows:
·Clock 1: 2, 4, 6, … seconds after the beginning
·Clock 2: 3, 6, 9, … seconds after the beginning
Therefore, it takes 6 seconds until the hands of both clocks point directly upward.

最大公约数_Stein 算法

// G
#include<bits/stdc++.h>
using namespace std;// 1≤N≤100
// 1≤Ti≤10e18
typedef unsigned long long ULL;
const int N=111;
const ULL Ti=1e19;
ULL x[N];ULL gcd( ULL a,ULL b )
{if( a==0 ) return b;if( b==0 ) return a;ULL c=0,temp;while( (a&1)==0 && (b&1)==0 ) a>>=1,b>>=1,c++;while( (a&1)==0 ) a>>=1;while( (b&1)==0 ) b>>=1;if( a<b ) temp=a,a=b,b=temp;while( a=(a-b)>>1 )        //{while( (a&1)==0 ) a>>=1;if( a<b ) temp=a,a=b,b=temp;}return b<<c;
}int main()
{ULL n,i,temp,ans;while( ~scanf("%llu",&n) ){for( i=0;i<n;i++ ) scanf("%llu",&x[i]);ans=x[0];for( i=1;i<n;i++ ){temp=gcd( ans,x[i] );ans=ans/temp*x[i];}printf("%llu\n",ans);}return 0;
}

  相关解决方案