跳到正文

个人记录

2024年10月9日总结

Rocketcar约 14 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月6日
篇幅
约 14 分钟

版本

  1. v1当前

T1 P1482 Cantor 表(升级版)

①① 时间复杂度与数据范围:

(以下分子用 fzfz 表示,分母用 fmfm 表示)

fz,fm≤104fz, fm \leq 10^4

时间复杂度:O(log⁡N)O( \log N)。

②② 细节点:

注意给出的分数不一定是最简分数。

注意:一定要先输出列在输出行!!!(被坑惨了)

3,3, 思路:

1,1, 分子乘上分子,分母乘上分母。

2,2, 求出分子分母的 gcd⁡\gcd,然后就将分数约分。

3,3, 先输出分母,在输出分子。

④④ 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int x1, x2, x3, x4;
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	char x;
	cin >> x1 >> x >> x2 >> x3 >> x >> x4;
	int ans1 = x1 * x3;
	int ans2 = x2 * x4;
	int cnt = gcd(ans1, ans2); // 求出最大公因数。
	cout << ans2 / cnt << ' ' << ans1 / cnt << endl; // 先输出列在输出行。
	return 0;
}

T2 U414963 分数运算

①① 时间复杂度与数据范围:

fz,fm≤20fz,fm \le 20。

时间复杂度:O(Nlog⁡N)O(N \log N)

②② 细节点:

1,1, 注意数据范围,一定要开 longlonglong long。

2,2, 如果答案是整数,那么就直接输出分子即可。

③③ 思路:

1,1, 我们可以使用 whilewhile 循环读入, while(cin >> ch >> x >> ch1 >> y)。

2,2, 使用题目所给出的公式,其他题目均可使用的

ab+cd=ad+cbbd\frac{a}{b}+\frac{c}{d}=\frac{ad+cb}{bd}

的公式

也可以用我的方法:

先将分母同分,然后将两个分数相加,非常的麻烦。

3,3, 分数需要进行约分。

④④ 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int ans1, ans2, ans3, ans4;
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
char x;
char c;
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> ans1 >> c >> ans2;
	int fz = ans1, fm = ans2;
	while (cin >> x >> ans1 >> c >> ans2)
	{
		if (x == '+')
		{
			int lcm1 = fm * ans2 / gcd(fm, ans2);
			fz *= lcm1 / fm;
			ans1 *= lcm1 / ans2;
			fz = ans1 + fz;
			fm = lcm1;
			int gcd1 = gcd(fm, fz);
			fm /= gcd1;
			fz /= gcd1; // 额,这是我的方法。
		}
	}
	if (fm != 1)cout << fz << '/' << fm << endl;
	else cout << fz << endl;
	return 0;
}

T3 P1572 计算分数

①① 时间复杂度与数据范围:

fz,fm<=fz,fm <= INT_MAX。 存在负数。

时间复杂度:O(NlogN)O(NlogN)。

②② 细节点:

1,1, 分母为 11 时输出分子。

2,2, 减法的分数计算公式: a / b - c / d == (a * d - b * c) / (b * d)

3,3, 一定要提前开 longlonglong long !!!。

4,4, 答案存在负数,输出是得格外注意:

C++
1, 分母不能为负数
2,一定要化简为最简分数。

③③ 思路:

1,1, 向上一题一样,我们可以使用 whilewhile 循环读入, while(cin >> ch >> x >> ch1 >> y)。

2,2, 一定要判断是否是 ++ 还是 −-。

3,3, 如果只有一个分数,也得在开头判断。

④④ 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int fz, fm;
char ch, cal;
int x, y;
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> fz >> ch >> fm;
	int gcd1 = gcd(fz, fm);
	fz /= gcd1, fm /= gcd1;
	if (fm < 0)
	{
		fz = -1 * abs(fz);
		fm = abs(fm);
	}
	while(cin >> cal >> x >> ch >> y)
	{
		if (cal == '-')
		{
			int cnt1 = fz * y - x * fm;
			int cnt2 = fm * y;
			int gcd1 = gcd(cnt1, cnt2);
			cnt1 /= gcd1, cnt2 /= gcd1;
			fz = cnt1, fm = cnt2;
		}
		else
		{
			int cnt1 = fz * y + x * fm;
			int cnt2 = fm * y;
			int gcd1 = gcd(cnt1, cnt2);
			cnt1 /= gcd1, cnt2 /= gcd1;
			fz = cnt1, fm = cnt2;
		}
		if (fm < 0)
		{
			fz = -1 * abs(fz);
			fm = abs(fm);
		}
	}
	if (fm != 1)cout << fz << '/' << fm << endl;
	else cout << fz << endl;
	return 0;
}

T4 P2118 [NOIP2014 普及组] 比例简化

①① 时间复杂度与数据范围:

1≤L≤1001 \le L \le 100 。

时间复杂度 :O(L2)O(L^2)

②② 细节点:

1,1, 注意精度问题,可能精度出现误差。

2,2, a×d−b×c≤0a \times d - b \times c \le 0,说明此时有最小值,左边的小于右边的。

③③ 思路:

1,1, 双重循环从 00 到 LL 枚举比例。

2,2, 控制精度,将除法改成乘法,求出最小值。

3,3, 注意 A′A' 与 B′B' 需要互质。

4,4, 将 (a×b)(a \times b) 更 min×b×dmin \times b \times d 进行比较。

④④ 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int x, y, l;
int gcd (int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> x >> y >> l;
	int num1, num2;
	double minn = INT_MAX;
	for (int i = 0;i <= l;i ++)
	{
		for (int j = 0;j <= l;j ++)
		{
			if (gcd(i, j) == 1)
			{
				if (i * y >= j * x)
				{
					if (i * 1.0 / j <= minn)
					{
						num1 = i;
						num2 = j;
						minn = i * 1.0 / j;
					}
				}
			}
		}
	}
	cout << num1 << ' ' << num2 << endl;
	return 0;
}

T5 P9484 「LAOI-1」GCD

①① 时间复杂度与数据范围:

假如使用暴力算法:floydfloyd。

时间复杂度:O(Tn3)O(T n^3)

正解 :

O(Σqlogn)O(\Sigma q log n)

②② 细节点:

暴力:

1,1,直接暴力建边跑 floydfloyd 。

正解:

1,1, gcd(i, j) = i,lcm(i, j) = j,代表 jj 能被 ii 整除。

2,2, 如果 ii 和 jj 是倍数关系,那么就建立无向边,xx 到 yy 的最短路径。

③③ 思路:

1,1, floydfloyd 预期 4040 分,实际上 6060 分。

2,2, xx 走到了 yy,便权威正数,走一条答案一定会增加,xx 只能走到他的倍数上去,xx - 因数,xx - 最大公因数 (gcd(x,y))(gcd(x, y)) ,但是xx 不可能去走他的倍数,因为那样的边权太大了,因此只能走因数。所以答案就是

(x−gcd(x,y))+(y−gcd(x,y))(x - gcd(x, y)) + (y - gcd(x, y))

④④ 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int T, n;
int gcd (int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> T;
	while (T --)
	{
		int n, q;
		cin >> n >> q;
		while (q --)
		{
			int x, y;
			cin >> x >> y;
			cout << (x - gcd(x, y)) + (y - gcd(x, y)) << endl;
		}
	}
	return 0;
}

// 暴力代码

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int T, n ,q, d[1001][1001];
void floyd()
{
	for (int i = 1;i <= n;i ++)
	{
		for (int j = 1;j <= n;j ++)
		{
			for (int k = 1;k <= n;k ++)
			{
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}
}
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
void init()
{
	for (int i = 1;i <= n;i ++)
	{
		for (int j = 1;j <= n;j ++)
			d[i][j] = 1e9;
		d[i][i] = 0;
	}
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> T;
	while(T --)
	{
		cin >> n >> q;
		init();
		for (int i = 1;i <= n;i ++)
		{
			for (int j = 1;j <= n;j ++)
			{
				if (gcd(i, j) == i && i / gcd(i, j) * j == j)
				{
					d[i][j] = abs(i - j);
					d[j][i] = abs(i - j);
				}
			}
		}
		floyd();
		while (q --)
		{
			int x, y;
			cin >> x >> y;
			cout << d[x][y] << endl;
		}
	}
	return 0;
}

T6 P1414 又是毕业季 II

①① 时间复杂度与数据范围:

时间复杂度:O(NN)O(N \sqrt N)

②② 细节点:

1,1, gcdgcd 组成条件: gcd(a,b)≤min(a,b)gcd(a, b) \le min(a, b)。

2,2, 桶数组需要开 10610^6。

③③ 思路:

1,1, aa 和 bb 统计因数

每次统计的时候只能计算一次,问能否从 nn 个数字中选取 kk 个数保证 gcdgcd 最大。

2,2, n n个数字中,是否有最大的因子,出现的次数到达了 kk 次,有的话就是答案。

3,3, gcdgcd 的个数越多,那么求解的答案单调不增。

注意:求解的是因数,而不是质因数。

④④ 代码:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int vis[N], n;
void div(int x)
{
	for (int i = 1;i * i <= x;i ++)
	{
		if (x % i == 0)
		{
			vis[i] ++;
			if (x != i * i)
			{
				vis[x / i] ++;
			}
		}
	}
	return ;
}
signed main()
{
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	int maxn = -1e18;
	for (int i = 1;i <= n;i ++)
	{
		int x;
		cin >> x;
		maxn = max(maxn, x);
		div(x);
	}
	for (int i = 1;i <= n;i ++)
	{
		while (vis[maxn] < i)
		{
			maxn --;
		}
		cout << maxn << endl;
	 }
	return 0;
}
原文