QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#743964#3140. DivModulovwxyzWA 14ms10776kbPython32.7kb2024-11-13 20:26:232024-11-13 20:26:28

Judging History

This is the latest submission verdict.

  • [2024-11-13 20:26:28]
  • Judged
  • Verdict: WA
  • Time: 14ms
  • Memory: 10776kb
  • [2024-11-13 20:26:23]
  • Submitted

answer

from collections import defaultdict
import math

def Factorize(N):
    assert N>=1
    factors=defaultdict(int)
    for p in range(2,N):
        if p**2>N:
            break
        while N%p==0:
            factors[p]+=1
            N//=p
    if N!=1:
        factors[N]+=1
    return factors
def CRT(remainder_lst,mod_lst):
    assert len(remainder_lst)==len(mod_lst)
    if not remainder_lst:
        return 0,1
    remainder,mod=remainder_lst[0],mod_lst[0]
    for r,m in zip(remainder_lst[1:],mod_lst[1:]):
        if (r,m)==(-1,0):
            remainder,mod=-1,0
            break
        r%=m
        g=math.gcd(mod,m)
        lcm=LCM(mod,m)
        if remainder%g!=r%g:
            remainder,mod=-1,0
            break
        remainder,mod=(r+m*((remainder-r)//g)*Extended_Euclid(m//g,mod//g)[0])%lcm,lcm
    return remainder,mod
def Extended_Euclid(n,m):
    stack=[]
    while m:
        stack.append((n,m))
        n,m=m,n%m
    if n>=0:
        x,y=1,0
    else:
        x,y=-1,0
    for i in range(len(stack)-1,-1,-1):
        n,m=stack[i]
        x,y=y,x-(n//m)*y
    return x,y

def inverse(x,mod):
    return Extended_Euclid(x,mod)[0]
    
def LCM(n,m):
    if n or m:
        return abs(n)*abs(m)//math.gcd(n,m)
    return 0

M,N,D=map(int,input().split())
F=Factorize(D)
def solve(N,M):
    remainders=[]
    mods=[]
    C=[]
    P,E=[],[]
    for p,e in F.items():
        pe=p**e
        def solve0(N,p,e,pe):
            if p==2:
                if e==2:
                    return 3
                return 1
            c=N//pe
            retu=pow(pe-1,c,pe)
            N=N-pe*c
            for i in range(1,N+1):
                if i%p:
                    retu*=i
                    retu%=pe
            return retu
        def solve1(N,p,e,pe):
            retu=1
            NN=N
            while NN:
                retu*=solve0(NN,p,e,pe)
                retu%=pe
                NN//=p
            cnt=0
            NN=N
            while NN:
                cnt+=NN//p
                NN//=p
            return retu,cnt
        x,c=solve1(M,p,e,pe)
        x0,c0=solve1(N,p,e,pe)
        x0=inverse(x0,pe)
        x*=x0
        c-=c0
        x%=pe
        x1,c1=solve1(M-N,p,e,pe)
        x1=inverse(x1,pe)
        x*=x1
        c-=c1
        x%=pe
        remainders.append(x)
        mods.append(pe)
        C.append(c)
        P.append(p)
        E.append(e)
    mi=min(c//e for c,e in zip(C,E))
    le=len(P)
    for i in range(le):
        C[i]-=mi*E[i]
    retu,mod=CRT(remainders,mods)
    for i in range(le):
        retu*=pow(P[i],C[i],mod)
        retu%=mod
    return retu

ans=solve(N,M)
print(ans)



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 11ms
memory: 10688kb

input:

9 2 3

output:

1

result:

ok single line: '1'

Test #2:

score: 0
Accepted
time: 3ms
memory: 10688kb

input:

5 2 5

output:

2

result:

ok single line: '2'

Test #3:

score: 0
Accepted
time: 8ms
memory: 10776kb

input:

6 3 6

output:

2

result:

ok single line: '2'

Test #4:

score: -100
Wrong Answer
time: 14ms
memory: 10760kb

input:

7654321 1234567 1050

output:

840

result:

wrong answer 1st lines differ - expected: '210', found: '840'