QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#67476#4127. 方程alpha1022100 ✓269ms1796kbC++232.2kb2022-12-10 16:19:532022-12-10 16:19:56

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-10 16:19:56]
  • Judged
  • Verdict: 100
  • Time: 269ms
  • Memory: 1796kb
  • [2022-12-10 16:19:53]
  • Submitted

answer

#include <cmath>
#include <cstdio>
using namespace std;
const int N = 16;
const int MX = 1e6;
const int LG = 30;
int T,mod;
int n,n1,n2,m;
int a[N + 5];
int fpow(int a,int b,int mod)
{
	int ret = 1;
	for(;b;b >>= 1)
		(b & 1) && (ret = (long long)ret * a % mod),a = (long long)a * a % mod;
	return ret;
}
int exgcd(int a,int b,int &x,int &y,int mod)
{
	if(!b)
	{
		x = 1,y = 0;
		return a;
	}
	int X,Y,ret = exgcd(b,a % b,X,Y,mod);
	x = Y,y = X - a / b * Y;
	return ret;
}
int inv(int a,int mod)
{
	int x,y;
	exgcd(a,mod,x,y,mod);
	return (x % mod + mod) % mod;
}
int f[MX + 5];
int fac(int n,int p,int mod)
{
	if(!n)
		return 1;
	return (long long)fpow(f[mod - 1],n / mod,mod) * f[n % mod] % mod * fac(n / p,p,mod) % mod;
}
int C(int n,int m,int p,int mod)
{
	if(n < m)
		return 0;
	f[0] = 1;
	for(register int i = 1;i < mod;++i)
		(i % p) ? (f[i] = (long long)f[i - 1] * i % mod) : (f[i] = f[i - 1]);
	int cnt = 0;
	for(register int i = n;i;i /= p)
		cnt += i / p;
	for(register int i = m;i;i /= p)
		cnt -= i / p;
	for(register int i = n - m;i;i /= p)
		cnt -= i / p;
	return (long long)fac(n,p,mod) * inv(fac(m,p,mod),mod) % mod * inv(fac(n - m,p,mod),mod) % mod * fpow(p,cnt,mod) % mod;
}
int C(int n,int m,int mod)
{
	int bound = sqrt(mod);
	int a[LG + 5],c[LG + 5],tot = 0;
	int M = 1,ans = 0;
	for(register int i = 2;i <= bound && mod > 1;++i)
	{
		int prod = 1;
		for(;!(mod % i);mod /= i,prod *= i);
		prod > 1 && (a[++tot] = C(n,m,i,prod),c[tot] = prod);
	}
	mod > 1 && (a[++tot] = C(n,m,mod,mod),c[tot] = mod);
	for(register int i = 1;i <= tot;++i)
		M *= c[i];
	for(register int i = 1;i <= tot;++i)
		ans = (ans + (long long)a[i] * (M / c[i]) % M * inv(M / c[i],c[i])) % M;
	return ans;
}
int coe = 1,ans;
void dfs(int k)
{
	if(k > n1)
	{
		ans = (ans + (long long)coe * C(m - 1,n - 1,mod)) % mod;
		return ;
	}
	dfs(k + 1),m -= a[k],coe = (long long)coe * (mod - 1) % mod,dfs(k + 1),m += a[k],coe = (long long)coe * (mod - 1) % mod;
}
int main()
{
	scanf("%d%d",&T,&mod);
	for(;T;--T)
	{
		scanf("%d%d%d%d",&n,&n1,&n2,&m),ans = 0;
		for(register int i = 1;i <= n1 + n2;++i)
			scanf("%d",a + i),i > n1 && (m -= a[i] - 1);
		dfs(1);
		printf("%d\n",ans);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 10
Accepted
time: 269ms
memory: 1676kb

input:

5 262203414
10898223 8 8 390259367
14139 9332 23904 12998 27720 9232 23420 13792 356 2165 1813 1772 485 1392 1227 1498
103775313 8 8 108251091
26418 5665 10714 30224 126 17520 32150 30497 1631 655 1397 2019 1111 307 517 603
367525289 8 8 806544773
28693 23759 7185 3565 10658 16920 14246 21783 451 12...

output:

117605862
106727016
163794576
55950048
247012788

result:

ok 5 lines

Test #2:

score: 10
Accepted
time: 2ms
memory: 1732kb

input:

5 262203414
21 0 0 27

12 0 0 48

24 0 0 50

15 0 0 48

13 0 0 31


output:

230230
111708293
212964870
254929767
86493225

result:

ok 5 lines

Test #3:

score: 10
Accepted
time: 5ms
memory: 1728kb

input:

5 10007
5045 8 8 8840
6234 4214 3760 6591 3936 7142 273 5845 592 145 319 141 345 655 255 452
3182 8 8 9761
2179 423 820 202 3685 3493 4641 6131 505 461 191 397 168 649 502 612
7012 8 8 9545
292 6867 5890 6789 7727 7885 4523 7385 472 290 612 487 172 627 74 49
1528 8 8 5679
4318 1660 1530 4993 3853 12...

output:

3686
8655
0
4457
4465

result:

ok 5 lines

Test #4:

score: 10
Accepted
time: 2ms
memory: 1728kb

input:

5 262203414
714391128 0 8 847759954
1990 1114 535 2450 3193 1386 3238 558
675496043 0 8 707578651
3090 2420 1963 3264 687 2740 687 2192
41770109 0 8 732728517
1534 1532 2476 2390 2305 1196 1038 303
60104522 0 8 97218534
1273 832 3000 680 2806 2525 2097 363
159404337 0 8 167493776
3162 3211 965 2049 ...

output:

177780570
0
258240642
194366436
25802139

result:

ok 5 lines

Test #5:

score: 10
Accepted
time: 2ms
memory: 1732kb

input:

5 10007
6020 0 0 8833

8025 0 0 8312

3092 0 0 9859

652 0 0 7142

928 0 0 9955

output:

2559
5171
9461
7973
7769

result:

ok 5 lines

Test #6:

score: 10
Accepted
time: 2ms
memory: 1632kb

input:

5 437367875
10996832 0 0 56131039

55460732 0 0 198748899

8075397 0 0 83942175

428376447 0 0 594802602

194841132 0 0 397293164


output:

303126250
277144000
47634125
0
389733750

result:

ok 5 lines

Test #7:

score: 10
Accepted
time: 178ms
memory: 1580kb

input:

5 10007
125019792 8 8 806926910
19440 12618 1054 19561 5351 27708 21762 2771 912 1901 2542 435 3254 2709 3115 1961
444587613 8 8 881486321
9218 8404 18044 27030 19140 29628 20351 17312 2833 1592 2683 1530 1206 879 2741 1919
418759179 8 8 745952011
3610 27797 8539 2516 5562 21768 5452 9313 2667 2650 ...

output:

0
4215
4466
2915
9114

result:

ok 5 lines

Test #8:

score: 10
Accepted
time: 206ms
memory: 1796kb

input:

5 437367875
214718285 8 8 456385616
18080 22923 14695 15725 15931 28188 31942 5712 2247 333 2165 296 1305 1394 2512 491
273784947 8 8 627451865
12315 24085 20752 19928 12555 10944 998 21782 3208 2358 3080 3182 800 1628 1670 2295
214051948 8 8 335898352
19059 10260 29763 31734 3533 21604 26488 10272 ...

output:

79061500
251156801
284922750
108974530
192887275

result:

ok 5 lines

Test #9:

score: 10
Accepted
time: 2ms
memory: 1556kb

input:

5 10007
5 0 0 10

5 1 0 10
7
5 1 1 10
6 2
4 0 0 9

4 1 1 9
6 2

output:

126
126
70
56
35

result:

ok 5 lines

Test #10:

score: 10
Accepted
time: 7ms
memory: 1656kb

input:

5 437367875
25 8 8 50
4 6 2 7 25 7 22 13 2 2 1 1 1 1 2 1
22 8 8 34
9 12 5 24 5 7 15 21 1 1 1 1 1 1 1 1
29 8 8 47
23 28 45 17 44 12 15 46 1 3 3 3 1 2 2 1
20 8 8 42
5 22 15 6 32 37 25 40 3 1 1 1 1 2 2 2
20 8 8 35
11 17 31 33 1 18 17 1 2 1 1 2 1 3 1 1

output:

118744288
352381690
35365881
91936040
21474179

result:

ok 5 lines