zoukankan      html  css  js  c++  java
  • 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代码:
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 int gcd(int a,int b){
     5     return b?gcd(b,a%b):a;
     6 }
     7 int main(){
     8     int t,n,x,ans;LL sum;
     9     scanf("%d",&t);
    10     while(t--){
    11         scanf("%d",&n);
    12         scanf("%d",&x);
    13         ans=sum=x;
    14         for(int i=1;i<n;++i){
    15             scanf("%d",&x);
    16             sum+=x;
    17             ans=gcd(ans,x);
    18         }
    19         printf("%lld %d
    ",sum,ans);
    20     }
    21     return 0;
    22 }
  • 相关阅读:
    centos出现“FirewallD is not running”怎么办
    Centos7开放及查看端口
    phpRedis函数使用总结
    如何在Windows的PHPstudy中使用redis
    Redis命令操作详解
    不可不知 DDoS的攻击原理与防御方法
    DDoS 攻击与防御:从原理到实践
    ORM Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
    PHP常用函数大全500+
    Linux彻底卸载Nginx
  • 原文地址:https://www.cnblogs.com/acgoto/p/9315857.html
Copyright © 2011-2022 走看看