码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 2003. Simple Magic(数学啊 )

时间:2015-02-11 20:39:10      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:ural   数学   

题目链接: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

input output
3
1
2
3
1
4
2
2
2
2
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)}, 的方式,求所给数组!

代码如下:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
int 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;
}


URAL 2003. Simple Magic(数学啊 )

标签:ural   数学   

原文地址:http://blog.csdn.net/u012860063/article/details/43737463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!