ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #815 (Div. 2) 【A~C】

2022-08-19 10:35:00  阅读:181  来源: 互联网

标签:case fractions Codeforces leq each test Div include 815


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:

$ $$$\max(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) - \min(a_{1}, a_{2}, \ldots, a_{l-1}, a_{r+1}, a_{r+2}, \ldots, a_{n}) + \max(a_{l}, \ldots, a_{r}) - \min(a_{l}, \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,使下列式子的值最大
image

思路

image
要找到蓝色区域的最大值和最小值,和红色区域的最大值和最小值,要使式子的值最大,那么,两个最大值应该是整个数组的最大值,两个最小值是整个数组的最小值

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;
}

标签:case,fractions,Codeforces,leq,each,test,Div,include,815
来源: https://www.cnblogs.com/heystar/p/16601119.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有