Description
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 xide-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 nintegers separated by spaces. The ith integer 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
题意:输入一堆数,求这堆数的最小公倍数。
思路: 若 p= a1^b1 * a2^ b2 * ... * an^bn , q= a1^c1 * a2^ c2 * ... * an^cn
则 p和q的最小公倍数 lcm(p,q) = p= a1^max(b1,c1) * a2^ max(b2,c2) * ... * an^max(bn,cn);
根据上述思路,将每个数因式分解,然后记录各个数的因子,因子数保存最大值。最后将所有因子乘起来就是最小公倍数 ,由于结果很大,所以用到了高精度。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=10010;
int num[maxn],ans[maxn],m,n,T,t;
int pri[30]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101};
void initial()
{
memset(num,0,sizeof(num));
memset(ans,0,sizeof(ans));
ans[0]=m=1;
}
void mul(int num)
{
int tmp=0,now;
for(int i=0;i<m;i++) ans[i]*=num;
for(int i=0;i<m;i++)
{
now=ans[i]+tmp;
tmp=now/10;
ans[i]=now%10;
}
while(tmp)
{
ans[m]=tmp%10;
tmp/=10;
m++;
}
}
int main()
{
scanf("%d",&T);
for(int co=1;co<=T;co++)
{
initial();
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&t);
int w=t,cnt=0;
for(int j=0;pri[j]*pri[j]<=t && j<25;j++)
{
while(w%pri[j]==0)
{
cnt++;
w=w/pri[j];
}
if(cnt)
{
num[pri[j]]=max(cnt, num[pri[j]]);
cnt=0;
}
}
if(w>1) num[w]=max(num[w],1);
}
for(int i=2;i<maxn;i++)
{
if(num[i]==0) continue;
int pt=(int)pow(i,num[i]);
mul(pt);
}
printf("Case %d: ",co);
for(int i=m-1;i>=0;i--) printf("%d",ans[i]);
printf("\n");
}
return 0;
}
尝试用java也写了下,不过MLE了,第一次写java大数就这样,哎。。。
import java.io.BufferedInputStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigInteger ans = null;
int nCase,n,w;
Scanner cin = new Scanner(System.in);
nCase = cin.nextInt();
for(int co=1;co<=nCase;co++)
{
n = cin.nextInt();
for(int i=0;i<n;i++) {
w = cin.nextInt();
if(i==0) ans=BigInteger.valueOf(w);
else ans = ans.multiply(BigInteger.valueOf(w)).divide(ans.gcd(BigInteger.valueOf(w)));
}
System.out.println("Case "+co+": "+ans);
ans = null;
}
}
}