LightOJ 1024 - Eid (高精度乘法求n个数的LCM)

In a strange planet there are n races. They are completely different as well as their food habits. Each race has a food-eating period. That means the ith race eats after every xi de-sec (de-sec is the unit they use for counting time and it is used for both singular and plural). And at that particular de-sec they pass the whole day eating.

The planet declared the de-sec as 'Eid' in which all the races eat together.

Now given the eating period for every race you have to find the number of de-sec between two consecutive Eids.

Input

Input starts with an integer T (≤ 225), denoting the number of test cases.

Each case of input will contain an integer n (2 ≤ n ≤ 1000) in a single line. The next line will contain n integers separated by spaces. The ithinteger of this line will denote the eating period for the ith race. These integers will be between 1 and 10000.

Output

For each case of input you should print a line containing the case number and the number of de-sec between two consecutive Eids. Check the sample input and output for more details. The result can be big. So, use big integer calculations.

Sample Input

2

3

2 20 10

4

5 6 30 60

Sample Output

Case 1: 20

Case 2: 60

题意:给出n个数,求出这n个数的最小公倍数

思路:把每个数都拆分成素因子的乘积,记录每个素因子的最大数量值,然后相乘

例如 : 5 = 5                    一个 5
             25 = 5 * 5            两个 5
             30=2 * 3 * 5         一个 2 , 一个 3 ,一个  5
             60=2 * 2 * 3 * 5    俩个 2 ,一个 3  ,一个  5
   可以看出 2的最大数量值为2 ,3的最大数量数量值为1,5的最大数量值为2,所以lcm=2*2*3*5*5;
   由于 n 的范围为1000,值小于10000,所以求出来的值会非常大,用高精度乘法来做

代码:具体看代码

#include
#include
#include
using namespace std;
typedef unsigned long long ll;
const int INF=0x3f3f3f3f;
int ans[1010];     //存lcm的值 数组中每位存的是四位数
int prime[10010];
int m[10010];
void isprime()      //素数筛
{
    for(int i=0;i<=10000;i++)
        prime[i]=1;
    prime[0]=prime[1]=0;
    for(int i=2;i<=10000;i++)
        if(prime[i])
            for(int j=i+i;j<=i;j+=i)
                prime[j]=0;
}
void mul(int ans[],int v)    //高精度乘法------万进制
{
    for(int i=0;i<=1000;i++)  //模拟乘法
        ans[i]=ans[i]*v;
    for(int i=0;i<=1000;i++)   
    {
        ans[i+1]+=ans[i]/10000;
        ans[i]%=10000;
    }
}
void print()       //输出
{
    int pos=1000;
    while(pos>=0&&ans[pos]==0)   //去除前导零
        pos--;
    printf("%d",ans[pos--]);     
    while(pos>=0)           
        printf("%04d",ans[pos--]);
    printf("\n");
}
int main()
{
    int t,n,x,K=1;
    isprime();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(m,0,sizeof(m));
        memset(ans,0,sizeof(ans));
        for(int i=0;i

 

你可能感兴趣的:(数论)