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 ...
随机推荐
- [NOIP2007] 提高组 洛谷P1098 字符串的展开
题目描述 在初赛普及组的“阅读程序写结果”的问题中,我们曾给出一个字符串展开的例子:如果在输入的字符串中,含有类似于“d-h”或者“4-8”的字串,我们就把它当作一种简写,输出时,用连续递增的字母获数 ...
- Codeforces704C. Black Widow
n<=1e5个值v,分别由<=1e5的m个变量中的1<=ki<=2个布尔变量xj(或某个变量取反)或起来组成,而所有的v异或起来为1,一个x不会在输入数据中出现超过2次,包括他 ...
- hdu_1398_Square Coins_201404260953
Square Coins Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- Ubuntu 16.04 GNOME在桌面左侧添加启动器(Launcher)
安装Dash to Dock: git clone https://github.com/micheleg/dash-to-dock.git cd dash-to-dock/ make make in ...
- JFileChooser 中文API
javax.swing类 JFileChooser java.lang.Object java.awt.Component java.awt.Container javax.swing.JCompon ...
- JDBC基本应用
首先我们来看一下JDBC操作数据的核心: Connection 应用程序与数据库之间的桥梁 数据库驱动程序是构建桥梁的基石和材料 DriverManager类是基石和材料的管理员 Statement ...
- Linux bash: scp: command not found的问题记录
,总结 scp成功,须要两个server都安装了scp服务才行.
- iOS动画案例(1) 仿qq账号信息里的一个动画
受人所托,模仿qq账号信息里的一个动画,感觉挺有意思,也没感觉有多难,就开始做了,结果才发现学的数学知识都还给体育老师了,研究了大半天才做出来. 先看一下动画效果: 用到的知识点: ( ...
- c#控件重绘的问题
1.当Panel有背景图像的时候,往Panel添加控件(带图像),画面会非常闪烁,所以,Panel尽量不要带背景图像 2.带背景图像可以参考designer.cs里面的写法... 添加Control之 ...
- SpringMVC之请求参数的获取方式
转载出处:https://www.toutiao.com/i6510822190219264516/ SpringMVC之请求参数的获取方式 常见的一个web服务,如何获取请求参数? 一般最常见的请求 ...