来自学长的馈赠8

IOI 难度略微上升

A. 跳一跳

简单的期望\(DP\)

\(f_i\) 表示跳到 \(i\) 再跳到 \(n\) 的期望时间

显然有 \(f_i = \frac{1}{n - i + 1}f_i + \frac{n - i}{n - i + 1}(f_{i + 1} - 1) + 1\)

最后答案为 \(f_1 - 1\)

code
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int mod = 1e9 + 7;
const int maxn = 10000005;
int inv[maxn];
int main(){
	int n;scanf("%d",&n);
	inv[1] = 1;
	for(int i = 2; i <= n; ++i){
		inv[i] = 1ll * (mod - (mod / i)) * inv[mod % i] % mod;
	}
	int ans = 1, las = 0, now = 0;
	for(int i = n - 1; i >= 1; --i){
		now = ((las - 1 + mod) % mod + 1ll * (n - i + 1) * inv[n - i] % mod) %  mod;
		las = now;
	}
	printf("%d\n",(las + 1) % mod);
	return 0;
}

B. 求和

离谱

显然使用扩展欧拉定理

但是会\(T\),怎么办?打表

正解需要分段打表,就离谱

用来打表的code
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn = 10000005;
int mod[25] = {20040829,20040828,6499584,1658880,442368,147456,49152,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
int gcd(int x, int y){return y == 0 ? x : gcd(y, x % y);}
int md;
int qpow(int x, int y, bool op){
	int ans = 1;
	for(;y;y >>= 1, x = 1ll * x * x % mod[md])if(y & 1) ans = 1ll * ans * x % mod[md];
	return op ? ans : ans + mod[md];
}
int f[maxn],rd[15];
signed main(){
	int t; scanf("%d",&t);
	int mx = 0;
	for(int i = 1;i <= t; ++i){
		scanf("%d",&rd[i]);
		mx = max(mx, rd[i]);
	}
	for(register int i = 1; i <= mx; ++i){
		int k = 3;
		if(i & 1){
			for(register int j = 19; j; --j){
				md = j; k = qpow(i,k,gcd(k,mod[j - 1]) == 1);
				// printf("%d %d\n",i,k);
			}
		}else{
			k = 0;
			for(register int j = 8; j; --j){
				md = j; k = qpow(i,k,gcd(k,mod[j - 1]) == 1);
				// printf("%d %d\n",i,k);
			}
		}
		md = 0; k = qpow(i, k, 1);
		f[i]= (f[i - 1] + k) % mod[0];
		// if(i % 1000 == 0)printf("%d,",f[i]);
		// if(i % 10000 == 0)printf("%d,",f[i]);
		// if(i % 100000 == 0)printf("\n");
	}
	for(int i = 1; i <= t; ++i)printf("%d\n",f[rd[i]]);
	return 0;
}
ACcode10000一表法
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn = 10000005;
int mod[25] = {20040829,20040828,6499584,1658880,442368,147456,49152,16384,8192,4096,2048,1024,512,256,128,64,32,16,8,4,2,1};
int b[5555] = {0,5413320,3257714,18991750,6142245,19199927,8814055,6173913,1425749,7402576,17107771,19233494,394008,7403328,3271418,17200114,4928929,8992733,14877783,18935065,3090209,7987787,3674077,1859421,7912130,9934759,19476406,9742396,12055344,3353959,8853221,11880610,14870898,16358226,11600408,13484786,2118974,5477720,9880623,16650494,509582,12004494,19464061,4384530,5326135,7262329,10708218,11297356,18600784,3285952,14103680,584254,14110389,18367281,13168064,6429814,9803398,14196887,14854980,5245480,11964472,6094866,6513590,3037758,7254474,4989527,15263711,1604437,2534034,18596901,7143177,19270236,14766286,9280450,8089102,2174031,13127125,13018983,10672633,11650693,5581318,11946811,5716434,2204934,4692458,14782855,4411763,5551166,19910016,1948006,6922243,8514314,5808062,5785126,9669366,17460770,1523386,12735682,16925991,2184093,14920264,14043205,13639516,8204748,515615,18582930,15820234,14338157,10872055,5682854,2965211,8616626,18004240,6768368,11315167,4258240,6284621,14404502,14657940,6078386,16428824,7411212,19546299,10412819,2527938,8584784,12442651,4732242,6811583,3904709,3255114,8427798,15397877,13021037,13115499,2198758,17685094,9848745,899085,14117691,14014314,6189292,19094018,10498909,11421469,14448001,10558011,12406216,10950870,18630011,13660123,18882887,13050561,2103628,2842450,17935895,15914693,19054350,3187805,9084315,6111490,5920465,8865487,15394512,11386330,3412435,4668136,6469830,15990090,5872675,13982392,9037543,1940953,6764352,17881819,2382288,18972332,10157271,3567474,551490,18747916,1363655,11507150,1421697,8396357,12441284,12217520,9574373,9108351,9580711,7803833,16367658,6752969,336042,19528258,8430876,19625437,9529061,9027537,11592365,11468348,16185313,13614766,9841269,8496659,13730403,13767962,3314434,9016023,5984703,12895442,17896565,2077338,9128586,1462821,8742901,14426468,11017847,12345893,3518057,12928334,7226054,2676103,4974740,15788932,3561607,12942731,10485005,4795134,8045147,2490553,19676913,5879845,314897,11492286,8233536,1425806,17267999,9931957,16061637,15506247,8382016,4480682,7291390,3588268,2971969,16740167,5474712,17427589,5233481,10410757,19753007,15837991,7361473,8181182,1471017,17645964,814150,14819010,12532351,5468471,3415260,17356102,14718867,13960071,7042416,1374814,4877222,16068214,10860461,556763,2202232,13932013,2457252,16242259,13717184,18454532,18690942,3631756,2344037,13151325,6981395,18833645,15150660,3570195,17428844,10830123,18523609,3653398,19019518,2677749,6410898,6654919,14910101,6794801,10224049,9764297,12698975,11400425,16665537,5816760,12701955,1991371,4069131,6119727,11251427,13396639,17159631,15262998,13496524,9626421,4470254,4936027,11417078,7919624,12766622,12811578,19381182,10547853,3203953,15521703,15694477,11976039,14102240,19228452,1039125,7786691,5822323,4604702,14900144,7904651,10813300,17699354,5066011,6799702,7479902,3293639,9633508,10779256,3049545,374193,1507399,970030,2666364,19983199,4443456,7232623,11209430,14456710,5559451,3855869,10326534,12102185,17616110,4230523,15099373,9114419,6213317,1741787,19004645,12440187,16489095,11016062,65701,2955779,7713176,5583637,16948178,9006097,10981582,18081199,1855969,5264783,5084895,2935960,6720989,11211092,703915,10238667,17839408,4756016,5526356,1163598,11418770,13753966,9148088,6848499,8589911,13268075,14529102,10359582,13151123,12661149,1107185,17097424,5184939,15307016,8091176,12739061,11299870,4950801,205322,11982135,12330284,8125948,17626914,77068,8756585,12174292,8810037,8372256,18083000,4709643,17861663,17184411,18222119,15067092,9965990,13907473,17349440,11124498,2446259,1117110,4763920,12799083,2496008,16303549,44243,19393395,6546035,15042540,11948973,17011256,14354892,16022538,11525045,1398272,10582015,10918816,3640447,3102801,6600825,19227538,13896831,9904179,5432449,14442441,16644375,16632347,924528,6570383,10069935,1659968,1197305,1211124,17155250,8589855,4470203,12910438,14430410,6024066,11956918,9714971,19858748,15759756,9008317,5810626,5318548,6262540,1525096,9237762,9960125,17589903,11566538,12831417,15222624,11114839,6206770,16546949,1550134,281253,6115967,3620211,18101378,10269180,17530455,6695421,13512759,14689299,15500851,9591684,1944631,15107108,2539946,9339108,12608670,12949446,13284767,3282764,13986406,9339016,6205166,11639585,1489915,2762096,8525358,18771898,13897875,11794823,2294498,16562479,13588097,18679627,12718484,6193424,7563287,7856557,8291534,16043084,1945023,17589444,92642,2759695,7577214,10682016,17648409,1010978,7001721,4553288,15987877,5338866,8993161,6294386,16068240,18926466,17895268,18878980,19577320,10512659,16682975,11275391,3758800,6962143,6749091,3159699,18108977,2070305,18718874,12627217,11177720,13297493,6085162,6988165,5689321,14703312,13043537,6403850,8392530,2097344,15243761,9173985,11154896,10428847,14764279,8544069,8071905,13590850,3610437,8170173,1826818,11175744,6299529,3284185,13469651,5682308,5307292,2129577,3132116,14990342,18276859,16930776,2887051,3636438,18353246,18923411,4862944,12895126,7764072,2308007,2546640,16410653,5759345,16831839,14252658,7979777,3656427,6283744,1754825,10026224,16034211,16863737,5721433,16373102,10146749,11595967,9975975,15055965,14732044,19647519,18196716,13052782,15205636,19535527,13246571,2071667,7696391,3994418,18769001,11657074,12962318,19021860,3491751,5266912,4043674,8855205,11582163,859957,14656068,13731714,2871214,14056832,16277239,5327278,5954868,13956457,16948619,14518956,7243094,12274625,7437584,8117374,13772869,8455226,2551466,9705824,13922660,7328099,2427057,2710155,19053125,1840948,19384047,3821364,18392073,2339168,2463228,19319941,12050457,12652839,5856459,17779519,13705670,799198,7350196,13330670,17837188,12119891,12226423,6593318,15246213,9917263,7570268,16996600,6677867,4339113,16303879,7023796,9816313,6792408,12913236,517899,4179979,16856113,12404556,8088670,16615257,16916633,4813419,5877180,18011469,12741877,8833877,4073668,3926010,4525374,5849144,12290536,17872545,17952195,19419927,16258900,10958159,19674559,12951772,4851952,1350641,18018044,3466436,808302,10981643,17774089,2485830,8331952,3449177,6080312,10376236,7801368,8202241,20019637,19804623,19650536,7771194,19525350,18658748,12167709,12635195,19851540,15694086,11233824,112948,17726928,13151025,11316806,4658386,7494561,5116672,2088521,6577760,3020333,1555470,7960444,5100644,18631367,17576661,19388804,10738435,4603590,11106570,16669927,17622918,14735989,4041175,7056147,12963232,2855981,10975952,6016839,5253449,14739441,652250,13640980,6225896,2076102,18443806,1372072,16403607,10057293,434834,4697481,4954147,10003819,16500793,11014729,6299135,10533186,14582123,3963872,3033366,1951722,12977026,17983260,18189496,14900009,1040612,11822326,14894446,12587736,179246,13969168,11185622,18564487,1106107,1936060,860863,19830381,14216548,3065445,17459035,4396953,36626,14209604,4112532,14546291,14746452,19961297,3997779,15091508,4945192,8897443,13130290,4034699,17458158,15159077,6868500,18361086,3430638,1795928,6372905,7495979,3659904,14941657,10292382,6056534,1653753,2158291,9116730,19571559,16946917,3134095,16623830,19388739,942300,17503313,6450303,13483283,10263747,15756241,11065653,17542460,19832688,18136584,6984606,15885089,5857167,257751,16147309,11767607,826424,15473863,47897,19240969,14081729,9046172,18430125,4911626,11838858,10080950,19535537,6318715,8068549,14577122,19140582,13437708,19498094,15806012,6554611,2246363,18217684,8913037,18457249,11625274,18050657,5540175,7178553,4140296,7520373,5131467,18161500,8769123,7711790,17962149,12024911,13133019,7935096,9936028,18001758,15753222,12898290,1586962,18515958,3774100,11117826,7096822,374274,19875463,3200181,19329756,18692283,13544312,1494339,1911772,14775593,16035599,1117475,4524116,19776237,9965133,5059359,3236737,7270304,8545292,12625159,18554424,6789939,7511233,12118124,15178414,11019509,8284492,12444686,18471417,15504757,7255618,1686874,9297142,3373533,7536363,4981566,17360445,3588872,16336393,6610556,1177557,16720621,18504048,3499274,3751674,1500731,8625900,5058309,9048125,4767324,10015482,19372640,17543323,9699897,6498208,10713632,18357217,8630610,13819239,10266944,3323443,17711057,13892649,3258170,7712994,2732350,14365589,20040176,11910445,5275131,1817013,14013952,4758215,15354102,3362022,5445261,7192745,3825863,7093344,19828066,444051,4348090,10956902,13074801,17990867,11389959,16772065,5551634,14715625,17397927,10796500,11701048,6287687,7594595,3030748,10808698,14381162,9195507,16631193,10183412,19511364,6284138,16423850,4793537,18811282,806930,16807978,990321,8479600,8479600};
int gcd(int x, int y){return y == 0 ? x : gcd(y, x % y);}
int md,block = 10000;
int qpow(int x, int y, bool op){
	int ans = 1;
	for(;y;y >>= 1, x = 1ll * x * x % mod[md])if(y & 1) ans = 1ll * ans * x % mod[md];
	return op ? ans : ans + mod[md];
}
int f[maxn];
signed main(){
	int t; scanf("%d",&t);
	int mx = 0;
	for(int i = 1;i <= t; ++i){
		int x; scanf("%d",&x);
		int ans = b[x/block];
		for(register int i = (x/block) * block + 1; i <= x; ++i){
			int k = 3;
			if(i & 1){
				for(register int j = 19; j; --j){
					md = j; k = qpow(i,k,gcd(k,mod[j - 1]) == 1);
				}
			}else{
				k = 0;
				for(register int j = 8; j; --j){
					md = j; k = qpow(i,k,gcd(k,mod[j - 1]) == 1);
				}
			}
			md = 0; k = qpow(i, k, 1);
			ans = (ans + k) % mod[0];
		}
		printf("%d\n",ans);
	}
	
	return 0;
}

1000 一表就不粘了。。。

C. 明明的随机数

如果你会斯特林数那么应该能发现答案\(unique_{ans} \times \{^n_{k1}\} \times \{^m_{k2}\}\)

现在就是求\(unique_{ans}\)\(k1\)行都不相等,\(k2\)列都不相等的矩形数量

\(f_i\)\(i\)个行不限制,\(k\)个列都不同的方案数

\(g_i\)\(i\)行互不相同,\(k\)列互不相同的方案数

那么显然有\(f_i = \sum_{j = 1} ^i\{^i_j\}g_j\)

$f_i = (c^i )^{\frac{m}{}} $

根据斯特林反演\(g_i = \sum_{j = 1} ^i(-1)^{i - j}[^i_j]f_j\)

然后通过

\(k! \{^m_k\} = \sum_{i = 0}^k(-1)^{k - i}(^k_i)i^m\)

快速求第二类斯特林数即可

后面还有多项式解法,我太弱了不会

code
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
const int maxn = 250055;
const int mod = 998244353;
int fac[maxn],inv[maxn],s1[3005][3005];
int ans = 0;
int qpow(int x, int y){
	int ans = 1;
	for(; y; y >>= 1, x = 1ll * x * x % mod)if(y & 1)ans = 1ll * ans * x % mod;
	return ans;
}
inline int addmod(int a, int b){
	return a += b, a >= mod ? a - mod : a;
}
inline int delmod(int a, int b){
	return a -= b, a < 0 ? a + mod : a;
}
void pre(){
	fac[0] = 1;
	for(int i = 1; i <= 250000; ++i)fac[i] = 1ll * fac[i - 1] * i % mod;
	inv[250000] = qpow(fac[250000], mod - 2);
	for(int i = 250000 - 1; i; --i)inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
	inv[0] = 1; 
	s1[0][0] = 1;
	for(int i = 1; i <= 3000; ++i)
		for(int j = 1; j <= 3000; ++j)
		 	s1[i][j] = addmod(s1[i - 1][j - 1], 1ll * s1[i - 1][j] * (i - 1) % mod);
}
int C(int n, int m){
	if(n < m)return 0;
	return 1ll * fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int s2(int n, int m){
	int ans = 0;
	for(int i = 0; i <= m; ++i){
		if(i & 1)ans = delmod(ans, 1ll * C(m, i) * qpow(m - i, n) % mod);
		else ans = addmod(ans, 1ll * C(m, i) * qpow(m - i, n) % mod);
	}
	return 1ll * ans * inv[m] % mod;
}
int n,m,k1,k2,c;
int F(int x){
	int ans = 1, now = qpow(c,x);
	for(int i = 0; i < k2; ++i)ans = 1ll * ans * now % mod, now = delmod(now,1);
	return ans;
}
int G(int k1){
	int ans = 0;
	for(int i = 1; i <= k1; ++i)
		if((k1 - i) & 1)ans = delmod(ans, 1ll * s1[k1][i] * F(i) % mod);
		else ans = addmod(ans, 1ll * s1[k1][i] * F(i) % mod);
	return ans;
}
int main(){
	int t; scanf("%d",&t);
	pre();
	for(int i = 1; i <= t; ++i){
		scanf("%d%d%d%d%d",&n,&m,&k1,&k2,&c); ans = 0;
		printf("%lld\n",1ll * G(k1) * s2(n, k1) % mod * s2(m, k2) % mod);
	}
	return 0;
}

D. 凯爹博弈

博弈论

先咕着

posted @ 2022-08-03 07:53  Chen_jr  阅读(39)  评论(0编辑  收藏  举报