QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#825196#8428. Partition into TeamsYangHHaoAC ✓15ms14416kbC++171.5kb2024-12-21 17:41:282024-12-21 17:41:39

Judging History

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

  • [2024-12-21 17:41:39]
  • 评测
  • 测评结果:AC
  • 用时:15ms
  • 内存:14416kb
  • [2024-12-21 17:41:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
/*
 * Author: Yang
 * Date: 2024-12-03
 */
//#pragma GCC optimize(2)
using ll=long long;
using uint=unsigned int;
using ull=unsigned ll;
using pii=pair<int,int>;
#define all(v) begin(v),end(v)
#define fst first
#define scd second
#define rep(i,s,t) for(int i=(s);i<=(t);++i)
#define irep(i,t,s) for(int i=(t);i>=(s);--i)
template<typename T>
bool chmax(T &x,T y){return x<y?(x=y,1):0;}
template<typename T>
bool chmin(T &x,T y){return y<x?(x=y,1):0;}
const int Pmx=1e6+10;
int inv[Pmx];
int fac[Pmx],ifac[Pmx];
void work(){
    ll n,n0;int p;
    cin>>n>>p;
    n0=n;
    inv[1]=1;
    rep(i,2,p-1) inv[i]=1ll*(p-p/i)*inv[p%i]%p;
    fac[0]=ifac[0]=1;

    auto C=[&](int x,int y)->int {
        if(y<0||y>x) return 0;
        return 1ll*fac[x]*ifac[y]%p*ifac[x-y]%p;
    };
    auto qp=[&](int a,ll b){
        int r=1;
        while(b){
            if(b&1) r=1ll*r*a%p;
            a=1ll*a*a%p;b>>=1;
        }
        return r;
    };

    rep(i,1,p-1){
        fac[i]=1ll*fac[i-1]*i%p;
        ifac[i]=1ll*ifac[i-1]*inv[i]%p;
    }
    vector<int> dn;
    while(n){
        dn.push_back(n%p),n/=p;
    }
    int ans=1;
    for(int x:dn){
        int cur=0;
        rep(i,0,min(x/2,(p-1)/2)){
            // cout<<x<<": "<<i<<' '<<1ll*C(x,2*i)*C(2*i,i)%p<<"\n";
            (cur+=1ll*C(x,2*i)*C(2*i,i)%p)%=p;
        }
        // cout<<x<<' '<<cur<<'\n';
        ans=1ll*ans*cur%p;
    }
    // cout<<ans<<"\n";
    cout<<1ll*(qp(3,n0)+p-ans)*inv[2]%p<<"\n";
}
signed main(){
    //ios::sync_with_stdio(false);
    work();
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 7684kb

input:

5 5

output:

1

result:

ok 1 number(s): "1"

Test #2:

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

input:

5 7

output:

5

result:

ok 1 number(s): "5"

Test #3:

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

input:

789 97

output:

53

result:

ok 1 number(s): "53"

Test #4:

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

input:

98 23

output:

10

result:

ok 1 number(s): "10"

Test #5:

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

input:

398 7

output:

4

result:

ok 1 number(s): "4"

Test #6:

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

input:

272 31

output:

18

result:

ok 1 number(s): "18"

Test #7:

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

input:

920 199

output:

39

result:

ok 1 number(s): "39"

Test #8:

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

input:

390 5167

output:

1236

result:

ok 1 number(s): "1236"

Test #9:

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

input:

445 24337

output:

4546

result:

ok 1 number(s): "4546"

Test #10:

score: 0
Accepted
time: 6ms
memory: 14032kb

input:

28 586501

output:

269032

result:

ok 1 number(s): "269032"

Test #11:

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

input:

304 5

output:

0

result:

ok 1 number(s): "0"

Test #12:

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

input:

7158 41

output:

16

result:

ok 1 number(s): "16"

Test #13:

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

input:

8487 331

output:

148

result:

ok 1 number(s): "148"

Test #14:

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

input:

501 6763

output:

363

result:

ok 1 number(s): "363"

Test #15:

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

input:

3011 61129

output:

49319

result:

ok 1 number(s): "49319"

Test #16:

score: 0
Accepted
time: 6ms
memory: 9012kb

input:

7266 358159

output:

7643

result:

ok 1 number(s): "7643"

Test #17:

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

input:

360860 7

output:

1

result:

ok 1 number(s): "1"

Test #18:

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

input:

154939 19

output:

8

result:

ok 1 number(s): "8"

Test #19:

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

input:

813268 47

output:

40

result:

ok 1 number(s): "40"

Test #20:

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

input:

965601 1531

output:

1147

result:

ok 1 number(s): "1147"

Test #21:

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

input:

689332 78079

output:

17208

result:

ok 1 number(s): "17208"

Test #22:

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

input:

287719 34369

output:

15373

result:

ok 1 number(s): "15373"

Test #23:

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

input:

439237583 7

output:

6

result:

ok 1 number(s): "6"

Test #24:

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

input:

319142531 79

output:

21

result:

ok 1 number(s): "21"

Test #25:

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

input:

592330255 631

output:

530

result:

ok 1 number(s): "530"

Test #26:

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

input:

164278463 4517

output:

3038

result:

ok 1 number(s): "3038"

Test #27:

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

input:

753671285 65371

output:

22369

result:

ok 1 number(s): "22369"

Test #28:

score: 0
Accepted
time: 15ms
memory: 14048kb

input:

289665297 663127

output:

168503

result:

ok 1 number(s): "168503"

Test #29:

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

input:

350585676619 7

output:

5

result:

ok 1 number(s): "5"

Test #30:

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

input:

4283693890775 31

output:

1

result:

ok 1 number(s): "1"

Test #31:

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

input:

1234727503131 151

output:

67

result:

ok 1 number(s): "67"

Test #32:

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

input:

9399186742989 1327

output:

678

result:

ok 1 number(s): "678"

Test #33:

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

input:

1409645481800 8719

output:

1939

result:

ok 1 number(s): "1939"

Test #34:

score: 0
Accepted
time: 11ms
memory: 14416kb

input:

3782178721166 552859

output:

458256

result:

ok 1 number(s): "458256"

Test #35:

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

input:

827235816874722972 5

output:

2

result:

ok 1 number(s): "2"

Test #36:

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

input:

648249960855836992 61

output:

56

result:

ok 1 number(s): "56"

Test #37:

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

input:

467138518199434341 563

output:

202

result:

ok 1 number(s): "202"

Test #38:

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

input:

616207494477524619 653

output:

95

result:

ok 1 number(s): "95"

Test #39:

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

input:

129689263663821851 34403

output:

30422

result:

ok 1 number(s): "30422"

Test #40:

score: 0
Accepted
time: 11ms
memory: 13480kb

input:

605206340470214709 479971

output:

381982

result:

ok 1 number(s): "381982"