算法競賽入門經典(第二版)-劉汝佳-第十章 數學概念與方法 習題(12/51)

說明

本文是我對第十章51道習題的練習總結,建議配合紫書——《算法競賽入門經典(第2版)》閱讀本文。
先貼代碼,文字性題解回頭再補充。

習題

習10-1

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAX = 100;

int A[10][10];

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	int T;
	cin >> T;
	FOR1(t, 1, T) {
		FOR1(i, 0, 8) {
			if (i & 1) continue;
			FOR1(j, 0, i) {
				if (j & 1) continue;
				cin >> A[i][j];
			}
		}

		FOR1(i, 0, 6) {
			if (i & 1) continue;
			FOR1(j, 0, i) {
				if (j & 1) continue;
				A[i + 2][j + 1] = (A[i][j] - A[i + 2][j] - A[i + 2][j + 2]) / 2;
				A[i + 1][j] = A[i + 2][j] + A[i + 2][j + 1];
				A[i + 1][j + 1] = A[i + 2][j + 1] + A[i + 2][j + 2];
			}
		}

		FOR1(i, 0, 8) {
			FOR1(j, 0, i-1) {
				printf("%d ", A[i][j]);
			}
			printf("%d\n", A[i][i]);
		}
	}
	return 0;
}

習10-2

題意

思路

代碼



習10-3

題意

思路

代碼



習10-4

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAX = 1299709+100000;//確保下一個素數被包含

int n;
int isprime[MAX];
vector<int> prime;

void process_prime() {
	memset(isprime, 0x3f, sizeof(isprime));
	isprime[1] = 0;
	FOR1(i, 2, MAX) {
		if (isprime[i]) {
			prime.push_back(i);
			for (int j = i * 2; j <= MAX; j += i) {
				isprime[j] = 0;
			}
		}
	}
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	process_prime();
	while (cin >> n && n) {
		if (isprime[n]) {
			printf("0\n", n);
			continue;
		}
		int s = prime.size();
		FOR1(i, 0, s - 1) {
			if (prime[i] > n) {
				printf("%d\n", prime[i] - prime[i - 1]);
				break;
			}
		}
	}
	return 0;
}

習10-5

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAXN = 1120;
const int MAXP = 187; //計算得出最多有187個素數
const int MAXK = 14; //計算得出最多有187個素數

int n, k, ans;
int isprime[MAXN+1];
vector<int> prime;
//int dp[MAXP + 1][MAXK + 1][MAXN + 1]; //dp[i][j][m]表示前i個素數中挑出j個素數其值爲m的可能數,實際上第一維可以省略
int dp[MAXK + 1][MAXN + 1]; //dp[j][m]表示前i個素數中挑出j個素數其值爲m的可能數,第一維已經省略

void process_prime() {
	memset(isprime, 0x3f, sizeof(isprime));
	isprime[1] = 0;
	FOR1(i, 2, MAXN) {
		if (isprime[i]) {
			prime.push_back(i);
			for (int j = i * 2; j <= MAXN; j += i) {
				isprime[j] = 0;
			}
		}
	}
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	process_prime();
	while (cin >> n >> k && n) {
		memset(dp, 0, sizeof(dp));
		dp[0][0] = 1;
		FOR1(i, 1, MAXP) {
			if (prime[i-1] > n) break;
			int pi = prime[i-1];
			FOR2(j, min(i, k), 1) {
				FOR2(m, n, pi) {
					dp[j][m] += dp[j - 1][m - pi];
				}
			}
		}
		printf("%d\n", dp[k][n]);
	}
	return 0;
}

習10-6

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAX = 10000;//確保下一個素數被包含

int n, p; //p表示prime中素數個數
int isprime[MAX + 1];
vector<int> prime;

void process_prime() {
	memset(isprime, 0x3f, sizeof(isprime));
	isprime[1] = 0;
	FOR1(i, 2, MAX) {
		if (isprime[i]) {
			prime.push_back(i);
			for (int j = i * 2; j <= MAX; j += i) {
				isprime[j] = 0;
			}
		}
	}
	p = prime.size();
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	process_prime();
	while (cin >> n && n) {
		int res = 0;
		int i = 0, j = 0;
		int s = prime[0];
		while (j < p) {
			if (s == n)	{
				res++;
				if (j == p - 1) break;
				s += prime[++j];
			}
			else if (s < n) {
				if (j == p - 1) break;
				s += prime[++j];
			}
			else {
				if (i == p - 1) break;
				s -= prime[i++];
				if (i > j)
					s += prime[++j];
			}
		}
		printf("%d\n", res);
	}
	return 0;
}

習10-7

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

typedef long long LL;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const LL MAX = 1000000;
const LL LMAX = MAX*MAX;

LL a[2];
int p; //p表示oneprime中素數個數
int isprime[MAX + 1];
vector<LL> oneprime;

void process_prime() {
	memset(isprime, 0x3f, sizeof(isprime));
	isprime[1] = 0;
	FOR1(i, 2, MAX) {
		if (isprime[i]) {
			for (LL j = (LL)i * i; j <= LMAX; j *= i)
				oneprime.push_back(j);
			for (int j = i * 2; j <= MAX; j += i) {
				isprime[j] = 0;
			}
		}
	}
	sort(oneprime.begin(), oneprime.end());
	p = oneprime.size();
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	process_prime();
	int T;
	cin >> T;
	FOR1(t, 1, T) {
		cin >> a[0] >> a[1];
		a[1]++;
		int res[2];
		FOR1(j, 0, 1) {
			FOR1(i, 0, p - 1) {
				if (oneprime[i] >= a[j]) {
					res[j] = i;
					break;
				}
			}
		}
		printf("%d\n", res[1] - res[0]);
	}
	return 0;
}

習10-8

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

typedef long long LL;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const LL MAX = 500000;
const LL LMAX = MAX*MAX;

int n, n0;
map<LL, int> oneprime;

void process_prime() {
	FOR1(i, 2, MAX) {
		if (!oneprime.count(i)) {
			int k = 2;
			for (LL j = (LL)i * i; j <= LMAX; j *= i, k++)
			if (!oneprime.count(j)) oneprime[j] = k;
		}
	}
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	process_prime();
	while (cin >> n && n) {
		if (n == (-(1<<30) - (1<<30))) {
			printf("31\n");
			continue;
		}
		n0 = abs(n);
		if (oneprime.count(n0)) {
			int ans = oneprime[n0];
			if (n < 0) {
				while (ans % 2 == 0) ans >>= 1;
			}
			printf("%d\n", ans);
		}
		else
			printf("1\n");
	}
	return 0;
}

習10-9

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

//const int MAXN2 = 1e9;
const int MAXN = 40000;

int L, U;
int isprime[MAXN+1];
vector<int> prime;
int resn, resi;

void process_prime() {
	memset(isprime, 0x3f, sizeof(isprime));
	isprime[1] = 0;
	FOR1(i, 2, MAXN) {
		if (isprime[i]) {
			prime.push_back(i);
			for (int j = i * 2; j <= MAXN; j += i) {
				isprime[j] = 0;
			}
		}
	}
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	process_prime();
	int T;
	cin >> T;
	FOR1(t, 1, T) {
		cin >> L >> U;
		resn = L, resi = 0;
		FOR1(k, L, U) {
			int k1 = k;
			int ik = k1 - L;
			int res = 1;
			int k2 = sqrt(k1) + 1;
			int pcnt = prime.size();
			FOR1(j, 0, pcnt-1) {
				int mult = 1;
				int pj = prime[j];
				if (k1 == 1) break;
				while (k1 % pj == 0) {
					k1 /= pj;
					mult++;
				}
				res *= mult;
			}
			if (k1 > 1) //說明還有約數
				res *= 2;
			if (res > resi) {
				resn = k;
				resi = res;
			}
		}
		printf("Between %d and %d, %d has a maximum of %d divisors.\n", L, U, resn, resi);
	}
	return 0;
}

習10-10

題意

思路

代碼



習10-11

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAXN = 10000;

int n;
string s[3];

string add(string s1, string s2) {
	string sum;
	int up = 0;
	int l1 = s1.size(), l2 = s2.size();
	FOR1(i, 0, MAXN) {
		if (i >= l1 && i >= l2) {
			if (up) sum += (char)(up + 48);
			break;
		}
		int a1 = (i < l1) ? s1[i]-48 : 0;
		int a2 = (i < l2) ? s2[i]-48 : 0;
		up = up + a1 + a2;
		sum += (char)(up % 10 + 48);
		up /= 10;
	}
	return sum;
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	while (cin >> n) {
		s[1] = "1";
		s[2] = "2";
		FOR1(i, 0, n - 3) {
			s[0] = s[1];
			s[1] = s[2];
			s[2] = add(s[0], s[1]);
		}
		s[1] = add(s[0], s[2]);
		int scnt = s[1].size();
		FOR2(i, scnt - 1, 0)
			printf("%c", s[1][i]);
		printf("\n");
	}
	return 0;
}

習10-12

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAXN = 50000;

int n;
double F[MAXN + 1];

void pre_process() {
	F[1] = 1;
	F[2] = 0.5;
	FOR1(i, 2, MAXN - 1)
		F[i + 1] = F[i] * (2 * i - 1) / 2 / i;
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	pre_process();
	int T;
	cin >> T;
	FOR1(t, 1, T) {
		cin >> n;
		printf("%.4lf\n", 1 - F[n / 2]);
	}
	return 0;
}

習10-13

題意

思路

代碼



習10-14

題意

思路

代碼



習10-15

題意

思路

代碼



習10-16

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAXN = 1000;

int n;
string S[1001];

string add(string s1, string s2) {
	string sum;
	int up = 0;
	int l1 = s1.size(), l2 = s2.size();
	FOR1(i, 0, MAXN) {
		if (i >= l1 && i >= l2) {
			if (up) sum += (char)(up + 48);
			break;
		}
		int a1 = (i < l1) ? s1[i] - 48 : 0;
		int a2 = (i < l2) ? s2[i] - 48 : 0;
		up = up + a1 + a2;
		sum += (char)(up % 10 + 48);
		up /= 10;
	}
	return sum;
}

void pre_process() {
	S[1] = "0";
	S[2] = "1";
	S[3] = "1";
	FOR1(i, 4, MAXN)
		S[i] = add(S[i-2], add(S[i-1], S[i-2]));
}

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	pre_process();
	while (cin >> n) {
		int scnt = S[n].size();
		FOR2(i, scnt - 1, 0)
			printf("%c", S[n][i]);
		printf("\n");
	}
	return 0;
}

習10-17

題意

思路

代碼


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
    
const int N = 1000001;

bool isPrime[N+1];
bool isSemi[N+1];
int countSemi[N/4+1];
    
void initPrime()
{   
    int i;
    for (i = 0; i <= N; i ++)
        isPrime[i] = true;
    isPrime[0] = isPrime[1] = false;
    for (i = 5; i <= N; i += 4) {
        if (isPrime[i]) {
            if (i > (int)sqrt((double)N)) continue;
            for (int j = i*i; j <= N; j += 4*i)
                isPrime[j] = false;
        }
    }
}

void initSemi()
{
    int i, j;
    for (i = 1; i <= N; i += 4) {
        isSemi[i] = false;
        countSemi[i/4] = 0;
    }
    for (i = 5; i <= N; i += 4) {
        if (i > (int)sqrt((double)N)) break;
        for (j = i; j <= N; j += 4) {
            if (i * j > N) break;
            if (isPrime[i] && isPrime[j])
                isSemi[i * j] = true;
        }
    }
    for (i = 5; i <= N; i += 4) {
        countSemi[i/4] = countSemi[i/4-1];
        if (isSemi[i])
            countSemi[i/4] ++;
    }
}

int main(void)
{
    int n;
    initPrime();
    initSemi();
    while (cin >> n && n) {
        printf("%d %d\n", n, countSemi[n/4]);
    }
    return 0;
}

習10-18

題意

思路

代碼



習10-19

題意

思路

代碼



習10-20

題意

思路

代碼

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

#define FOR1(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define FOR2(i, a, b) for (int i = (a); i >= (int)(b); i--)

const int MAXM = 2000;

int n, m;
int u[MAXM], d[MAXM];

int main() {
#ifdef CODE_LIANG
	freopen("datain.txt", "r", stdin);
	freopen("dataout.txt", "w", stdout);
#endif
	while (cin >> n >> m) {
		int ans = -1, minfloor = 0x3f3f3f3f;
		FOR1(i, 0, m - 1) {
			cin >> u[i] >> d[i];
			int floor = (n*u[i]) - (n*u[i] - 1) / (u[i] + d[i]) * (u[i] + d[i]);
			if (floor < minfloor) {
				ans = i;
				minfloor = floor;
			}
		}
		printf("%d\n", minfloor);
	}
	return 0;
}

習10-21

題意

思路

代碼



習10-22

題意

思路

代碼



習10-23

題意

思路

代碼



習10-24

題意

思路

代碼



習10-25

題意

思路

代碼



習10-26

題意

思路

代碼



習10-27

題意

思路

代碼



習10-28

題意

思路

代碼



習10-29

題意

思路

代碼



習10-30

題意

思路

代碼



習10-31

題意

思路

代碼



習10-32

題意

思路

代碼



習10-33

題意

思路

代碼



習10-34

題意

思路

代碼



習10-35

題意

思路

代碼



習10-36

題意

思路

代碼



習10-37

題意

思路

代碼



習10-38

題意

思路

代碼



習10-39

題意

思路

代碼



習10-40

題意

思路

代碼



習10-41

題意

思路

代碼



習10-42

題意

思路

代碼



習10-43

題意

思路

代碼



習10-44

題意

思路

代碼



習10-45

題意

思路

代碼



習10-46

題意

思路

代碼



習10-47

題意

思路

代碼



習10-48

題意

思路

代碼



習10-49

題意

思路

代碼



習10-50

題意

思路

代碼



習10-51

題意

思路

代碼



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章