QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#91082#6126. Sequence and SequenceDenisovAC ✓9657ms18400kbPython33.5kb2023-03-27 02:45:042023-03-27 02:45:05

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-27 02:45:05]
  • 评测
  • 测评结果:AC
  • 用时:9657ms
  • 内存:18400kb
  • [2023-03-27 02:45:04]
  • 提交

answer

import math
from fractions import Fraction



factorials = [1]
N = 200
for i in range(N + 5):
    factorials.append(factorials[-1] * (i + 1))


def C(n, k):
    return factorials[n] // factorials[k] // factorials[n - k]


def sign(n):
    if n % 2 == 0:
        return 1
    return -1


poly = [[Fraction(0, 1), Fraction(1, 1)]]
pp = [[Fraction(1, 1)]]
for i in range(1, N):
    cur = [Fraction(0, 1) for j in range(i + 2)]
    cur[i + 1] += 1
    for j in range(1, i + 1):
        for it in range(len(poly[i - j])):
            cur[it] += C(i + 1, j + 1) * sign(j + 1) * poly[i - j][it]
    for j in range(len(cur)):
        cur[j] /= i + 1
    poly.append(cur)

    cur = [Fraction(0, 1) for j in range(len(pp[-1]) + 2)]
    for j in range(len(pp[-1])):
        cur[j] -= pp[-1][j]
        cur[j + 1] += pp[-1][j] / 2
        cur[j + 2] += pp[-1][j] / 2
    pp.append(cur)
#print(pp)

ff = [[Fraction(1, 1)]]
FF = []

K = 5
MX = 613

for i in range(K):
    cur = [Fraction(0, 1) for j in range(len(ff[-1]) + 1)]
    for j in range(len(ff[-1])):
        for k in range(len(poly[j])):
            cur[k] += ff[-1][j] * poly[j][k]
    FF.append(cur)
    if i == K - 1:
        break
    cur = [Fraction(0, 1) for j in range(len(FF[-1]) * 2)]
    for j in range(len(FF[-1])):
        for k in range(len(pp[j])):
            cur[k] += FF[-1][j] * pp[j][k]
    ff.append(cur)


prec_f = []
prec_F = []
prec = []


def P(n) -> int:
    D = 1 + 8 * n
    k = (int(math.isqrt(D)) - 1) // 2
    while (k + k + 3) * (k + k + 3) <= D:
        k += 1
    while (k + k + 1) * (k + k + 1) > D:
        k -= 1
    return k


good = [0, 1]
M = 1000
for i in range(2, M + 5):
    good.append(good[-1] + good[P(i)])

for i in range(K):
    cur = []
    for x in range(MX):
        s = Fraction(0, 1)
        y = 1
        for k in range(len(ff[i])):
            s += y * ff[i][k]
            y *= x
        cur.append(s)
    prec_f.append(cur)
    cur = []
    for x in range(MX):
        s = Fraction(0, 1)
        y = 1
        for k in range(len(FF[i])):
            s += y * FF[i][k]
            y *= x
        cur.append(s)
    prec_F.append(cur)
    cur = [0]
    s = 0
    for x in range(1, MX):
        s += prec_f[i][x] * good[P(x)]
        cur.append(s)
    prec.append(cur)


def f(d, n):
    if d == 0:
        return 1
    if d < len(ff):
        if n < MX:
            return prec_f[d][n]
        s = Fraction(0, 1)
        x = 1
        for i in range(len(ff[d])):
            s += ff[d][i] * x
            x *= n
        return s
    return F(d - 1, (n * (n + 1)) // 2 - 1)


def F(d, n):
    if d == 0:
        return n
    if d < len(FF):
        if n < MX:
            return prec_F[d][n]
        s = Fraction(0, 1)
        x = 1
        for i in range(len(FF[d])):
            s += FF[d][i] * x
            x *= n
        return s
    s = 0
    for i in range(1, n + 1):
        s += f(d, i)
    return s


def solve(n: int, d = 0):
    if n == 1:
        return f(d, 1)
    if d < len(prec) and n < MX:
        return prec[d][n]
    to = P(n)
    return F(d, n) * solve(to) - solve(to, d + 1)


"""


def stupid(n):
    return good[n]
"""



t = int(input())


while t > 0:
    n = int(input())
    #n = 10000000000000000000000000000000000000000 - t
    #n = M - t + 1
    cur = solve(n)
    #print(type(cur))
    print(cur)
    #cur -= stupid(n)
    #assert cur == 0
    t -= 1

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6907ms
memory: 18092kb

input:

4
10
100
1000
987654321123456789

output:

30
2522
244274
235139898689017607381017686096176798

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 7992ms
memory: 18008kb

input:

10000
613939207402503646
408978283345333197
976677512231308716
629053280913850466
148712339
236220313279945487
590396556995977994
9226
215693877607285701
649702683896705
343173887453826567
847003949499596615
867133040287550291
159928123569892354
864534948175618654
209739383170746721
4295456752378791...

output:

91030728117067063595428375419346402
40469246710473908695676971059074376
229951682342450470520349294486964970
95558039501640054006660579352994194
5418340556433412
13536357243714243974772906966693552
84197207203086091733385317631619504
20934656
11291075621624104092841708040651034
104019777231815308683...

result:

ok 10000 lines

Test #3:

score: 0
Accepted
time: 7335ms
memory: 18400kb

input:

1000
6403632579734162001008235137760132245297
1307698664787972023762442022139627469666
668870338048562416595095770565441759482
5092270
8806864498496723812760785099973409980711
2178464116010775202899984038946879469187
204381824371
8638495456004772442511643693521120926431
45954412333082528168092594892...

output:

9822905445826021159291522774438593145331066315784567505849706373529921001845336
409552844852728078841401625660602682494169687360338453221088647649526339235330
107160056181509722327918304399871120167096186888511567354513496578559803370274
6354453295964
185817323129718525790559571287806776421589155460...

result:

ok 1000 lines

Test #4:

score: 0
Accepted
time: 7615ms
memory: 18004kb

input:

2000
2587627816607030340103003175959756184662
7728753705064569253253827797978613582938
6507847628603052973674714721
6989857636824717968431061258300652290539
4734281027640913533293237760425416005062
9411123735455625690768098631226366597446
8309753930995253536640660321476246470149
63565157427098067709...

output:

1603610451790269237852635641930301658193367441164307312552842461667027137613454
14309943493171496191506053530749878345271155973702143153225815632926701434642842
10414697803791950572309383355053080338229465674803757518
11704102206894264239190418551798088411458157423624785417336589284698838535371266
5...

result:

ok 2000 lines

Test #5:

score: 0
Accepted
time: 8436ms
memory: 18016kb

input:

5000
6701025283447923273597553918313900029215
1618190467906965189844764312914748628527
2135261797018456059451326428589353332126
8027429917075086154217136768450383650445
8263301530632040969183919589286944799002
376886964626613356031779878
1191561726595055348898524031899625958102
453561433135467095374...

output:

10756647971303093856509780939435968749671842310025383400207261624750784751725876
627115945498078452730193858129037650151913122482133071938783012929533045529940
1091921493223233296724616654299408127388059493196845968361252389122720100379070
154375707400033063668088306493199269136326791073281723548116...

result:

ok 5000 lines

Test #6:

score: 0
Accepted
time: 9657ms
memory: 18036kb

input:

10000
260865660295317278841
5232352637620496342310336202478387251106
7108789244285764135987032973912918380
12766535319519586095540974948550152061
5138306300154916887884637635208203681949
7603163140266839847784708214115317398585
149590294591155205497765668766786424787
63283557694500045989299147454323...

output:

16323111740957704392106241109891718054228
6557703685144914472554701877112177422100848067214049191882271294010013621817762
12143115079716078114619105501427985631361994195400195527663921137836417758
39139456824156526604158618001888125076786817219954316014947704612553450312
6324051018379978443719363340...

result:

ok 10000 lines