学校aitmc QUIZ1 TLE

chall.py

from AITMCLAB.libnum import s2n, invmod


cof_t = [[353, -1162, 32767], [206, -8021, 42110], [262, -7088, 31882], [388, -6394, 21225], [295, -9469, 44468],
         [749, -3501, 40559], [528, -2690, 10210], [354, -5383, 18437], [491, -8467, 26892], [932, -6984, 20447],
         [731, -6281, 11340], [420, -5392, 44071], [685, -6555, 40938], [408, -8070, 47959], [182, -9857, 49477],
         [593, -3584, 49243], [929, -7410, 31929], [970, -4549, 17160], [141, -2435, 36408], [344, -3814, 18949],
         [291, -7457, 40587], [765, -7011, 32097], [700, -8534, 18013], [267, -2541, 33488], [249, -8934, 12321],
         [589, -9617, 41998], [840, -1166, 22814], [947, -5660, 41003], [206, -7195, 46261], [784, -9270, 28410],
         [338, -3690, 19608], [559, -2078, 44397], [534, -3438, 47830], [515, -2139, 39546], [603, -6460, 49953],
         [234, -6824, 12579], [805, -8793, 36465], [245, -5886, 21077], [190, -7658, 20396], [392, -7053, 19739],
         [609, -5399, 39959], [479, -8172, 45734], [321, -7102, 41224], [720, -4487, 11055], [208, -1897, 15237],
         [890, -4427, 35168], [513, -5106, 45849], [666, -1137, 23725], [755, -6732, 39995], [589, -6421, 43716],
         [866, -3265, 30017], [416, -6540, 34979], [840, -1305, 18242], [731, -6844, 13781], [561, -2728, 10298],
         [863, -5953, 23132], [204, -4208, 27492], [158, -8701, 12720], [802, -4740, 16628], [491, -6874, 29057],
         [531, -4829, 29205], [363, -4775, 41711], [319, -9206, 46164], [317, -9270, 18290], [680, -5136, 12009],
         [880, -2940, 34900], [162, -2587, 49881], [997, -5265, 20890], [485, -9395, 23048], [867, -1652, 18926],
         [691, -7844, 11180], [355, -5990, 13172], [923, -2018, 23110], [214, -4719, 23005], [921, -9528, 29351],
         [349, -7957, 20161], [470, -1889, 46170], [244, -6106, 23879], [419, -5440, 43576], [930, -1123, 29859],
         [151, -5759, 23405], [843, -6770, 36558], [574, -6171, 33778], [772, -1073, 44718], [932, -4037, 40088],
         [848, -5813, 27304], [194, -6016, 39770], [966, -6789, 14217], [219, -6849, 40922], [352, -6046, 18558],
         [794, -8254, 29748], [618, -5887, 15535], [202, -9288, 26590], [611, -4341, 46682], [155, -7909, 16654],
         [935, -5739, 39342], [998, -6538, 24363], [125, -5679, 36725], [507, -7074, 15475], [699, -5836, 47549]]


def cal(n, cof):
    if n < 3:
        return n + 1
    else:
        return cof[2] * cal(n - 3, cof) + cof[1] * cal(n - 2, cof) + cof[0] * cal(n - 1, cof)


s = 0
for i in range(100):
    s += cal(200000, cof_t[i])

flag = '*****{*********************************************}'
s = str(s)[200:500]
s = int(s)
print(s)
flag = s2n(flag)
output = invmod(flag, s)
print(output)
# output = 874313388900970582168771813986340175856554854682958894216185742683256912266861620261838531994627287832433945013130218514716009094549062315903275275501187457843298705849957186742390702863963997451342976490306497616743715976818578290815009600131253639539142251202986322147938603975735394719542018754226

你直接跑发现会栈溢出(递归太多次了)
本来可以C用dp跑的 但是你会发现这s>几万位了
所以果断python 那么就需要矩阵加速线性递推数列的计算
solution.py (这里写了个巨丑的3x3矩阵乘法)

"""
利用矩阵加速线性递推计算
"""
import sys
from tqdm import tqdm
# import sys
def Martrix_qpow(A,B,C,n):
    MOD=10**600
    a1,a2,a3,a4,a5,a6,a7,a8,a9 = A,B,C,1,0,0,0,1,0
    t1,t2,t3,t4,t5,t6,t7,t8,t9 = 1,0,0,0,1,0,0,0,1
    while(n):
        if(n&1):
            tt1 = t1*a1+t2*a4+t3*a7
            # tt1%=MOD
            tt2 = t1*a2+t2*a5+t3*a8
            # tt2%=MOD
            tt3 = t1*a3+t2*a6+t3*a9
            # tt3%=MOD
            tt4 = t4*a1+t5*a4+t6*a7
            # tt4%=MOD
            tt5 = t4*a2+t5*a5+t6*a8
            # tt5%=MOD
            tt6 = t4*a3+t5*a6+t6*a9
            # tt6%=MOD
            tt7 = t7*a1+t8*a4+t9*a7
            # tt7%=MOD
            tt8 = t7*a2+t8*a5+t9*a8
            # tt8%=MOD
            tt9 = t7*a3+t8*a6+t9*a9
            # tt9%=MOD
            t1,t2,t3,t4,t5,t6,t7,t8,t9=tt1,tt2,tt3,tt4,tt5,tt6,tt7,tt8,tt9

        aa1 = a1*a1+a2*a4+a3*a7
        aa2 = a1*a2+a2*a5+a3*a8
        aa3 = a1*a3+a2*a6+a3*a9
        aa4 = a4*a1+a5*a4+a6*a7
        aa5 = a4*a2+a5*a5+a6*a8
        aa6 = a4*a3+a5*a6+a6*a9
        aa7 = a7*a1+a8*a4+a9*a7
        aa8 = a7*a2+a8*a5+a9*a8
        aa9 = a7*a3+a8*a6+a9*a9
        # aa1%=MOD
        # aa2%=MOD
        # aa3%=MOD
        # aa4%=MOD
        # aa5%=MOD
        # aa6%=MOD
        # aa7%=MOD
        # aa8%=MOD
        # aa9%=MOD
        a1,a2,a3,a4,a5,a6,a7,a8,a9=aa1,aa2,aa3,aa4,aa5,aa6,aa7,aa8,aa9

        n>>=1
    # return [[t1,t2,t3],[t4,t5,t6],[t7,t8,t9]]
    return (3*t1+2*t2+t3)

cof_t = [[353, -1162, 32767], [206, -8021, 42110], [262, -7088, 31882], [388, -6394, 21225], [295, -9469, 44468],
         [749, -3501, 40559], [528, -2690, 10210], [354, -5383, 18437], [491, -8467, 26892], [932, -6984, 20447],
         [731, -6281, 11340], [420, -5392, 44071], [685, -6555, 40938], [408, -8070, 47959], [182, -9857, 49477],
         [593, -3584, 49243], [929, -7410, 31929], [970, -4549, 17160], [141, -2435, 36408], [344, -3814, 18949],
         [291, -7457, 40587], [765, -7011, 32097], [700, -8534, 18013], [267, -2541, 33488], [249, -8934, 12321],
         [589, -9617, 41998], [840, -1166, 22814], [947, -5660, 41003], [206, -7195, 46261], [784, -9270, 28410],
         [338, -3690, 19608], [559, -2078, 44397], [534, -3438, 47830], [515, -2139, 39546], [603, -6460, 49953],
         [234, -6824, 12579], [805, -8793, 36465], [245, -5886, 21077], [190, -7658, 20396], [392, -7053, 19739],
         [609, -5399, 39959], [479, -8172, 45734], [321, -7102, 41224], [720, -4487, 11055], [208, -1897, 15237],
         [890, -4427, 35168], [513, -5106, 45849], [666, -1137, 23725], [755, -6732, 39995], [589, -6421, 43716],
         [866, -3265, 30017], [416, -6540, 34979], [840, -1305, 18242], [731, -6844, 13781], [561, -2728, 10298],
         [863, -5953, 23132], [204, -4208, 27492], [158, -8701, 12720], [802, -4740, 16628], [491, -6874, 29057],
         [531, -4829, 29205], [363, -4775, 41711], [319, -9206, 46164], [317, -9270, 18290], [680, -5136, 12009],
         [880, -2940, 34900], [162, -2587, 49881], [997, -5265, 20890], [485, -9395, 23048], [867, -1652, 18926],
         [691, -7844, 11180], [355, -5990, 13172], [923, -2018, 23110], [214, -4719, 23005], [921, -9528, 29351],
         [349, -7957, 20161], [470, -1889, 46170], [244, -6106, 23879], [419, -5440, 43576], [930, -1123, 29859],
         [151, -5759, 23405], [843, -6770, 36558], [574, -6171, 33778], [772, -1073, 44718], [932, -4037, 40088],
         [848, -5813, 27304], [194, -6016, 39770], [966, -6789, 14217], [219, -6849, 40922], [352, -6046, 18558],
         [794, -8254, 29748], [618, -5887, 15535], [202, -9288, 26590], [611, -4341, 46682], [155, -7909, 16654],
         [935, -5739, 39342], [998, -6538, 24363], [125, -5679, 36725], [507, -7074, 15475], [699, -5836, 47549]]
sys.set_int_max_str_digits(1000000000)
# print((Martrix_qpow(1,0,1,200000-2)))

s = 0
for i in tqdm(range(100)):
    A,B,C = cof_t[i][0],cof_t[i][1],cof_t[i][2]
    s += (Martrix_qpow(A,B,C,200000-2))
    # s%=(10**600)
    print(s)
print('---------------\n',s)

from primefac import modinv
from Crypto.Util.number import *
# s = '57709171548283978493789672164389262367780003799912379430159180996708931153685247188448766302221828834229468065336798890762185471907598053284655084528167876668913499231166615741617457075837994765524299317728879425919879738373533542715286046186604560260709422789916542737083936554835927765446587880054113941556536245076120483977590860174687574141915271810322922694619145802742691031173004606234319342659656136109538946966020100547023188045069304897700235667305789512376362788658253222588063422771988979917451491041056388945341732267366088833540253010571209387382627472623492993652482346825872512251031517'
s = str(s)[200:500]
s = int(s)
output = 874313388900970582168771813986340175856554854682958894216185742683256912266861620261838531994627287832433945013130218514716009094549062315903275275501187457843298705849957186742390702863963997451342976490306497616743715976818578290815009600131253639539142251202986322147938603975735394719542018754226
flag = modinv(output,s)
print(long_to_bytes(flag))

跑一跑 很快就能跑出结果
耗时这么长是因为出题人要s[200:500] 这是取开头。。。
最后得到flag
image

posted @ 2023-10-22 12:29  N0zoM1z0  阅读(32)  评论(0编辑  收藏  举报