zoukankan      html  css  js  c++  java
  • Ural 2003: Simple Magic(数论&思维)

    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 n lines 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
    3
    1
    2
    3
    
    1
    
    4
    2
    2
    2
    2
    
    infinity
    
    Problem Author: Kirill Borozdin

    题意:给定N个数,每一轮变换成两两对应的GCD:即变换前是X个数,变换后是X*(X-1)/2个数。如: {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}.                问第几次变换后全部变为1,如果不行,输出“infinity”。

    思路:如果有某一轮变换前有三个或以上的相同的数(不等于1),则不可能全部变为1,如样例的2,2,2。但是不可能模拟每一轮转化的过程。我们换个角度:如果一个因子在大于等于三个数里出现,则者三个数会相互影响,一直繁殖下去.所以答案为:

               0:已经全部是1

               1:所有数互质

               2:有相同因子,但是有同一因子的个数不大于2.

               inf:存在一个因子,在操作两个数里出现过。

    (坚持A掉题之前不看题解,自己做!!!加油)

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=10000;
    int p[maxn+10],vis[maxn+10],num[maxn*1000+10],cnt;
    void getprime()
    {
        for(int i=2;i<=maxn;i++){
            if(!vis[i]) p[++cnt]=i;
            for(int j=1;j<=cnt&&i*p[j]<=maxn;j++){
                vis[i*p[j]]=1;
                if(i%p[j]==0) break;
            }
        }
    }
    int main()
    {
        getprime();
        int N,i,j,x;
        bool F=true;
        scanf("%d",&N);
        for(i=1;i<=N;i++){
            scanf("%d",&x);
            if(x!=1) F=false;
            for(j=1;j<=cnt;j++){
                if(x%p[j]==0){
                    num[p[j]]++;
                    while(x%p[j]==0) x/=p[j];
                }
            }
            if(x>1) num[x]++;
        }
        if(F) {
            printf("0
    ");
            return 0;
        }
        for(i=2;i<=10000000;i++)
          if(num[i]>2){
             printf("infinity
    ");
             return 0;
        }
        for(i=2;i<=10000000;i++)
         if(num[i]==2){
             printf("2
    ");
             return 0;
        }
        printf("1
    ");
        return 0;
    }
  • 相关阅读:
    [工控安全][原创]施耐德某PLC模块用户密码相关漏洞
    [工控安全][原创]施耐德某PLC模块敏感信息泄露漏洞
    [工控安全][原创]西门子PLC固件逆向之定位s7comm协议的一个切入口
    [安全工具][原创]保存IDA Pro中生成的函数调用关系(图)
    [工控安全][原创]西门子PLC固件逆向之socket API(总览)
    [工控安全][原创]面向开环控制的震网病毒恶意载荷探究
    [工控安全][翻译]Rogue7:西门子s7comm-plus协议全解析
    [工控/IOT安全][笔记]ARM设备固件装载基址定位的研究
    Tor源码阅读与改造(一)
    Java WebDriver 使用经验
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8867550.html
Copyright © 2011-2022 走看看