QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#822820 | #8428. Partition into Teams | IvanZhang2009 | AC ✓ | 27ms | 18088kb | C++20 | 3.6kb | 2024-12-20 16:51:35 | 2024-12-20 16:51:42 |
Judging History
answer
/*
* __----~~~~~~~~~~~------___
* . . ~~//====...... __--~ ~~
* -. \_|// |||\\ ~~~~~~::::... /~
* ___-==_ _-~o~ \/ ||| \\ _/~~-
* __---~~~.==~||\=_ -_--~/_-~|- |\\ \\ _/~
* _-~~ .=~ | \\-_ '-~7 /- / || \ /
* .~ .~ | \\ -_ / /- / || \ /
* / ____ / | \\ ~-_/ /|- _/ .|| \ /
* |~~ ~~|--~~~~--_ \ ~==-/ | \~--===~~ .\
* ' ~-| /| |-~\~~ __--~~
* |-~~-_/ | | ~\_ _-~ /\
* / \ \__ \/~ \__
* _--~ _/ | .-~~____--~-/ ~~==.
* ((->/~ '.|||' -_| ~~-/ , . _||
* -_ ~\ ~~---l__i__i__i--~~_/
* _-~-__ ~) \--______________--~~
* //.-~~~-~_--~- |-------~~~~~~~~
* //.-~~~--\
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*
* 神兽保佑 永无BUG
*/
/*
* @Description: I want to be the weakest vegetable in the world!
* @Author: I.Z.
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
int MOD;
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define pb push_back
#define REP(i,b,e) for(int i=(b);i<(int)(e);++i)
#define over(x) {cout<<(x)<<endl;return;}
#define lowbit(x) ((x)&(-(x)))
#define cntbit(x) __builtin_popcount(x)
#define deal(v) sort(all(v));v.erase(unique(v.begin(),v.end()),v.end())
#define lbound(v,x) lower_bound(all(v),x)-v.begin()
mt19937 sd(random_device{}());
int qpow(int a,int b,int m=MOD,int res=1){
a%=m;
while(b>0)res=(b&1)?(res*a%m):(res),a=a*a%m,b>>=1;
return res;
}
int exgcd(int x,int y,int &a,int &b){
if(y==0){
a=1;b=0;
return x;
}
int d=exgcd(y,x%y,a,b);
int z=b;
b=a-b*(x/y);
a=z;
return d;
}
int _x_,_y_;
inline int inverse(int x,int y=MOD){
exgcd(x,y,_x_,_y_);
return (_x_+y)%y;
}
int fac[2000005],inv[2000005];
void init(int n){
fac[0]=inv[0]=1;
REP(i,1,n+1)fac[i]=fac[i-1]*i%MOD;
inv[n]=qpow(fac[n],MOD-2);
for(int i=n-1;i>=1;--i)inv[i]=inv[i+1]*(i+1)%MOD;
}
int binom(int x,int y){
return x<y||y<0? 0:fac[x]*inv[y]%MOD*inv[x-y]%MOD;
}
int n;
int calc(int x,int y){
if(x<y||y<0)return 0;
if(x<MOD)return binom(x,y);
else if(!y||y==x)return 1;
else return calc(x%MOD,y%MOD)*calc(x/MOD,y/MOD)%MOD;
}
map<pii,int>mp;
int solve(int n,int y){//求 sum C(n,2x)*C(2x+y,x)
if(mp.find({n,y})!=mp.end())return mp[{n,y}];
if(n<MOD){
int ans=0;
REP(i,0,n/2+1)(ans+=calc(n,2*i)*calc(2*i+y,i))%=MOD;
return mp[{n,y}]=ans;
}
int ans=0;
REP(i,0,MOD){
//枚举 x%i
int j=i*2%MOD;//x=ap+i, 2x+y=2ap+2i+y
int co=calc(n%MOD,j)*calc((j+y)%MOD,i)%MOD;
int c=solve(n/MOD,(j+y)/MOD);
(ans+=c*co)%=MOD;
}
return mp[{n,y}]=ans;
}
void Main() {
cin>>n>>MOD;
init(min(n,MOD-1));
int ans=solve(n,0);
ans=qpow(3,n)+MOD-ans;ans*=(MOD+1)/2;
ans%=MOD;
over(ans)
}
void TC() {
int tc=1;
while(tc--){
Main();
cout.flush();
}
}
signed main() {
return cin.tie(0),cout.tie(0),ios::sync_with_stdio(0),TC(),0;
}
/*
1. CLEAR the arrays (ESPECIALLY multitests)
2. DELETE useless output
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5576kb
input:
5 5
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 1ms
memory: 5636kb
input:
5 7
output:
5
result:
ok 1 number(s): "5"
Test #3:
score: 0
Accepted
time: 0ms
memory: 5548kb
input:
789 97
output:
53
result:
ok 1 number(s): "53"
Test #4:
score: 0
Accepted
time: 0ms
memory: 5660kb
input:
98 23
output:
10
result:
ok 1 number(s): "10"
Test #5:
score: 0
Accepted
time: 1ms
memory: 5608kb
input:
398 7
output:
4
result:
ok 1 number(s): "4"
Test #6:
score: 0
Accepted
time: 0ms
memory: 5664kb
input:
272 31
output:
18
result:
ok 1 number(s): "18"
Test #7:
score: 0
Accepted
time: 1ms
memory: 5836kb
input:
920 199
output:
39
result:
ok 1 number(s): "39"
Test #8:
score: 0
Accepted
time: 1ms
memory: 5864kb
input:
390 5167
output:
1236
result:
ok 1 number(s): "1236"
Test #9:
score: 0
Accepted
time: 1ms
memory: 5612kb
input:
445 24337
output:
4546
result:
ok 1 number(s): "4546"
Test #10:
score: 0
Accepted
time: 1ms
memory: 5804kb
input:
28 586501
output:
269032
result:
ok 1 number(s): "269032"
Test #11:
score: 0
Accepted
time: 1ms
memory: 5584kb
input:
304 5
output:
0
result:
ok 1 number(s): "0"
Test #12:
score: 0
Accepted
time: 0ms
memory: 5872kb
input:
7158 41
output:
16
result:
ok 1 number(s): "16"
Test #13:
score: 0
Accepted
time: 1ms
memory: 5664kb
input:
8487 331
output:
148
result:
ok 1 number(s): "148"
Test #14:
score: 0
Accepted
time: 0ms
memory: 5584kb
input:
501 6763
output:
363
result:
ok 1 number(s): "363"
Test #15:
score: 0
Accepted
time: 1ms
memory: 5672kb
input:
3011 61129
output:
49319
result:
ok 1 number(s): "49319"
Test #16:
score: 0
Accepted
time: 0ms
memory: 5636kb
input:
7266 358159
output:
7643
result:
ok 1 number(s): "7643"
Test #17:
score: 0
Accepted
time: 1ms
memory: 5608kb
input:
360860 7
output:
1
result:
ok 1 number(s): "1"
Test #18:
score: 0
Accepted
time: 1ms
memory: 5620kb
input:
154939 19
output:
8
result:
ok 1 number(s): "8"
Test #19:
score: 0
Accepted
time: 0ms
memory: 5584kb
input:
813268 47
output:
40
result:
ok 1 number(s): "40"
Test #20:
score: 0
Accepted
time: 0ms
memory: 5676kb
input:
965601 1531
output:
1147
result:
ok 1 number(s): "1147"
Test #21:
score: 0
Accepted
time: 5ms
memory: 8420kb
input:
689332 78079
output:
17208
result:
ok 1 number(s): "17208"
Test #22:
score: 0
Accepted
time: 2ms
memory: 6072kb
input:
287719 34369
output:
15373
result:
ok 1 number(s): "15373"
Test #23:
score: 0
Accepted
time: 1ms
memory: 5596kb
input:
439237583 7
output:
6
result:
ok 1 number(s): "6"
Test #24:
score: 0
Accepted
time: 1ms
memory: 5532kb
input:
319142531 79
output:
21
result:
ok 1 number(s): "21"
Test #25:
score: 0
Accepted
time: 1ms
memory: 5664kb
input:
592330255 631
output:
530
result:
ok 1 number(s): "530"
Test #26:
score: 0
Accepted
time: 1ms
memory: 5628kb
input:
164278463 4517
output:
3038
result:
ok 1 number(s): "3038"
Test #27:
score: 0
Accepted
time: 4ms
memory: 7740kb
input:
753671285 65371
output:
22369
result:
ok 1 number(s): "22369"
Test #28:
score: 0
Accepted
time: 27ms
memory: 18088kb
input:
289665297 663127
output:
168503
result:
ok 1 number(s): "168503"
Test #29:
score: 0
Accepted
time: 1ms
memory: 5652kb
input:
350585676619 7
output:
5
result:
ok 1 number(s): "5"
Test #30:
score: 0
Accepted
time: 1ms
memory: 5572kb
input:
4283693890775 31
output:
1
result:
ok 1 number(s): "1"
Test #31:
score: 0
Accepted
time: 1ms
memory: 5584kb
input:
1234727503131 151
output:
67
result:
ok 1 number(s): "67"
Test #32:
score: 0
Accepted
time: 0ms
memory: 5812kb
input:
9399186742989 1327
output:
678
result:
ok 1 number(s): "678"
Test #33:
score: 0
Accepted
time: 2ms
memory: 5740kb
input:
1409645481800 8719
output:
1939
result:
ok 1 number(s): "1939"
Test #34:
score: 0
Accepted
time: 27ms
memory: 15588kb
input:
3782178721166 552859
output:
458256
result:
ok 1 number(s): "458256"
Test #35:
score: 0
Accepted
time: 0ms
memory: 5604kb
input:
827235816874722972 5
output:
2
result:
ok 1 number(s): "2"
Test #36:
score: 0
Accepted
time: 0ms
memory: 5600kb
input:
648249960855836992 61
output:
56
result:
ok 1 number(s): "56"
Test #37:
score: 0
Accepted
time: 1ms
memory: 5660kb
input:
467138518199434341 563
output:
202
result:
ok 1 number(s): "202"
Test #38:
score: 0
Accepted
time: 0ms
memory: 5676kb
input:
616207494477524619 653
output:
95
result:
ok 1 number(s): "95"
Test #39:
score: 0
Accepted
time: 0ms
memory: 7892kb
input:
129689263663821851 34403
output:
30422
result:
ok 1 number(s): "30422"
Test #40:
score: 0
Accepted
time: 27ms
memory: 13760kb
input:
605206340470214709 479971
output:
381982
result:
ok 1 number(s): "381982"