(light oj 1024) Eid (最小公倍数)
题目链接: http://lightoj.com/volume_showproblem.php?problem=1024
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 (≤ ), denoting the number of test cases. Each case of input will contain an integer n ( ≤ n ≤ ) in a single line. The next line will contain n integers separated by spaces. The ith integer of this line will denote the eating period for the ith race. These integers will be between and . 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 Output for Sample Input
Case :
Case :
题目大意:给出n个数 求n个数的最小公倍数;
思路:求最小公倍数可以用最大公约数来求,但是由于n的大小与每个数的大小,所以最后说求得最小公倍数可能是个大数,所以需要用到大数相乘;
利用每个数的因子求最小公倍数。例如:
5 的因子 5;
6的因子 2 3;
30的因子 2 3 5;
60的因子 2 2 3 5;
保存出现的每个因子最大个数 2个2 1个3 1个5 相乘就得到最小公倍数 60;
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include <map>
#include <string>
#include <vector>
#include<iostream>
using namespace std;
#define N 100006
#define INF 0x3f3f3f3f
#define LL long long
#define mod 1000000007
int prim[N];
int arr[N],ans[N];
int k =;
void Init()///先打个素数表
{
memset(arr,,sizeof(arr));
for(int i=;i<=;i++)
{
if(!arr[i])
{
prim[k++] = i;
for(int j=i;j<=;j+=i)
arr[j] = ;
}
}
}
void mul(int n)///大数相乘
{
for(int i=;i<;i++)
ans [i] = ans[i]*n;
for(int i=;i<;i++)
{
ans[i+] +=ans[i]/;
ans[i] = ans[i]%;
}
}
void prin()///大数输出
{
int i = ;
while(i>= && !ans[i]) i--;
printf("%d",ans[i--]);
while(i>=)
printf("%04d",ans[i--]);
printf("\n");
}
int main()
{
int T;
int con=;
Init();
scanf("%d",&T);
while(T--)
{
int n,num;
scanf("%d",&n);
memset(arr,,sizeof(arr));
for(int i=;i<n;i++)
{
scanf("%d",&num);
for(int j=;j<k&&prim[j]*prim[j]<=num;j++)
{
int s = ;
while(num%prim[j]==)///统计每个数每个因子的个数
{
s++;
num = num/prim[j];
}
arr[prim[j]] = max(s,arr[prim[j]]);///保存当前因子最多的个数
}
if(num>)
arr[num] = max(arr[num],);///这个数为素数
}
memset(ans,,sizeof(ans));
ans[] = ;
for(int i=;i<;i++)
{
if(!arr[i]) continue;
int sum = ;
for(int j=;j<arr[i];j++)
sum = sum*i;
mul(sum);///每个因子相乘
}
printf("Case %d: ",con++);
prin();
}
return ;
}
(light oj 1024) Eid (最小公倍数)的更多相关文章
- Light OJ 1114 Easily Readable 字典树
题目来源:Light OJ 1114 Easily Readable 题意:求一个句子有多少种组成方案 仅仅要满足每一个单词的首尾字符一样 中间顺序能够变化 思路:每一个单词除了首尾 中间的字符排序 ...
- Light OJ 1429 Assassin`s Creed (II) BFS+缩点+最小路径覆盖
题目来源:Light OJ 1429 Assassin`s Creed (II) 题意:最少几个人走全然图 能够反复走 有向图 思路:假设是DAG图而且每一个点不能反复走 那么就是裸的最小路径覆盖 如 ...
- Light OJ 1406 Assassin`s Creed 减少国家DP+支撑点甚至通缩+最小路径覆盖
标题来源:problem=1406">Light OJ 1406 Assassin`s Creed 意甲冠军:向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路: ...
- Light OJ 1316 A Wedding Party 最短路+状态压缩DP
题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...
- light oj 1007 Mathematically Hard (欧拉函数)
题目地址:light oj 1007 第一发欧拉函数. 欧拉函数重要性质: 设a为N的质因数.若(N % a == 0 && (N / a) % a == 0) 则有E(N)=E(N ...
- Light OJ 1406 Assassin`s Creed 状态压缩DP+强连通缩点+最小路径覆盖
题目来源:Light OJ 1406 Assassin`s Creed 题意:有向图 派出最少的人经过全部的城市 而且每一个人不能走别人走过的地方 思路:最少的的人能够走全然图 明显是最小路径覆盖问题 ...
- Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...
- Jan's light oj 01--二分搜索篇
碰到的一般题型:1.准确值二分查找,或者三分查找(类似二次函数的模型). 2.与计算几何相结合答案精度要求比较高的二分查找,有时与圆有关系时需要用到反三角函数利用 角度解题. 3.不好直接求解的一类计 ...
- Light OJ 1272 Maximum Subset Sum 高斯消元 最大XOR值
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u011686226/article/details/32337735 题目来源:problem=12 ...
随机推荐
- 升级node版本
一.升级方法: 1.产看node版本,没安装的请先安装: $ node -v 2.清楚node缓存: $ sudo npm cache clean -f 3.安装node版本管理工具'n'; $ su ...
- WebUtils【MD5加密(基于MessageDigest)】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于MD5加密,主要场景是在调用登录接口时对密码进行MD5加密处理. 效果图 暂不需要 代码分析 基于Java.security.M ...
- 尴尬的事情又发生Newtonsoft.Json vs Protobuf.net
写程序做下性能测试都是例行的事情了,一般在普通电脑上测试一下如果比较理想那基本不出什么意外!但世事难料,代码写得不好经常担心CPU不够用,其实写得好但不能完全发挥出CPU资源的优势更是一件悲剧的事情! ...
- docker~yml里使用现有网络
回到目录 我们在进行docker swarm部署高可用集群时,在yml文件里可能要配置一些服务,而这些服务可能要使用一些公用的数据库,这些数据库可能已经运行在某个容器里,而这些容器有自己的网络,doc ...
- [翻译] Linux 内核中的位数组和位操作
目录 Linux 内核里的数据结构 原文链接与说明 Linux 内核中的位数组和位操作 位数组声明 体系结构特定的位操作 通用位操作 链接 Linux 内核里的数据结构 原文链接与说明 https:/ ...
- 【Vuex】vuex基本介绍与使用
Vuex是什么? 官方解释: Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化.Vuex 也集 ...
- Error fetching https://gems.ruby-china.org/: bad response Not Found 404 (https://gems.ruby-china.org/specs.4.8.gz) 报错解决办法
执行换源操作 gem source -a https://gems.ruby-china.org/ 时报错: Error fetching https://gems.ruby-china.org/: ...
- Head First设计模式读书笔记
阅读指南: 精读一章内容,手工输入一章代码(注1),与书中描述的思想进行印证,实在搞不懂就放过吧.设计模式绝对不会一次就看懂的. 这本书对于理解设计模式很有帮助,就是例子不太符合中国人的思维模式,但是 ...
- http header Content-Type之常用三种
Content-Type 用于指示资源的MIME类型 在响应头中,告诉客户端实际返回内容的类型 在请求头中,告诉服务器实际发送的数据类型 句法: Content-Type: text/html; ch ...
- Where Can I Download Full Installers for WebLogic Server
Where can I download full installers for the different versions of WebLogic Server (WLS)? Full insta ...