E - Cheap Kangaroo(求多个数的最大公约数)
Description
There are N kangaroos going out to eat at an Indian restaurant. The ith kangaroo wants to eat exactly xi food. The kangaroos all want to order the same size of plates, but each one can order more than one plate for themselves if they need to. If the kangaroo orders more than he needs, he can simply hide the leftovers in his pouch.
At this Indian restaurant, the cost of the plate is the same as its size. Since Karl the Kangaroo is paying and is low on money, he wants to know what is the minimum cost to feed all N kangaroos and what is the largest possible size of the plates that satisfies this minimum cost?
Input
The first line of input is T – the number of test cases.
The first line of each test case is an integer N (1 ≤ N ≤ 105).
The second line contains N space-separated integers xi (1 ≤ xi ≤ 109).
Output
For each test case, output a line containing two space-separated integers – the minimum cost and the maximum plate size that corresponds to when the total cost is minimized.
Input
2
1
5
2
4 2
Output
5 5
6 2
解题思路:题目的意思就是求n只袋鼠需要的食物总量和统一订购最大的盘子即为所有袋鼠食量的最大公约数!
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int main(){
int t,n,x,ans;LL sum;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%d",&x);
ans=sum=x;
for(int i=;i<n;++i){
scanf("%d",&x);
sum+=x;
ans=gcd(ans,x);
}
printf("%lld %d\n",sum,ans);
}
return ;
}
E - Cheap Kangaroo(求多个数的最大公约数)的更多相关文章
- C语言辗转相除法求2个数的最小公约数
辗转相除法最大的用途就是用来求两个数的最大公约数. 用(a,b)来表示a和b的最大公约数. 有定理: 已知a,b,c为正整数,若a除以b余c,则(a,b)=(b,c). (证明过程请参考其它资料) 例 ...
- 求N个数的最大公约数和最小公倍数(转)
除了分解质因数,还有另一种适用于求几个较小数的最大公约数.最小公倍数的方法 下面是数学证明及算法实现 令[a1,a2,..,an] 表示a1,a2,..,an的最小公倍数,(a1,a2,..,an)表 ...
- c语言实践:求两个数的最大公约数
我的思路是这样的:比如12和16这两个数.先理解一下概念,什么叫最大公约数.就是12有很多个因数,16也有很多个因数,这两堆因数中有一些重合的因数,在这些重合的因数中找到那个最大的.那么最大公约数一定 ...
- python 函数求两个数的最大公约数和最小公倍数
1. 求最小公倍数的算法: 最小公倍数 = 两个整数的乘积 / 最大公约数 所以我们首先要求出两个整数的最大公约数, 求两个数的最大公约数思路如下: 2. 求最大公约数算法: 1. 整数A对整数 ...
- C++扬帆远航——14(求两个数的最大公约数)
/* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:gongyueshu.cpp * 作者:常轩 * 微信公众号:W ...
- 求两个数的最大公约数&求N个数的最大公约数
一.求两个数的最大公约数 如何编程计算N个数的最大公约数(Greatest common divisor)呢?第一想法那便是两两计算,但是往往最简单的想法是不怎么靠谱的.下面用递归来解决.递归有一大好 ...
- LightOj 1024 - Eid (求n个数的最小公约数+高精度)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1024 题意:给你n(2<=n<=1000)个数, 然后求n个数的最小公倍数 ...
- C++中用辗转相除法求两个数的最大公约数和最小公倍数
两个数的最大公约数:不能大于两个数中的最小值,算法口诀:小的给大的,余数给小的,整除返回小的,即最大公约数,(res=max%min)==0? max=min,min=res return min; ...
- 求两个数的最大公约数(Euclid算法)
求两个数 p 和 q 的最大公约数(greatest common divisor,gcd),利用性质 如果 p > q, p 和 q 的最大公约数 = q 和 (p % q)的最大公约数. 证 ...
- Java数据结构与算法之---求两个数的最大公约数(欧几里得算法)
一个简单的小算法来获取两个数的最大公约数, public class Test { public static void main(String[] args) { long result = gcd ...
随机推荐
- 【BZOJ2142】礼物(扩展lucas定理,中国剩余定理合并方程)
题意:有n件礼物,m个人,每个人分别需要w[i]件礼物,求分礼物的不同方案数 mod P 提示:设P=p1^c1 * p2^c2 * p3^c3 * … *pt ^ ct,pi为质数. 1≤n≤10^ ...
- 关于变量__name__的理解
__name__ 1. 基本含义 如果是放在Modules模块中,就表示是模块的名字: 如果是放在Classs类中,就表示类的名字: 2. 模块中的意义 这里重点说一下模块中的意义,这个用法在pyth ...
- 调整JVM内存大小
首次运行公司项目,出现了内存溢出,具体出现java.lang.OutOfMemoryError: PermGen space和java.lang.OutOfMemoryError:GC overhea ...
- 前端自动化测试工具--使用karma进行javascript单元测试(转)
Karma+Jasmine+PhantomJS组合的前端javascript单元测试工具. 1.介绍 Karma是由Google团队开发的一套前端测试运行框架,karma会启动一个web服务器,将js ...
- Linux系统备份还原工具1(DD)(应用实例)
DD使用教程:http://www.cnblogs.com/EasonJim/p/7442223.html 以下实例没经过大量测试,可能在一些机器上不会有效. 一般围绕以下几点进行设置: 1.dd完后 ...
- java压缩与解压文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- redhat and centos 系统经常使用命令
查看cpu信息: more /proc/cpuinfo |grep "model name" 通常是至强系统 查看内存信息: grep MemTotal / ...
- Application特征
1.位置:服务器内存,执行速度快2.使用范围:整个应用程序3.类型:任意类型4.声明周期:应用程序开始创建到销毁
- 【Android归纳】回调机制在Android中的应用与实战
回调这样的思想在程序中是比較普遍的.有时候可能我们并没有注意到.近期整理了一些对于回调的理解,分享给大家 先上概念...... 什么是回调函数? 回调函数就是一个通过函数指针调用的函数. 假设你把函数 ...
- linux驱动之中断方式获取键值
linux驱动之中断方式获取键值 ----------------------------------------------------------------------------------- ...