QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#101355#5031. 核zhoukangyang100 ✓1585ms237912kbC++174.5kb2023-04-29 11:07:482023-04-29 11:07:51

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-29 11:07:51]
  • 评测
  • 测评结果:100
  • 用时:1585ms
  • 内存:237912kb
  • [2023-04-29 11:07:48]
  • 提交

answer

#include<bits/stdc++.h>
#define L(i, j, k) for(int i = (j); i <= (k); ++i)
#define R(i, j, k) for(int i = (j); i >= (k); --i)
#define ll long long
#define vi vector <int>
#define sz(a) ((int) (a).size())
#define me(f, x) memset(f, x, sizeof(f))
#define uint unsigned int
#define ull unsigned long long 
#define bs bitset < N >
using namespace std;
const int N = 1e7 + 7;
int n, q, mod;

struct fastmod {
  typedef unsigned long long u64;
  typedef __uint128_t u128;

  int m;
  u64 b;

  fastmod(int m) : m(m), b(((u128)1 << 64) / m) {}
  int reduce(u64 a) {
    u64 q = ((u128)a * b) >> 64;
    int r = a - q * m;
    return r < m ? r : r - m;
  }
} z(2);

int pw[N], fac[N], ifac[N];
int qpow(int x, int y = mod - 2) {
	int res = 1;
	for(; y; x = (ll) x * x % mod, y >>= 1) if(y & 1) res = (ll) res * x % mod;
	return res;
}
void q_init(int x) {
	pw[0] = 1;
	L(i, 1, x) pw[i] = (ll) pw[i - 1] * q % mod;
	L(i, 1, x) fac[i] = pw[i] - 1;
	fac[0] = ifac[0] = 1;
	L(i, 1, x) 
		fac[i] = (ll) fac[i - 1] * fac[i] % mod;
	ifac[x] = qpow(fac[x]);
	R(i, x - 1, 0) 
		ifac[i] = (ll) ifac[i + 1] * (pw[i + 1] - 1) % mod;
} 
int q_C(int x, int y) {
	return x < y || y < 0 ? 0 : (ll) fac[x] * ifac[y] % mod * ifac[x - y] % mod;
}
inline int sgn(int x) {
	return (x & 1) ? mod - 1 : 1;
}

template < int L, int K > struct DBM {
	static const int R = (L + 1) * (K + 1);
	
	int lpos, a[R * 10 + 7];
	int G[R + 7][R + 7], cur[R + 7], ns[R + 7];
	int f[L + 2][K + 2], id[L + 2][K + 2], idt;
	inline void insert() {
		R(i, idt, 1) if(cur[i]) {
			if(!G[i][i]) {
				L(j, 1, i) 
					G[i][j] = cur[j];
			} 
			int iv = (ll) (mod - cur[i]) * qpow(G[i][i]) % mod;
			L(j, 1, i) 
				(cur[j] += (ll) iv * G[i][j] % mod) %= mod;
		}
	}
	void gauss(int *a, int n, int lpos = 0) {
		n = min(n, R * 10), idt = 0;
		L(i, 0, L) 
			L(j, 0, K) 
				id[i][j] = ++idt;
		L(i, L + 1, n) {
			L(j, 0, idt) 
				cur[j] = 0;
			L(x, 0, L) if(x < i) {
				int pw = a[i - x];
				L(y, 0, K) 
					cur[id[x][y]] = pw, 
					pw = (ll) pw * ::pw[lpos + i] % mod;
			}
			insert();
		}
		int pos = 1;
		while(G[pos][pos]) ++pos;
		ns[pos] = 1;
		if(pos > idt) {
			cout << "TAT" << endl;
			exit(0);
		}
		L(i, pos + 1, idt) {
			int s = 0;
			L(j, 1, i - 1) {
				(s += (ll) G[i][j] * ns[j] % mod) %= mod; 
			}
			ns[i] = (ll) (mod - s) * qpow(G[i][i]) % mod;
		}
		L(i, 0, L) 
			L(j, 0, K) 
				f[i][j] = ns[id[i][j]];
		L(i, L + 1, n) {
			L(j, 0, idt) 
				cur[j] = 0;
			int s = 0;
			L(x, 0, L) if(x < i) {
				int pw = a[i - x];
				L(y, 0, K) 
					(s += (ll) pw * f[x][y] % mod) %= mod, 
					pw = (ll) pw * ::pw[lpos + i] % mod;
			}
		}
	}
};

DBM < 2, 3 > S;

int f[N], g[N], ans[N], T[N], suf[N];
int Pw1[N], Pw2[N];
int main() {
	ios :: sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin >> n >> q >> mod;
//	n = 1e7, q = 998244353, mod = 1e9 + 7;
	z = fastmod(mod);
	q_init(n + 1);
//	cout << clock() << endl;
	int mn = min(n, 80);
	f[0] = 1;
	L(i, 1, mn) {
		f[i] = (ll) f[i - 1] * (pw[n] + mod - pw[n - i]) % mod;
	}
	L(i, 0, mn) {
		g[i] = (ll) sgn(i) * qpow(q, i * (i - 1) / 2) % mod;
	}
	L(i, 0, mn) {
		L(j, 0, mn - i) {
			(ans[i + j] += (ll) q_C(i + j, i) * f[i] % mod * g[j] % mod) %= mod; 
		}
	}
	L(i, 0, n) {
		ans[i] = (ll) ans[i] * q_C(n, i) % mod;
	}
	if(mn < n) S.gauss(ans, mn);
	
	L(i, mn + 1, n) {
		int pw = 1;
		unsigned long long t = 0;
		L(y, 0, 3) t += (ll) pw * S.f[0][y], pw = z.reduce((ull) pw * ::pw[i]);
		T[i] = t % mod;
	}
	suf[n + 1] = 1;
	R(i, n, mn + 1) 
		suf[i] = z.reduce((ll) suf[i + 1] * T[i]);
	int all = qpow(suf[mn + 1]);
	L(i, mn + 1, n) {
		unsigned long long s = 0;
		L(x, 1, 2) {
			int pw = ans[i - x];
			s += (ull) pw * S.f[x][0];
			pw = z.reduce((ull) pw * ::pw[i]);
			s += (ull) pw * S.f[x][1];
			pw = z.reduce((ull) pw * ::pw[i]);
			s += (ull) pw * S.f[x][2];
			pw = z.reduce((ull) pw * ::pw[i]);
			s += (ull) pw * S.f[x][3];
		}
		ans[i] = z.reduce((ull)z.reduce((ull) (mod - z.reduce(s)) * all) * suf[i + 1]);
		all = z.reduce((ull) all * T[i]);
	}
	int pw = 1;
	L(i, 1, n) {
		pw = (ll) pw * q % (mod - 1);
	}
	const int K = sqrt(mod) + 1;
	Pw1[0] = Pw2[0] = 1;
	L(i, 1, K) {
		Pw1[i] = (ll) Pw1[i - 1] * 3 % mod;
	}
	L(i, 1, K) {
		Pw2[i] = (ll) Pw2[i - 1] * Pw1[K] % mod;
	}
	int s = 1;
	ull ns = 0;
	R(i, n, 0) {
		int w = (ll) Pw1[s % K] * Pw2[s / K] % mod;
		ns += (ull) ans[i] * w;
		if(!(i & 16)) ns %= mod;
		s = (ll) s * pw % (mod - 1);
	}
	ns %= mod;
	cout << ns << '\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 3584kb

input:

1 2 540053233

output:

9

result:

ok 1 number(s): "9"

Test #2:

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

input:

2 2 156542707

output:

43046970

result:

ok 1 number(s): "43046970"

Test #3:

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

input:

1 2 186225229

output:

9

result:

ok 1 number(s): "9"

Test #4:

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

input:

3 3 109884329

output:

100602209

result:

ok 1 number(s): "100602209"

Test #5:

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

input:

1 2 144802297

output:

9

result:

ok 1 number(s): "9"

Subtask #2:

score: 20
Accepted

Dependency #1:

100%
Accepted

Test #6:

score: 20
Accepted
time: 2ms
memory: 3544kb

input:

20 21992843 328859143

output:

110137213

result:

ok 1 number(s): "110137213"

Test #7:

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

input:

22 332524739 654888401

output:

410922781

result:

ok 1 number(s): "410922781"

Test #8:

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

input:

26 302215049 566649113

output:

221720840

result:

ok 1 number(s): "221720840"

Test #9:

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

input:

15 111009527 722130737

output:

648834664

result:

ok 1 number(s): "648834664"

Test #10:

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

input:

82 110032063 394529383

output:

111730592

result:

ok 1 number(s): "111730592"

Test #11:

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

input:

9 11172911 259650437

output:

68381774

result:

ok 1 number(s): "68381774"

Test #12:

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

input:

86 12016027 354886243

output:

263687778

result:

ok 1 number(s): "263687778"

Test #13:

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

input:

91 273689959 454097881

output:

114436127

result:

ok 1 number(s): "114436127"

Test #14:

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

input:

73 148878967 694206977

output:

176215101

result:

ok 1 number(s): "176215101"

Test #15:

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

input:

45 205982233 227598247

output:

156769598

result:

ok 1 number(s): "156769598"

Subtask #3:

score: 10
Accepted

Dependency #2:

100%
Accepted

Test #16:

score: 10
Accepted
time: 3ms
memory: 3568kb

input:

2778 122825869 147297463

output:

43419574

result:

ok 1 number(s): "43419574"

Test #17:

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

input:

289 7729669 589652893

output:

552952137

result:

ok 1 number(s): "552952137"

Test #18:

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

input:

2281 35651417 203950963

output:

21659018

result:

ok 1 number(s): "21659018"

Test #19:

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

input:

1684 258745639 373223677

output:

355596229

result:

ok 1 number(s): "355596229"

Test #20:

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

input:

2107 86850989 455823859

output:

245960059

result:

ok 1 number(s): "245960059"

Test #21:

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

input:

1323 43290799 791120419

output:

509649562

result:

ok 1 number(s): "509649562"

Test #22:

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

input:

2401 34064903 185314627

output:

70571452

result:

ok 1 number(s): "70571452"

Test #23:

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

input:

1073 82288187 564447959

output:

168200843

result:

ok 1 number(s): "168200843"

Test #24:

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

input:

1926 29995039 129122281

output:

60921463

result:

ok 1 number(s): "60921463"

Test #25:

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

input:

3000 66915659 765705179

output:

222619979

result:

ok 1 number(s): "222619979"

Subtask #4:

score: 20
Accepted

Test #26:

score: 20
Accepted
time: 149ms
memory: 27140kb

input:

998818 198334853 998244353

output:

153251445

result:

ok 1 number(s): "153251445"

Test #27:

score: 0
Accepted
time: 141ms
memory: 25104kb

input:

914379 128814383 998244353

output:

477606145

result:

ok 1 number(s): "477606145"

Test #28:

score: 0
Accepted
time: 151ms
memory: 25796kb

input:

944474 478445339 998244353

output:

174204073

result:

ok 1 number(s): "174204073"

Test #29:

score: 0
Accepted
time: 142ms
memory: 26532kb

input:

948637 711592127 998244353

output:

178256114

result:

ok 1 number(s): "178256114"

Test #30:

score: 0
Accepted
time: 153ms
memory: 26260kb

input:

927564 14465663 998244353

output:

315244613

result:

ok 1 number(s): "315244613"

Test #31:

score: 0
Accepted
time: 145ms
memory: 26192kb

input:

934615 392799073 998244353

output:

892700270

result:

ok 1 number(s): "892700270"

Test #32:

score: 0
Accepted
time: 146ms
memory: 26528kb

input:

917196 124972031 998244353

output:

782017412

result:

ok 1 number(s): "782017412"

Test #33:

score: 0
Accepted
time: 152ms
memory: 27104kb

input:

957149 392606173 998244353

output:

159348443

result:

ok 1 number(s): "159348443"

Test #34:

score: 0
Accepted
time: 167ms
memory: 27188kb

input:

997042 184649453 998244353

output:

464643024

result:

ok 1 number(s): "464643024"

Test #35:

score: 0
Accepted
time: 144ms
memory: 26532kb

input:

953353 14071961 998244353

output:

391688875

result:

ok 1 number(s): "391688875"

Subtask #5:

score: 10
Accepted

Test #36:

score: 10
Accepted
time: 1534ms
memory: 236240kb

input:

9909956 720431399 720431401

output:

86883659

result:

ok 1 number(s): "86883659"

Test #37:

score: 0
Accepted
time: 1526ms
memory: 236508kb

input:

9924163 267052829 267052831

output:

75754681

result:

ok 1 number(s): "75754681"

Test #38:

score: 0
Accepted
time: 1534ms
memory: 237256kb

input:

9967885 197873129 197873131

output:

16653739

result:

ok 1 number(s): "16653739"

Test #39:

score: 0
Accepted
time: 1539ms
memory: 237024kb

input:

9952642 101872151 101872153

output:

0

result:

ok 1 number(s): "0"

Test #40:

score: 0
Accepted
time: 1523ms
memory: 237016kb

input:

9955909 167874431 167874433

output:

130012020

result:

ok 1 number(s): "130012020"

Test #41:

score: 0
Accepted
time: 1558ms
memory: 237896kb

input:

9994785 399509567 399509569

output:

153324498

result:

ok 1 number(s): "153324498"

Test #42:

score: 0
Accepted
time: 1566ms
memory: 237004kb

input:

9954011 108819131 108819133

output:

101671540

result:

ok 1 number(s): "101671540"

Test #43:

score: 0
Accepted
time: 1567ms
memory: 237912kb

input:

9997570 213315827 213315829

output:

57441081

result:

ok 1 number(s): "57441081"

Test #44:

score: 0
Accepted
time: 1546ms
memory: 237784kb

input:

9995867 113028299 113028301

output:

67837072

result:

ok 1 number(s): "67837072"

Test #45:

score: 0
Accepted
time: 1532ms
memory: 236172kb

input:

9909335 247275617 247275619

output:

202966817

result:

ok 1 number(s): "202966817"

Subtask #6:

score: 30
Accepted

Dependency #1:

100%
Accepted

Dependency #2:

100%
Accepted

Dependency #3:

100%
Accepted

Dependency #4:

100%
Accepted

Dependency #5:

100%
Accepted

Test #46:

score: 30
Accepted
time: 1520ms
memory: 236244kb

input:

9921815 38466881 725310841

output:

601117286

result:

ok 1 number(s): "601117286"

Test #47:

score: 0
Accepted
time: 1541ms
memory: 236416kb

input:

9919464 4830599 747345523

output:

168521454

result:

ok 1 number(s): "168521454"

Test #48:

score: 0
Accepted
time: 1585ms
memory: 237524kb

input:

9981374 3616373 154722097

output:

2696288

result:

ok 1 number(s): "2696288"

Test #49:

score: 0
Accepted
time: 1547ms
memory: 236128kb

input:

9906664 12433457 558159149

output:

538699014

result:

ok 1 number(s): "538699014"

Test #50:

score: 0
Accepted
time: 1546ms
memory: 237744kb

input:

9985736 46853 410275823

output:

258567756

result:

ok 1 number(s): "258567756"

Test #51:

score: 0
Accepted
time: 1574ms
memory: 237260kb

input:

9962926 33790087 203505083

output:

40932778

result:

ok 1 number(s): "40932778"

Test #52:

score: 0
Accepted
time: 1530ms
memory: 236024kb

input:

9903735 146658401 157137433

output:

154493145

result:

ok 1 number(s): "154493145"

Test #53:

score: 0
Accepted
time: 1540ms
memory: 236164kb

input:

9913516 105010771 110717611

output:

67979325

result:

ok 1 number(s): "67979325"

Test #54:

score: 0
Accepted
time: 1560ms
memory: 237080kb

input:

9953517 268142489 675913921

output:

523115756

result:

ok 1 number(s): "523115756"

Test #55:

score: 0
Accepted
time: 1566ms
memory: 237576kb

input:

9981005 11993207 114120883

output:

7261617

result:

ok 1 number(s): "7261617"

Test #56:

score: 0
Accepted
time: 1568ms
memory: 236828kb

input:

9945956 36522077 168104303

output:

82398556

result:

ok 1 number(s): "82398556"

Test #57:

score: 0
Accepted
time: 1568ms
memory: 237384kb

input:

9967933 15301477 352827883

output:

242773007

result:

ok 1 number(s): "242773007"

Test #58:

score: 0
Accepted
time: 1535ms
memory: 236016kb

input:

9911781 83845891 360130933

output:

158254305

result:

ok 1 number(s): "158254305"

Test #59:

score: 0
Accepted
time: 1528ms
memory: 236320kb

input:

9916390 100404191 108138473

output:

103346432

result:

ok 1 number(s): "103346432"

Test #60:

score: 0
Accepted
time: 1537ms
memory: 237464kb

input:

9974438 7828049 430399297

output:

76675277

result:

ok 1 number(s): "76675277"