A.Burenka Plays with Fractions
题目描述
Burenka came to kindergarden. This kindergarten is quite strange, so each kid there receives two fractions ( $ \frac{a}{b} $ and $ \frac{c}{d} $ ) with integer numerators and denominators. Then children are commanded to play with their fractions.
Burenka is a clever kid, so she noticed that when she claps once, she can multiply numerator or denominator of one of her two fractions by any integer of her choice (but she can’t multiply denominators by $ 0 $ ). Now she wants know the minimal number of claps to make her fractions equal (by value). Please help her and find the required number of claps!
输入格式
The first line contains one integer $ t $ ( $ 1 \leq t \leq 10^4 $ ) — the number of test cases. Then follow the descriptions of each test case.
The only line of each test case contains four integers $ a $ , $ b $ , $ c $ and $ d $ ( $ 0 \leq a, c \leq 10^9 $ , $ 1 \leq b, d \leq 10^9 $ ) — numerators and denominators of the fractions given to Burenka initially.
输出格式
For each test case print a single integer — the minimal number of claps Burenka needs to make her fractions equal.
样例 #1
样例输入 #1
8
2 1 1 1
6 3 2 1
1 2 2 3
0 1 0 100
0 1 228 179
100 3 25 6
999999999 300000000 666666666 100000000
33 15 0 84
样例输出 #1
1
0
2
0
1
1
1
1
提示
In the first case, Burenka can multiply $ c $ by $ 2 $ , then the fractions will be equal.
In the second case, fractions are already equal.
In the third case, Burenka can multiply $ a $ by $ 4 $ , then $ b $ by $ 3 $ . Then the fractions will be equal ( $ \frac{1 \cdot 4}{2 \cdot 3} = \frac{2}{3} $ ).
题意
给你a,b,c,d四个数,可以组成a/b 和c/d,判断需要进行多少次操作,两个分数可以相等
思路
将两个分数进行交叉相乘,即ad = a * d
,cb = c * b
。
分情况讨论:
- 如果两个分数中,最多只有一个分子等于0,即
(a == 0 && c != 0) || (a != 0 && c == 0)
,那么答案就是1
,另一个不为0
的分数乘0。 - 如果
ad == cb
,那么两个分数一定相等,答案就是0
- 如果
ad != cb
,并且大数是小数的倍数,即大数 % 小数 == 0
,答案就是1
,反之为2
。
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
void solve()
{
ll a , b , c ,d;
cin >> a >> b >> c >> d;
ll ad = a * d;
ll cb = c * b;
ll ans = 0;
if( (a == 0 && c != 0) || (a != 0 && c == 0))
ans = 1;
else if(ad != cb)
{
ll mx = max(ad , cb);
ll mn = min(ad , cb);
if(mx % mn == 0)
ans = 1;
else
ans = 2;
}
// cout << "ans = ";
cout << ans << endl;
}
int main()
{
int T;
cin >> T;
while(T --) solve();
return 0;
}
B.Interesting Sum
题目描述
You are given an array $ a $ that contains $ n $ integers. You can choose any proper subsegment $ a_l, a_{l + 1}, \ldots, a_r $ of this array, meaning you can choose any two integers $ 1 \le l \le r \le n $ , where $ r - l + 1 < n $ . We define the beauty of a given subsegment as the value of the following expression:
$ $KaTeX parse error: Can't use function '$' in math mode at position 200: …ldots, a_{r}). $̲ $
Please find the maximum beauty among all proper subsegments.
输入格式
The first line contains one integer $ t $ ( $ 1 \leq t \leq 1000 $ ) — the number of test cases. Then follow the descriptions of each test case.
The first line of each test case contains a single integer $ n $ $ (4 \leq n \leq 10^5) $ — the length of the array.
The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ( $ 1 \leq a_{i} \leq 10^9 $ ) — the elements of the given array.
It is guaranteed that the sum of $ n $ over all test cases does not exceed $ 10^5 $ .
输出格式
For each testcase print a single integer — the maximum beauty of a proper subsegment.
样例 #1
样例输入 #1
4
8
1 2 2 3 1 5 6 1
5
1 2 3 100 200
4
3 3 3 3
6
7 8 3 1 1 8
样例输出 #1
9
297
0
14
提示
In the first test case, the optimal segment is $ l = 7 $ , $ r = 8 $ . The beauty of this segment equals to $ (6 - 1) + (5 - 1) = 9 $ .
In the second test case, the optimal segment is $ l = 2 $ , $ r = 4 $ . The beauty of this segment equals $ (100 - 2) + (200 - 1) = 297 $ .
题意
给你一个长度为n的数组,需要你找出l,r,使下列式子的值最大。
思路
要找到蓝色区域的最大值和最小值,和红色区域的最大值和最小值,要使式子的值最大,那么,两个最大值应该是整个数组的最大值,两个最小值是整个数组的最小值
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 1e5 + 10;
int a[N];
void solve()
{
int n;
cin >> n;
for(int i = 0;i < n;i ++)
cin >> a[i];
sort(a,a+n);
// cout << "ans = ";
cout << a[n-1] - a[0] + a[n-2] - a[1] << endl;
}
int main()
{
int T;
cin >>T;
while(T --) solve();
return 0;
}
C.Corners
Corners
题目描述
You are given a matrix consisting of $ n $ rows and $ m $ columns. Each cell of this matrix contains $ 0 $ or $ 1 $ .
Let’s call a square of size $ 2 \times 2 $ without one corner cell an L-shape figure. In one operation you can take one L-shape figure, with at least one cell containing $ 1 $ and replace all numbers in it with zeroes.
Find the maximum number of operations that you can do with the given matrix.
输入格式
The first line contains one integer $ t $ ( $ 1 \leq t \leq 500 $ ) — the number of test cases. Then follow the descriptions of each test case.
The first line of each test case contains two integers $ n $ and $ m $ ( $ 2 \leq n, m \leq 500 $ ) — the size of the matrix.
Each of the following $ n $ lines contains a binary string of length $ m $ — the description of the matrix.
It is guaranteed that the sum of $ n $ and the sum of $ m $ over all test cases does not exceed $ 1000 $ .
输出格式
For each test case output the maximum number of operations you can do with the given matrix.
样例 #1
样例输入 #1
4
4 3
101
111
011
110
3 4
1110
0111
0111
2 2
00
00
2 2
11
11
样例输出 #1
8
9
0
2
提示
In the first testcase one of the optimal sequences of operations is the following (bold font shows l-shape figure on which operation was performed):
- Matrix before any operation was performed: 101111011110
- Matrix after $ 1 $ operation was performed: 100101011110
- Matrix after $ 2 $ operations were performed: 100100011110
- Matrix after $ 3 $ operations were performed: 100100010110
- Matrix after $ 4 $ operations were performed: 100000010110
- Matrix after $ 5 $ operations were performed: 100000010100
- Matrix after $ 6 $ operations were performed: 100000000100
- Matrix after $ 7 $ operations were performed: 000000000100
- Matrix after $ 8 $ operations were performed: 000000000000
In the third testcase from the sample we can not perform any operation because the matrix doesn’t contain any ones.
In the fourth testcase it does not matter which L-shape figure we pick in our first operation. We will always be left with single one. So we will perform $ 2 $ operations.
题意
给你一个01矩阵,每次选择一个L型子段,并将字段内的所有值变为0,求让这个01矩阵全变为0,最多需要进行多少次
思路【来自CurleyD】
如果在这个01矩阵中存在一种最多只有1个1的L型子段,那么就没有浪费1,答案就是1的个数。
答案:矩阵中1的个数 - 构建L型子段时浪费的1
抵消1的三种情况:
- 矩阵全为1,浪费两个1
- L型子段中最多有一个1,不浪费
- L型子段中至少有两个1,浪费1个1
C++代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 510;
char s[N][N];
int a[N][N];
void solve()
{
int n , m;
cin >> n >> m;
int cnt = 0;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
cin >> s[i][j];
a[i][j] = s[i][j] - '0';
if(a[i][j] == 1)
cnt ++;
}
}
//cout << "ans = ";
//矩阵中至少存在一个L型子段中只有一个1,没有浪费1
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
if(i + 1 <= n && j + 1 <= m && a[i][j] + a[i+1][j] + a[i][j+1] + a[i+1][j+1] <= 2)
{
cout << cnt << endl;
return ;
}
}
}
//矩阵全为1,至少浪费两个1
if(cnt == n * m)
cout << cnt - 2 << endl;
else
cout << cnt - 1 << endl;
return ;
}
int main()
{
int T;
cin >> T;
while(T --) solve();
return 0;
}