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 ...
随机推荐
- Jboot使用appassembler-maven-plugin插件生成启动脚本
appassembler-maven-plugin介绍: 使用appassembler-maven-plugin插件可自动生成跨平台的启动脚本,可省去手工写脚本的麻烦,而且还可以生成jsw的后台运行程 ...
- Mybatis根据数据库中的表自动生成Bean对象与Mapper文件 (小白式教程)
示例IDE采用 IDEA //**********************华丽的分割线****************// 1.新建一个java项目-->在Src目录下创建3个包(Package ...
- mint-ui message box 问题;
当引用 mint-ui message box 的 出现的问题,我暂时是不知道为什么: 官网是这样写的: 于是 我也这么做的:(这里用小写,具体我也不清楚,毕竟文档上写的也不是很清楚,但是只有这样写, ...
- centos总结linux下svn安装与使用
一.安装篇 centos下yum安装 yum install subversion 查看安装是否成功: svnserve --version 查看安装内容与位置 rpm -ql subversion ...
- php源码建博客5--建库建表-配置文件-错误日志
主要: 整理框架 建库建表 配置文件类 错误日志记录 --------------本篇后文件结构:-------------------------------------- blog ├─App │ ...
- python应用:爬虫框架Scrapy系统学习第三篇——初识scrapy
scrapy的最通用的爬虫流程:UR2IM U:URL R2:Request 以及 Response I:Item M:More URL 在scrapy shell中打开服务器一个网页 cmd中执行: ...
- Android Dagger2.0 学习一下
0.前言 个人感觉通过项目学习一些牛逼的框架,效果挺不错的. 1.个人理解 一直觉得Dagger2比较高大上,网上看了很多资料,很多,没有感觉. 然后怀疑智商问题,然后放弃了. 最后因为要做一个项目, ...
- 使用putty远程登录Ubuntu时,报Network error:Connection refused错误及解决
putty远程登录Ubuntu,弹出Network error:Connection refused的错误提示框,就是因为Ubuuntu没有安装ssh服务. 执行命令: sudo apt instal ...
- 武汉ber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 汽车VIN码识别/汽车车架号OCR识别,移动端VIN码识别,OCR扫描工具
本文推荐了一项汽车VIN码自动识别技术,用户通过手机“扫一扫”的简单操作,就可以快速识别VIN码,查询到车辆的详细信息,为汽修汽配.二手车交易.车辆监管.查勘理赔提高工作效率. VIN是英文Vehic ...