T1 P1482 Cantor 表(升级版)
时间复杂度与数据范围:
(以下分子用 表示,分母用 表示)
时间复杂度:。
细节点:
注意给出的分数不一定是最简分数。
注意:一定要先输出列在输出行!!!(被坑惨了)
思路:
分子乘上分子,分母乘上分母。
求出分子分母的 ,然后就将分数约分。
先输出分母,在输出分子。
代码:
#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 分数运算
时间复杂度与数据范围:
。
时间复杂度:
细节点:
注意数据范围,一定要开 。
如果答案是整数,那么就直接输出分子即可。
思路:
我们可以使用 循环读入,
while(cin >> ch >> x >> ch1 >> y)。
使用题目所给出的公式,其他题目均可使用的
的公式
也可以用我的方法:
先将分母同分,然后将两个分数相加,非常的麻烦。
分数需要进行约分。
代码:
#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 计算分数
时间复杂度与数据范围:
INT_MAX。 存在负数。
时间复杂度:。
细节点:
分母为 时输出分子。
减法的分数计算公式:
a / b - c / d == (a * d - b * c) / (b * d)
一定要提前开 !!!。
答案存在负数,输出是得格外注意:
1, 分母不能为负数
2,一定要化简为最简分数。思路:
向上一题一样,我们可以使用 循环读入,
while(cin >> ch >> x >> ch1 >> y)。
一定要判断是否是 还是 。
如果只有一个分数,也得在开头判断。
代码:
#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 普及组] 比例简化
时间复杂度与数据范围:
。
时间复杂度 :
细节点:
注意精度问题,可能精度出现误差。
,说明此时有最小值,左边的小于右边的。
思路:
双重循环从 到 枚举比例。
控制精度,将除法改成乘法,求出最小值。
注意 与 需要互质。
将 更 进行比较。
代码:
#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
时间复杂度与数据范围:
假如使用暴力算法:。
时间复杂度:
正解 :
细节点:
暴力:
直接暴力建边跑 。
正解:
gcd(i, j) = i,lcm(i, j) = j,代表 能被 整除。
如果 和 是倍数关系,那么就建立无向边, 到 的最短路径。
思路:
预期 分,实际上 分。
走到了 ,便权威正数,走一条答案一定会增加, 只能走到他的倍数上去, - 因数, - 最大公因数 ,但是 不可能去走他的倍数,因为那样的边权太大了,因此只能走因数。所以答案就是
代码:
#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
时间复杂度与数据范围:
时间复杂度:
细节点:
组成条件: 。
桶数组需要开 。
思路:
和 统计因数
每次统计的时候只能计算一次,问能否从 个数字中选取 个数保证 最大。
个数字中,是否有最大的因子,出现的次数到达了 次,有的话就是答案。
的个数越多,那么求解的答案单调不增。
注意:求解的是因数,而不是质因数。
代码:
#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;
}