結果

問題 No.2189 六平方和
ユーザー FromBooskaFromBooska
提出日時 2023-06-02 15:48:17
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,039 bytes
コンパイル時間 272 ms
コンパイル使用メモリ 87,152 KB
実行使用メモリ 348,144 KB
最終ジャッジ日時 2023-08-28 02:22:25
合計ジャッジ時間 6,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
75,656 KB
testcase_01 AC 70 ms
71,420 KB
testcase_02 AC 68 ms
71,300 KB
testcase_03 AC 71 ms
71,276 KB
testcase_04 AC 72 ms
70,912 KB
testcase_05 AC 74 ms
71,308 KB
testcase_06 AC 72 ms
71,276 KB
testcase_07 AC 72 ms
71,308 KB
testcase_08 AC 72 ms
71,268 KB
testcase_09 AC 71 ms
71,420 KB
testcase_10 AC 75 ms
71,348 KB
testcase_11 AC 79 ms
79,332 KB
testcase_12 AC 70 ms
70,908 KB
testcase_13 AC 71 ms
71,336 KB
testcase_14 AC 70 ms
71,144 KB
testcase_15 AC 70 ms
71,260 KB
testcase_16 AC 69 ms
71,212 KB
testcase_17 AC 70 ms
71,320 KB
testcase_18 AC 81 ms
77,404 KB
testcase_19 AC 91 ms
77,620 KB
testcase_20 TLE -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# この作戦でどうだ
# まずM**N (mod B) = Rをpowで計算する
# xi**2 (mod B)は常に0 for xi=B, or 1 for xi=1にできる
# x1からR以下一番近いところにx1**2 (mod B)がなる値にx1を固定
# 同様にx2, x3、ーーーと決めていく
# どんどん差は小さくなり、差を埋められればYes
# WA出た、かなり通ったけど。
# ラグランジュの四平方定理により、全ての自然数が高々四個の平方数の和で表される
# つまりこの問題にNOはないのだと思う
# 最初の2つのxを求めたらあとは全探索にしてはどうだろう

N, M, B = map(int, input().split())

R = pow(M, N, B)
R_remainder = R
INF = 10**20
X = []
for i in range(2):
    #print('i', i, 'R_remainder', R_remainder, X)
    if R_remainder == 0:
        X.append(B)
        continue
    
    diff = INF
    visited = set()
    for j in range(1, B+1):
        calc = (j**2)%B
        if calc not in visited:
            visited.add(calc)
            if calc <= R_remainder and R_remainder-calc < diff:
                diff = R_remainder-calc
                num = j
        elif calc in visited:
            break
    X.append(num)
    R_remainder -= (num**2)%B

if R_remainder == 0:
    for i in range(4):
        X.append(B)
    print('YES')
    print(*X)
else: 
    for x3 in range(0, R_remainder+1):

        for x4 in range(0, R_remainder+1):
            for x5 in range(0, R_remainder+1):
                for x6 in range(0, R_remainder+1):
                    if (x3**2 + x4**2 + x5**2 + x6**2)%B == R_remainder:
                        if x3 == 0:
                            x3 = B
                        if x4 == 0:
                            x4 = B
                        if x5 == 0:
                            x5 = B
                        if x6 == 0:
                            x6 = B
                        X.extend([x3, x4, x5, x6])
                        
                        print('YES')
                        print(*X)
                        exit()
0