URAL 2003. Simple Magic(数学啊 )

题目链接: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;
}
时间: 2024-12-26 05:53:56

URAL 2003. Simple Magic(数学啊 )的相关文章

ural 2003. Simple Magic 数论 因数分解

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 real

[2016-04-16][URAL][2066][Simple Expression]

时间:2016-04-16 20:04:26 星期六 题目编号:[2016-04-16][URAL][2066][Simple Expression] 题目大意:给出 a b c三个数字,在他们之间插入 + - *,问能得到的最小值是多少? 分析:直接枚举- #include<cstdio> #include<algorithm> using namespace std; int main(){ int a,b,c; scanf("%d%d%d",&a,

ACM学习历程—HDU5490 Simple Matrix (数学 &amp;&amp; 逆元 &amp;&amp; 快速幂) (2015合肥网赛07)

Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progression and sequence in the form of bn=b1qn−1(q>1,b1≠0) is called geometric progression. Huazheng wants to use these two simple sequences to generate a simp

17997 Simple Counting 数学

17997 Simple Counting 时间限制:2000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 Description Ly is crazy about counting . Recently , he got a simple problem , but he had to learn Gaoshu these days .So , he turns to you for help . You are given a sequenc

URAL 1823. Ideal Gas 数学,分类

1823. Ideal Gas Time limit: 0.5 second Memory limit: 64 MB Many of you know the universal method of solving simple physics problems: you have to find in a textbook an identity in which you know the values of all the quantities except for one, substit

URAL 1515. Cashmaster (数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1515 Background Once upon a time former petty bureaucrat, nowadays the Minister of Finance of the Soviet Federation, Victor Thiefton considered, that he had stolen so much money during the first half

URAL 1718 . Rejudge(数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1718 1718. Rejudge Time limit: 0.5 second Memory limit: 64 MB At three o'clock in the morning Sasha (aka Sandro) discovered that there were only seven tests for one of the problems in the first volume

URAL 1295 Crazy Notions 数学 找规律

1295. Crazy Notions Time limit: 0.5 second Memory limit: 64 MB For five days robot-loader JK546L54p has been buried under the thick layer of the Sibelian plutonium slag. The terrible strike of the atmospheric electricity has led to the depressurizati

URAL 2066 Simple Expression (水题,暴力)

题意:给定三个数,让你放上+-*三种符号,使得他们的值最小. 析:没什么好说的,全算一下就好.肯定用不到加,因为是非负数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>