Help us understand the problem. What is going on with this article?

Codeforces Round #615 C. Product of Three Numbers

こういう問題を瞬殺できるようにならないといけない。(20分かかったけど気持ち5分くらいで解きたい)

概要

  • nが与えられる。積がnとなる異なる$a,b,c$があるならば答えよ。ただし、それぞれ2以上の整数である。

積という時点で素因数分解した数の組み合わせという推察はできる。
- 64 ⇒可能 2*4*8
- 32 ⇒不可能 (2*2*2*2*2のため、どうやっても2か4が被る)
- 97 ⇒不可能(素数)

アプローチ

nは高々$10^{9}$なので、$2^{30}$以下である。ということは素因数は高々30であるので、総当たりする。
- 素因数分解する
- この数が2以下の場合は条件を満たせない。
- 素因数を適当に3つに分けて掛け算し、違う3つの数ができたらYESとして答えに採用

WORSTでも30*30*30 = 9000程度なので、力づくで試行した。

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            for j in range(cnt):
                arr.append(i)

    if temp != 1:
        arr.append(temp)

    if arr == []:
        arr.append(n)

    return arr
n = int(input())
for _ in range(n):
    x = int(input())
    dat = factorization(x)
    datlen = len(dat)
    if len(dat) <= 2:
        print("NO")
    else:
        f = False
        a = 1
        for i in range(datlen):
            a *= dat[i]
            b = 1
            for j in range(i+1, datlen):
                b *= dat[j]
                c = 1
                for k in range(j+1, datlen):
                    c *= dat[k]
                if a != b and b != c and a != c and a != 1 and b != 1 and c != 1:
                    f = True
                    break
            if f:
                break
        if f:
            print("YES")
            print("{0} {1} {2}".format(a, b, c))
        else:
            print("NO")

https://codeforces.com/contest/1294/submission/69332106

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした