URAL 2003. Simple Magic(数学啊 )

来源:互联网 发布:ubuntu 共享文件夹 编辑:程序博客网 时间:2024/06/07 11:49

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2003


2003. Simple Magic

Time limit: 1.0 second
Memory limit: 64 MB
Do you think that magic is simple? That some hand-waving and muttering incomprehensible blubber is enough to conjure wonderful gardens or a fireball to burn your enemies to ashes?
The reality is a little more complicated than that. To master skills, young wizards spend years studying such subjects as magical analysis and demonology practice.
In fact, Oleg, a student of the Institute of Magic and Common Sorcery (IMCS) is preparing for an exam. And there’s no way he can calculate the Dumbledore determinant. As you might have guessed, he asked you to help him.
Let us remind you the basic definitions just in case you haven’t been visiting lectures on the theory of nonlinear spells. The Gandalf theorem states that any part of subspace can be represented as a vector of magic potentials that is an array of n positive integers. A Dumbledore determinant of this array equals the minimum number of elementary magical transformations required to turn the original array into the array where all elements are equal to one. One elementary magical transformation turns the original array of length k into a new array of length k · (k − 1) / 2. The elements of the new array are greatest common divisors of each pair of elements of the original array. For example, the elementary magical transformation of array {2, 3, 3, 6} turns it into array {gcd(2, 3), gcd(2, 3), gcd(2, 6), gcd(3, 3), gcd(3, 6), gcd(3, 6)}, that is {1, 1, 2, 3, 3, 3}.

Input

The first line contains number n that is the length of the original array (3 ≤ n ≤ 10 000). Next nlines contain the elements of array that are positive integers not exceeding 107.

Output

Output Dumbledore determinant for the array given in the input. If Dumbledore determinant is not defined or it exceeds 1018, output “infinity”.

Samples

inputoutput
3123
1
42222
infinity
Problem Author: Kirill Borozdin
Problem Source: Ural Regional School Programming Contest 2013

题意:

问是否能按照类似:array {2, 3, 3, 6} turns it into array {gcd(2, 3), gcd(2, 3), gcd(2, 6), gcd(3, 3), gcd(3, 6), gcd(3, 6)}, 的方式,相互求最大公约数,最后变成数组里全是 1 ;

PS:

如果数组里有三个相同的素数存在,那么答案肯定是infinity!永远不可能变为全是1,会陷入死循环里!

所以答案只会是小于3的一个数!

代码如下:

#include <cstdio>#include <cmath>#include <cstring>#include <iostr<span style="white-space:pre"></span>eam>#include <algorithm>using namespace std;#define INF 0x3f3f3f3fint a[10000047];int main(){    int n;    int num;    while(~scanf("%d",&n))    {        memset(a, 0,sizeof(a));        int ans = -INF;        for(int i = 0; i < n; i++)        {            scanf("%d",&num);            int k = sqrt(num*1.0);            for(int i = 2; i <= k; i++)//质因子            {                if(num%i == 0)                {                    a[i]++;                    ans = max(ans,a[i]);                    while(1)                    {                        if(num%i == 0)                        {                            num/=i;                        }                        else                        {                            break;                        }                    }                }            }            if(num != 1)            {                a[num]++;            }            ans = max(ans,a[num]);        }        if(ans == 1)        {            printf("1\n");        }        else if(ans == 2)        {            printf("2\n");        }        else if(ans == 0)        {            printf("0\n");        }        else        {            printf("infinity\n");        }    }    return 0;}


1 0
原创粉丝点击