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<stdio.h>
#include<string.h>
#include<algorithm>
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<n;i++)
        {
            scanf("%d",&x);
            int d=2;
            while(d<=x)   //使 x 拆分为若干个素因子
             {
               int count=0;
               while(prime[d]&&x%d==0)
               {
                   count++;
                   x/=d;
               }
               if(count)
               {
                   m[d]=max(m[d],count);  //记录该素数的最大值
                   count=0;
               }
               d++;
            }
        }
        ans[0]=1;
        for(int i=2;i<=10000;i++)
        {
            if(m[i])
            {
                int sum=1;
                while(m[i]--)
                    sum*=i;
                mul(ans,sum);    //高精度乘法
            }
        }
        printf("Case %d: ",K++);
        print();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值