Ural1053 (GCD)

1053. Pinocchio

Time limit: 1.0 second
Memory limit: 64 MB
Father Carlo got the commission to make Pinocchio. Client expressed a wish to be unknown and he left material and insisted on finding Pinocchio's nose length as a result of performing the following algorithm:
  1. There's a set of N numbered blanks with integer lengths.
  2. If the set consists of only one blank, then it's length can be admitted as the length of Pinocchio's nose
  3. Let's choose some 2 blanks
    1. If lengths of the blanks coincide, then one of the blanks is eliminated from the set and algorithm goes back to point 2 to be repeated.
    2. If lengths of the blanks are different, then the piece of the long blank is sawed off and its length must be equal to the length of the other blank. Then the algorithm is repeated from point 2.
Example. There are 3 blanks in a set with lengths: 2, 3, 4. Then the change of the blank lengths can be shown in the following table. As a result Pinocchio will get the nose with length of 1.
Length of the
first blank
Length of the
second blank
Length of the
third blank
Comments
234Initial blank lengths
214Sawing off the second blank
213Sawing off the third blank
212Sawing off the third blank
112Sawing off the first blank
-12The first blank is eliminated
-11Sawing off the third blank
--1The second blank is eliminated

Input

The first line contains integer N (1 ≤ N ≤ 1000) . The other N successive lines contain integers L 1, L 2, …, LN.
1 ≤ L 1, L 2, …, LN ≤ MaxLongInt .

Output

Output should contain either one number (Pinocchio nose length), or the word IMPOSSIBLE (in upper case) if the nose length cannot be defined.

Sample

inputoutput
3
2
3
4
1
题目描述就不多说了,还以为时直接简单模拟的水题,但是TLE,后来突然想到了GCD,就是简单的GCD,求所有数的最大公约数嘛。。。真是too young ,too naive!贡献了一次TLE。。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1005

int a[N];

int gcd(int a,int b)
{
	if(a<b)swap(a,b);
	if(b==0)return a;
	else return gcd(b,a%b);
} 
int main()
{
	int n;
	int i,j;
	scanf("%d",&n);
	
	for(i=0;i<n;i++)
	 scanf("%d",&a[i]);
	 
	int tmp=a[0];
	for(i=1;i<n;i++)
	{
	   tmp=gcd(tmp,a[i]);
	} 
	cout<<tmp<<endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值