Fox and Number Game
Fox Ciel is playing a game with numbers now.
Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible.
Please help Ciel to find this minimal sum.
Input
The first line contains an integer n (2 ≤ n ≤ 100). Then the second line contains nintegers: x1, x2, ..., xn (1 ≤ xi ≤ 100).
Output
Output a single integer — the required minimal sum.
Examples
2
1 2
2
3
2 4 6
6
2
12 18
12
5
45 12 27 30 18
15
Note
In the first example the optimal way is to do the assignment: x2 = x2 - x1.
In the second example the optimal sequence of operations is: x3 = x3 - x2, x2 = x2 -x1.
题目意思:给你n个数,任意两个数直接,大数可以减去小数得到新的值再赋值给大数,如此反复,直到不出现大数和小数,即所有的数都相等。
解题思路:我看了看数据量很小,于是可以选择使用模拟的方法,将过程模拟了一下,其实能够发现这道题存在着规律,最后所有的数都会变成所有数的最大公约数。
上代码:
模拟法:
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,i,j,ans;
int a[];
scanf("%d",&n);
for(i=; i<n; i++)
{
scanf("%d",&a[i]);
}
while()
{
sort(a,a+n);
if(a[n-]==a[])///互相减,直到都相等
{
break;
}
for(j=n-; j>; j--)
{
if(a[j]!=a[j-])
{
a[j]=a[j]-a[j-];
}
else
{
continue;
}
}
}
ans=a[]*n;
printf("%d",ans);
return ;
}
找到规律,使用GCD:
#include<stdio.h>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
int r;
while(b>)
{
r=a%b;
a=b;
b=r;
}
return a;
}
int main()
{
int i,k,n,ans;
int a[];
scanf("%d",&n);
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
}
k=a[];
for(i=;i<n;i++)
{
ans=gcd(k,a[i]);
k=ans;
}
printf("%d\n",ans*n);
return ;
}
Fox and Number Game的更多相关文章
- Codeforces Round #228 (Div. 2) A. Fox and Number Game
#include <iostream> #include <algorithm> #include <vector> #include <numeric> ...
- Codeforces 389A (最大公约数)
Fox and Number Game Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u ...
- Codeforces Round #228 (Div. 2)
做codeforces以来题目最水的一次 A题: Fox and Number Game 题意:就是用一堆数字来回减,直到减到最小值为止,再把所有最小值加,求这个值 sol: 简单数论题目,直接求所有 ...
- CF 371B Fox Dividing Cheese[数论]
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #218 (Div. 2) B. Fox Dividing Cheese
B. Fox Dividing Cheese time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Codeforces Round #290 (Div. 2) D. Fox And Jumping dp
D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...
- Codeforces Round #290 (Div. 2) C. Fox And Names dfs
C. Fox And Names 题目连接: http://codeforces.com/contest/510/problem/C Description Fox Ciel is going to ...
- Codeforces Round #290 (Div. 2) B. Fox And Two Dots dfs
B. Fox And Two Dots 题目连接: http://codeforces.com/contest/510/problem/B Description Fox Ciel is playin ...
- Codeforces Round #290 (Div. 2) A. Fox And Snake 水题
A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...
随机推荐
- MySQL(mariadb)多实例应用与多实例主从复制
MySQL多实例 mysql多实例,简单理解就是在一台服务器上,mysql服务开启多个不同的端口(如3306.3307,3308),运行多个服务进程.这些 mysql 服务进程通过不同的 socket ...
- MYSQL 8.0.11 安装过程及 Navicat 链接时遇到的问题
参考博客:https://blog.csdn.net/WinstonLau/article/details/78666423 我的系统和软件版本是这样的: 系统环境:win7.64位 MySQL版本: ...
- Ubuntu 16.04LTS 更新清华源
1 备份原来的更新源 cp /etc/apt/sources.list /etc/apt/sources.list.backup 如果提示权限不够就输入下面两行,先进入到超级用户,再备份 sudo - ...
- 4 二维数组中的查找 JavaScript
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- Hive的内置函数
定义: UDF(User-Defined-Function),用户自定义函数对数据进行处理. UDTF(User-Defined Table-Generating Functions) 用来解决 输入 ...
- centos7 杂记
yum 源 https://www.cnblogs.com/renpingsheng/p/7845096.html 安装nginx php mysql https://www.cnblogs.com/ ...
- Go 入门 - 控制流
主要内容来自中文版的官方教程Go语言之旅 目的为总结要点 循环 Go 只有 for循环 for 由三部分组成,用分号间隔开 初始化语句:在第一次迭代之前执行,通常为一句短变量声明(i:=0) 条件表达 ...
- linux 查看内置命令
使用: man shell builtins 查找结果如下:
- [Real World Haskell翻译]第20章 Haskell系统编程
第20章 Haskell系统编程 到目前为止,我们已经讨论了大多数的高层次的概念.Haskell也可以用于较低级别的系统编程.很可能是用haskell编写出底层的与操作系统接口的程序. 在本章中,我们 ...
- MapWinGIS使用
.net语言中使用MapWinGIS.ocx http://www.cnblogs.com/kekec/archive/2011/03/30/1999709.html 基于MapWinGis的开发探索 ...