QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#112739#6518. Not Another Linear Algebra Problemgtm1514AC ✓2554ms277960kbC++142.0kb2023-06-13 08:44:412023-06-13 08:44:43

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-13 08:44:43]
  • 评测
  • 测评结果:AC
  • 用时:2554ms
  • 内存:277960kb
  • [2023-06-13 08:44:41]
  • 提交

answer

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
int n,q,mod,ord,jc[10000010],inv[10000010],pw[10000010],cpw[10000010],a[10000010],jcp[10000010],invp[10000010];
int C(int n,int m){
    int rn=n%ord,rm=m%ord;
    n/=ord;m/=ord;
    if(n<m||rn<rm)return 0;
    return 1ll*jc[n]*inv[m]%mod*inv[n-m]%mod*jcp[rn]%mod*invp[rm]%mod*invp[rn-rm]%mod;
}
int qpow(int a,int b,int mod){
    int ans=1;
    while(b){
        if(b&1)ans=1ll*ans*a%mod;
        a=1ll*a*a%mod;
        b>>=1;
    }
    return ans;
}
struct node{
    int sq=1<<16,pw[1<<17],gpw[1<<17];
    node(){
        pw[0]=gpw[0]=1;
        for(int i=1;i<sq;i++){
            pw[i]=3ll*pw[i-1]%mod;
        }
        gpw[1]=3ll*pw[sq-1]%mod;
        for(int i=2;i<sq;i++)gpw[i]=1ll*gpw[i-1]*gpw[1]%mod;
    }
    int get(int n){
        return 1ll*gpw[n/sq]*pw[n%sq]%mod;
    }
};
int main(){
    scanf("%d%d%d",&n,&q,&mod);jc[0]=inv[0]=inv[1]=1;
    node P;
    for(int i=2;i<=n;i++)inv[i]=1ll*(mod-mod/i)*inv[mod%i]%mod;
    for(int i=1;i<=n;i++)jc[i]=1ll*jc[i-1]*i%mod,inv[i]=1ll*inv[i-1]*inv[i]%mod;
    pw[0]=cpw[0]=1;pw[1]=q;
    for(int i=1;i<=n;i++)pw[i]=1ll*pw[i-1]*pw[1]%mod,cpw[i]=1ll*cpw[i-1]*pw[i-1]%mod;
    ord=n+1;
    for(int i=1;i<=n;i++)if(pw[i]==1){
        ord=i;break;
    }
    jcp[0]=invp[0]=1;
    for(int i=1;i<=n;i++)jcp[i]=1ll*jcp[i-1]*(1-pw[i]+mod)%mod;
    invp[n]=qpow(jcp[n],mod-2,mod);
    for(int i=n-1;i>=1;i--)invp[i]=1ll*invp[i+1]*(1-pw[i+1]+mod)%mod;
    a[0]=1;int invpw=qpow(q,mod-2,mod);
    for(int i=1,Inv=1;i<=n;i++){
        Inv=1ll*Inv*invpw%mod;
        a[i]=1ll*(1ll*((i&1)?(mod-1):1)*cpw[i]+1ll*(pw[i-1]-pw[n]+mod)%mod*(1-pw[i]+mod)%mod*a[i-1])%mod*Inv%mod;
    }
    for(int i=0;i<=n;i++)a[i]=1ll*a[i]*C(n,i)%mod;
    int ans=0,ret=qpow(q,n,mod-1);
    if(!ret)ret=mod-1;
    for(int i=0,tmp=1;i<=n;i++){
        ans=(ans+1ll*P.get(tmp)*a[n-i])%mod;
        tmp=1ll*tmp*ret%(mod-1);
    }
    printf("%d\n",ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 16788kb

input:

2 2 1000000007

output:

43046970

result:

ok 1 number(s): "43046970"

Test #2:

score: 0
Accepted
time: 2ms
memory: 17000kb

input:

100 127 998244353

output:

881381862

result:

ok 1 number(s): "881381862"

Test #3:

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

input:

1 2 540053233

output:

9

result:

ok 1 number(s): "9"

Test #4:

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

input:

2 2 156542707

output:

43046970

result:

ok 1 number(s): "43046970"

Test #5:

score: 0
Accepted
time: 1ms
memory: 16824kb

input:

1 2 186225229

output:

9

result:

ok 1 number(s): "9"

Test #6:

score: 0
Accepted
time: 7ms
memory: 16776kb

input:

3 3 109884329

output:

100602209

result:

ok 1 number(s): "100602209"

Test #7:

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

input:

1 2 144802297

output:

9

result:

ok 1 number(s): "9"

Test #8:

score: 0
Accepted
time: 2ms
memory: 16824kb

input:

20 21992843 328859143

output:

110137213

result:

ok 1 number(s): "110137213"

Test #9:

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

input:

22 332524739 654888401

output:

410922781

result:

ok 1 number(s): "410922781"

Test #10:

score: 0
Accepted
time: 7ms
memory: 16972kb

input:

26 302215049 566649113

output:

221720840

result:

ok 1 number(s): "221720840"

Test #11:

score: 0
Accepted
time: 7ms
memory: 16796kb

input:

15 111009527 722130737

output:

648834664

result:

ok 1 number(s): "648834664"

Test #12:

score: 0
Accepted
time: 7ms
memory: 16976kb

input:

82 110032063 394529383

output:

111730592

result:

ok 1 number(s): "111730592"

Test #13:

score: 0
Accepted
time: 2ms
memory: 16864kb

input:

9 11172911 259650437

output:

68381774

result:

ok 1 number(s): "68381774"

Test #14:

score: 0
Accepted
time: 2ms
memory: 16864kb

input:

86 12016027 354886243

output:

263687778

result:

ok 1 number(s): "263687778"

Test #15:

score: 0
Accepted
time: 5ms
memory: 16736kb

input:

91 273689959 454097881

output:

114436127

result:

ok 1 number(s): "114436127"

Test #16:

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

input:

73 148878967 694206977

output:

176215101

result:

ok 1 number(s): "176215101"

Test #17:

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

input:

45 205982233 227598247

output:

156769598

result:

ok 1 number(s): "156769598"

Test #18:

score: 0
Accepted
time: 2ms
memory: 16980kb

input:

2778 122825869 147297463

output:

43419574

result:

ok 1 number(s): "43419574"

Test #19:

score: 0
Accepted
time: 2ms
memory: 16824kb

input:

289 7729669 589652893

output:

552952137

result:

ok 1 number(s): "552952137"

Test #20:

score: 0
Accepted
time: 2ms
memory: 16784kb

input:

2281 35651417 203950963

output:

21659018

result:

ok 1 number(s): "21659018"

Test #21:

score: 0
Accepted
time: 2ms
memory: 16720kb

input:

1684 258745639 373223677

output:

355596229

result:

ok 1 number(s): "355596229"

Test #22:

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

input:

2107 86850989 455823859

output:

245960059

result:

ok 1 number(s): "245960059"

Test #23:

score: 0
Accepted
time: 2ms
memory: 16784kb

input:

1323 43290799 791120419

output:

509649562

result:

ok 1 number(s): "509649562"

Test #24:

score: 0
Accepted
time: 2ms
memory: 16744kb

input:

2401 34064903 185314627

output:

70571452

result:

ok 1 number(s): "70571452"

Test #25:

score: 0
Accepted
time: 7ms
memory: 16764kb

input:

1073 82288187 564447959

output:

168200843

result:

ok 1 number(s): "168200843"

Test #26:

score: 0
Accepted
time: 0ms
memory: 16828kb

input:

1926 29995039 129122281

output:

60921463

result:

ok 1 number(s): "60921463"

Test #27:

score: 0
Accepted
time: 0ms
memory: 17008kb

input:

3000 66915659 765705179

output:

222619979

result:

ok 1 number(s): "222619979"

Test #28:

score: 0
Accepted
time: 246ms
memory: 42056kb

input:

998818 198334853 998244353

output:

153251445

result:

ok 1 number(s): "153251445"

Test #29:

score: 0
Accepted
time: 226ms
memory: 46364kb

input:

914379 128814383 998244353

output:

477606145

result:

ok 1 number(s): "477606145"

Test #30:

score: 0
Accepted
time: 235ms
memory: 42016kb

input:

944474 478445339 998244353

output:

174204073

result:

ok 1 number(s): "174204073"

Test #31:

score: 0
Accepted
time: 244ms
memory: 43284kb

input:

948637 711592127 998244353

output:

178256114

result:

ok 1 number(s): "178256114"

Test #32:

score: 0
Accepted
time: 242ms
memory: 42056kb

input:

927564 14465663 998244353

output:

315244613

result:

ok 1 number(s): "315244613"

Test #33:

score: 0
Accepted
time: 239ms
memory: 39612kb

input:

934615 392799073 998244353

output:

892700270

result:

ok 1 number(s): "892700270"

Test #34:

score: 0
Accepted
time: 235ms
memory: 39800kb

input:

917196 124972031 998244353

output:

782017412

result:

ok 1 number(s): "782017412"

Test #35:

score: 0
Accepted
time: 245ms
memory: 43316kb

input:

957149 392606173 998244353

output:

159348443

result:

ok 1 number(s): "159348443"

Test #36:

score: 0
Accepted
time: 241ms
memory: 46756kb

input:

997042 184649453 998244353

output:

464643024

result:

ok 1 number(s): "464643024"

Test #37:

score: 0
Accepted
time: 233ms
memory: 46536kb

input:

953353 14071961 998244353

output:

391688875

result:

ok 1 number(s): "391688875"

Test #38:

score: 0
Accepted
time: 2553ms
memory: 277400kb

input:

9909956 720431399 720431401

output:

86883659

result:

ok 1 number(s): "86883659"

Test #39:

score: 0
Accepted
time: 2455ms
memory: 277268kb

input:

9924163 267052829 267052831

output:

75754681

result:

ok 1 number(s): "75754681"

Test #40:

score: 0
Accepted
time: 2424ms
memory: 277600kb

input:

9967885 197873129 197873131

output:

16653739

result:

ok 1 number(s): "16653739"

Test #41:

score: 0
Accepted
time: 2104ms
memory: 277688kb

input:

9952642 101872151 101872153

output:

0

result:

ok 1 number(s): "0"

Test #42:

score: 0
Accepted
time: 2448ms
memory: 277644kb

input:

9955909 167874431 167874433

output:

130012020

result:

ok 1 number(s): "130012020"

Test #43:

score: 0
Accepted
time: 2467ms
memory: 277880kb

input:

9994785 399509567 399509569

output:

153324498

result:

ok 1 number(s): "153324498"

Test #44:

score: 0
Accepted
time: 2417ms
memory: 277432kb

input:

9954011 108819131 108819133

output:

101671540

result:

ok 1 number(s): "101671540"

Test #45:

score: 0
Accepted
time: 2464ms
memory: 277924kb

input:

9997570 213315827 213315829

output:

57441081

result:

ok 1 number(s): "57441081"

Test #46:

score: 0
Accepted
time: 2481ms
memory: 277864kb

input:

9995867 113028299 113028301

output:

67837072

result:

ok 1 number(s): "67837072"

Test #47:

score: 0
Accepted
time: 2426ms
memory: 277092kb

input:

9909335 247275617 247275619

output:

202966817

result:

ok 1 number(s): "202966817"

Test #48:

score: 0
Accepted
time: 2542ms
memory: 276716kb

input:

9921815 38466881 725310841

output:

601117286

result:

ok 1 number(s): "601117286"

Test #49:

score: 0
Accepted
time: 2509ms
memory: 276744kb

input:

9919464 4830599 747345523

output:

168521454

result:

ok 1 number(s): "168521454"

Test #50:

score: 0
Accepted
time: 2484ms
memory: 277892kb

input:

9981374 3616373 154722097

output:

2696288

result:

ok 1 number(s): "2696288"

Test #51:

score: 0
Accepted
time: 2537ms
memory: 277068kb

input:

9906664 12433457 558159149

output:

538699014

result:

ok 1 number(s): "538699014"

Test #52:

score: 0
Accepted
time: 2485ms
memory: 277960kb

input:

9985736 46853 410275823

output:

258567756

result:

ok 1 number(s): "258567756"

Test #53:

score: 0
Accepted
time: 2462ms
memory: 277484kb

input:

9962926 33790087 203505083

output:

40932778

result:

ok 1 number(s): "40932778"

Test #54:

score: 0
Accepted
time: 2433ms
memory: 276852kb

input:

9903735 146658401 157137433

output:

154493145

result:

ok 1 number(s): "154493145"

Test #55:

score: 0
Accepted
time: 2434ms
memory: 276904kb

input:

9913516 105010771 110717611

output:

67979325

result:

ok 1 number(s): "67979325"

Test #56:

score: 0
Accepted
time: 2514ms
memory: 277616kb

input:

9953517 268142489 675913921

output:

523115756

result:

ok 1 number(s): "523115756"

Test #57:

score: 0
Accepted
time: 2413ms
memory: 277664kb

input:

9981005 11993207 114120883

output:

7261617

result:

ok 1 number(s): "7261617"

Test #58:

score: 0
Accepted
time: 2493ms
memory: 277520kb

input:

9945956 36522077 168104303

output:

82398556

result:

ok 1 number(s): "82398556"

Test #59:

score: 0
Accepted
time: 2554ms
memory: 277548kb

input:

9967933 15301477 352827883

output:

242773007

result:

ok 1 number(s): "242773007"

Test #60:

score: 0
Accepted
time: 2449ms
memory: 277312kb

input:

9911781 83845891 360130933

output:

158254305

result:

ok 1 number(s): "158254305"

Test #61:

score: 0
Accepted
time: 2421ms
memory: 276964kb

input:

9916390 100404191 108138473

output:

103346432

result:

ok 1 number(s): "103346432"

Test #62:

score: 0
Accepted
time: 2497ms
memory: 277568kb

input:

9974438 7828049 430399297

output:

76675277

result:

ok 1 number(s): "76675277"