[問題] hackerrank - Maximize It
https://www.hackerrank.com/challenges/maximize-it/problem
第一行輸入:數列數 某整數n
第二行之後為依序輸入數列內容
每個數列取一個元素,再把每個元素平方和之後去餘數n=Smax (mod n或%n)
找到Smax為多少就是答案,就是六個數列各取一元素平方和再餘數n的最大值!
Sample Input
3 1000
2 5 4
3 7 8 9
5 5 7 8 9 10
Sample Output
206
我的解答如下:
from itertools import product
a=input().split()
pr=[]
for i in range(0,int(a[0])):
b=input().split()
pr.append(b)
prsum = product(*pr)
maxi=0
for items in prsum:
subsum = 0
for item2s in items:
if int(item2s) >int(a[1]):
subsum = subsum + (int(item2s)%int(a[1]))*(int(item2s)%int(a[1]))
else:
subsum = subsum + int(item2s)*int(item2s)
subsum = subsum % int(a[1])
if subsum > maxi:
maxi = subsum
print(maxi)
17個測試過了13個,四個失敗
我解鎖了一個測試內容如下:
Input (stdin)
6 767
2 488512261 423332742
2 625040505 443232774
1 4553600
4 92134264 617699202 124100179 337650738
2 778493847 932097163
5 489894997 496724555 693361712 935903331 518538304
Expected Output
763
但我的程式碼給的是766
我找到一組 組合
488512261
2
1
337650738
2
518538304
這六個數字平方和之後餘數767明明就是766
不曉得有沒有人可以解我的惑 ~'~
感謝
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.116.228.113 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Python/M.1632410634.A.5A3.html
※ 編輯: newbrain (122.116.228.113 臺灣), 09/23/2021 23:26:44
※ 編輯: newbrain (122.116.228.113 臺灣), 09/23/2021 23:27:18
推
09/24 00:30,
2年前
, 1F
09/24 00:30, 1F
推
09/24 01:54,
2年前
, 2F
09/24 01:54, 2F
→
09/24 01:55,
2年前
, 3F
09/24 01:55, 3F
→
09/24 07:52,
2年前
, 4F
09/24 07:52, 4F