Google Code Jam 2019中问题3的解决方案

时间:2019-04-07 06:01:42

标签: python python-3.x

昨天我尝试解决google code jam问题“ Cryptopangrams”。我能够通过示例案例,但是我的解决方案未被接受。

问题陈述可以在这里找到: https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/000000000008830b

我试图使用传统方法来找到数字的因素(对于限制因素,polod rho似乎过于杀伤),然后将所有唯一因素分类到一个列表中。字母表中的字母与列表中的元素之间存在一对一的对应关系。

然后,我将字母替换为产品,并返回字符串。

当我在笔记本电脑上尝试该代码,针对问题中给出的两个示例案例对其进行测试时,该代码有效,但是当我将其上传至网站时,该代码失败了。

# Function to find the prime factors of n and returns them in a list
def prime_factors(n):
    i = 2
    factors = []
    while i * i <= n:
        if n % i:
            i += 1
        else:
            n //= i
            factors.append(i)
    if n > 1:
        factors.append(n)
    return factors

# Gets number of cases
cases = int(input())

for case in range(cases):
    text = ''
    alphabets = 'abcdefghijklmnopqrstuvwxyz'

    ch = input().split()
    no_of_chars = int(ch[1])
    max_prime = int(ch[0])

    # gets products from input
    products = [int(i) for i in input().split()]

    flag = None
    factors = []
    pairs = []

    # For loop to find factors and append them to pairs
    for i in range(len(products)):
        l = prime_factors(products[i])
        a, b = l[0], l[1]
        if i > 0:
            if pairs[i-1][1] == a:
                flag = True
            # Swaps elements of pair if not in linked order
            else:
                a, b = b, a
        pairs.append([a, b])

        # Adds new factors to list
        if a in factors:
            flag = True
        else:
            factors.append(a) 

        if b in factors:
            flag = False
        else:
            factors.append(b)

    # Sorts the factors
    factors.sort()
    for i in pairs:
        text += alphabets[factors.index(i[0])]
    text += alphabets[factors.index(pairs[-1][1])]
    # Prints output in the required format
    print('Case #{}: {}'.format(case + 1, text.upper())

在我的笔记本电脑上,输入(复制粘贴自https://codingcompetitions.withgoogle.com/codejam/round/0000000000051705/000000000008830b

2
103 31
217 1891 4819 2291 2987 3811 1739 2491 4717 445 65 1079 8383 5353 901 187 649 1003 697 3239 7663 291 123 779 1007 3551 1943 2117 1679 989 3053
10000 25
3292937 175597 18779 50429 375469 1651121 2102 3722 2376497 611683 489059 2328901 3150061 829981 421301 76409 38477 291931 730241 959821 1664197 3057407 4267589 4729181 5335543

我正在获取输出:

Case #1: CJQUIZKNOWBEVYOFDPFLUXALGORITHMS
Case #2: SUBDERMATOGLYPHICFJKNQVWXZ

与网站上提供的相同。 但是当我提交它时,我收到了以下消息。

Wrong Answer! Test Set Skipped

谁能告诉我我哪里出问题了? 谢谢!

3 个答案:

答案 0 :(得分:4)

1
107 29
15 15 15 15 21 49 77 143 221 323 437 667 899 1147 1517 1763 2021 2491 3127 3599 4087 4757 5183 5767 6557 7387 8633 9797 10403
Case #1: ABABACCDEFGHIJKLMNOPQRSTUVWXYZ

尝试此测试用例。 万一你弄错了。 说明: 假设形式为pq,pq,qr的半素数 如果您决定以p开头,则p,q,p,但此p不会除以qr; 如果以q,p,q,r ........ right

开始

因此Logic会一直将第一个半素的gcd与其他半素一起获取,直到您得到gcd!= firstsemiprime为止,该gcd会在某个位置上为您的一个素数 然后,前后迭代以获得其他素数。 解决方案: https://shashankmishracoder.wordpress.com/2019/04/07/google-code-jam-2019-qualification-round-problem-c-cryptopangrams/

答案 1 :(得分:2)

我解决这个问题的方法是利用数字为质数这一事实。现在给定p1 * p2和p2 * p3,我们可以得到p2 = gcd(p1 * p2,p2 * p3)和p1 = p1 * p2 / gcd(p1 * p2,p2 * p3)。唯一必要的条件是p1 * p2!= p2 * p3

因此python3中的代码类似于:

from math import gcd

T=int(input())
for t in range(T):
    N,M=map(int,input().split())
    l=list(map(int,input().split()))
    a=[0]*(M+1)
    for i in range(len(l)-1):
        if l[i]!=l[i+1]:
            a[i+1]=gcd(l[i],l[i+1])
            for j in range(i,-1,-1):
                a[j]=l[j]//a[j+1]
            for j in range(i+1,M):
                a[j+1]=l[j]//a[j]
    # print(a)
    b=sorted(list(set(a)))
    d={}
    for i in range(len(b)):
        d[b[i]]=chr(65+i)
    ans=""
    for i in a:
        ans+=d[i]
    print("Case #"+str(t+1)+":",ans)

答案 2 :(得分:0)

即使我的解决方案没有通过系统测试。但是我尝试了另一种方法。您的代码肯定会获得TLE,因为'n'的约束为nˆ100。 仔细阅读问题!给定数组中的每个数字都是两个质数的乘积。举例来说,a,b,c是素数。现在a [0] = a * b,a [1] = b * c。 现在,如果您能够确定“ b”,则可以使用正态除法轻松找到a和c。 a [0]和a [1]的最大公约数是b!

相关问题