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 ...
随机推荐
- vscode调试html文件
1. vscode调试html文件 1.1. 使用Debugger for Chrome进行调试 1.1.1. 基于本地file配置方式调试 1.1.2. 基于服务端配置方式调试 1.1.2.1. 启 ...
- Wireshark工具抓包的数据包分析
Wireshark(前称Ethereal)是一个网络封包分析软件.网络封包分析软件的功能是撷取网络封包,并尽可能显示出最为详细的网络封包资料. Wireshark使用WinPCAP作为接口,直接与网卡 ...
- linux操作之软件安装(一)
rpm 包安装 RedHat Package Manager的缩写 , linux 的软件包可能存在依赖关系,比如某某依赖某某才能使用. 挂载一个光盘 mount -t auto /dev/cdrom ...
- S3C2440上LCD驱动(FrameBuffer)实例开发讲解(一)
一.开发环境 主 机:VMWare--Fedora 9 开发板:Mini2440--64MB Nand, Kernel:2.6.30.4 编译器:arm-linux-gcc-4.3.2 二.背景知识 ...
- 『Python基础-13』函数 Function
这篇笔记记录的知识点: 函数的基本概念 自定义函数 函数的几种参数 编程的三种方式: 1.OOP 面向对象编程,万物皆对象,以class为主,抽象化 2.POP 面向过程编程,万事皆过程,def定义过 ...
- Linux字符设备驱动--No.2
分析中断注册函数:request_irq int butsOpen(struct inode *p, struct file *f) { int irq; int i; ; printk(KERN_E ...
- 查看 dll 是32位还是64位 的 bat
******* @echo offset work_path=./cd %work_path% for /R %%s in (*.dll) do ( echo %%~nxs call dumpbin ...
- Milking Order
Milking Order 题意:给出m个描述状态,其中包含若干个边的关系,问最多能取x (x<=m)个状态,使得形成的图没有环.就是说取x个状态,用状态中的关系建边,其中不能有环. 题解:最大 ...
- 青岛Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Python之多进程多线程
一.多进程与多线程的概念 1.多进程的概念 进程是程序在计算机上的的一次执行活动.当你运行一个程序,你就启动了一个进程.显然,程序是死的(静态的),进程是活的(动态的).进程可以分为系统进程和用户进程 ...