2024年10月9日总结

· · 个人记录

T1 P1482 Cantor 表(升级版)

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

(以下分子用 fz 表示,分母用 fm 表示)

fz, fm \leq 10^4
时间复杂度:O( \log N)。

② 细节点:

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

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

3, 思路:

$2,$ 求出分子分母的 $\gcd$,然后就将分数约分。 $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 分数运算](https://www.luogu.com.cn/problem/U414963) #### $①$ 时间复杂度与数据范围: $fz,fm \le 20$。 ##### 时间复杂度:$O(N \log N)

② 细节点:

$2,$ 如果答案是整数,那么就直接输出分子即可。 #### $③$ 思路: $1,$ 我们可以使用 $while$ 循环读入, `while(cin >> ch >> x >> ch1 >> y)`。 $2,$ 使用题目所给出的公式,其他题目均可使用的 $$$ \frac{a}{b}+\frac{c}{d}=\frac{ad+cb}{bd} $$$ 的公式 也可以用我的方法: 先将分母同分,然后将两个分数相加,非常的麻烦。 $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 计算分数](https://www.luogu.com.cn/problem/P1572) #### $①$ 时间复杂度与数据范围: $fz,fm <= $ INT_MAX。 存在负数。 ##### 时间复杂度:$O(NlogN)$。 #### $②$ 细节点: $1,$ 分母为 $1$ 时输出分子。 $2,$ 减法的分数计算公式: `a / b - c / d == (a * d - b * c) / (b * d)` $3,$ 一定要提前开 $long long$ !!!。 $4,$ 答案存在负数,输出是得格外注意: ```c++ 1, 分母不能为负数 2,一定要化简为最简分数。 ``` #### $③$ 思路: $1,$ 向上一题一样,我们可以使用 $while$ 循环读入, `while(cin >> ch >> x >> ch1 >> y)`。 $2,$ 一定要判断是否是 $+$ 还是 $-$。 $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 普及组\] 比例简化](https://www.luogu.com.cn/problem/P2118) #### $①$ 时间复杂度与数据范围: $1 \le L \le 100$ 。 ##### 时间复杂度 :$O(L^2)

② 细节点:

$2,$ $a \times d - b \times c \le 0$,说明此时有最小值,左边的小于右边的。 #### $③$ 思路: $1,$ 双重循环从 $0$ 到 $L$ 枚举比例。 $2,$ 控制精度,将除法改成乘法,求出最小值。 $3,$ 注意 $A'$ 与 $B'$ 需要互质。 $4,$ 将 $(a \times b)$ 更 $min \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](https://www.luogu.com.cn/problem/P9484) #### $①$ 时间复杂度与数据范围: 假如使用暴力算法:$floyd$。 ##### 时间复杂度:$O(T n^3)

正解 :

O(\Sigma q log n)

② 细节点:

暴力:

正解: $1,$ `gcd(i, j) = i`,`lcm(i, j) = j`,代表 $j$ 能被 $i$ 整除。 $2,$ 如果 $i$ 和 $j$ 是倍数关系,那么就建立无向边,$x$ 到 $y$ 的最短路径。 #### $③$ 思路: $1,$ $floyd$ 预期 $40$ 分,实际上 $60$ 分。 $2,$ $x$ 走到了 $y$,便权威正数,走一条答案一定会增加,$x$ 只能走到他的倍数上去,$x$ - 因数,$x$ - 最大公因数 $(gcd(x, y))$ ,但是$x$ 不可能去走他的倍数,因为那样的边权太大了,因此只能走因数。所以答案就是 $(x - gcd(x, y)) + (y - gcd(x, y))

④ 代码:

#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(N \sqrt N)

② 细节点:

$2,$ 桶数组需要开 $10^6$。 #### $③$ 思路: $1,$ $a$ 和 $b$ 统计因数 每次统计的时候只能计算一次,问能否从 $n$ 个数字中选取 $k$ 个数保证 $gcd$ 最大。 $2,$ $ n$个数字中,是否有最大的因子,出现的次数到达了 $k$ 次,有的话就是答案。 $3,$ $gcd$ 的个数越多,那么求解的答案单调不增。 注意:求解的是因数,而不是质因数。 #### $④$ 代码: ```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; } ```