cf--#730--div2

A. Exciting Bets

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Welcome to Rockport City!

It is time for your first ever race in the game against Ronnie. To make the race interesting, you have bet a dollars and Ronnie has bet b dollars. But the fans seem to be disappointed. The excitement of the fans is given by gcd(a,b), where gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y. To make the race more exciting, you can perform two types of operations:

Increase both a and b by 1.
Decrease both a and b by 1. This operation can only be performed if both a and b are greater than 0.
In one move, you can perform any one of these operations. You can perform arbitrary (possibly zero) number of moves. Determine the maximum excitement the fans can get and the minimum number of moves required to achieve it.

Note that gcd(x,0)=x for any x≥0.

Input
The first line of input contains a single integer t (1≤t≤5⋅103) — the number of test cases.

The first and the only line of each test case contains two integers a and b (0≤a,b≤1018).

Output
For each test case, print a single line containing two integers.

If the fans can get infinite excitement, print 0 0.

Otherwise, the first integer must be the maximum excitement the fans can get, and the second integer must be the minimum number of moves required to achieve that excitement.

Example
input
4
8 5
1 2
4 4
3 9
output
3 1
1 0
0 0
6 3
Note
For the first test case, you can apply the first operation 1 time to get a=9 and b=6. It can be shown that 3 is the maximum excitement possible.

For the second test case, no matter how many operations you apply, the fans will always have an excitement equal to 1. Since the initial excitement is also 1, you don’t need to apply any operation.

For the third case, the fans can get infinite excitement by applying the first operation an infinite amount of times.

For the fourth test case, you can apply the second operation 3 times to get a=0 and b=6. Since, gcd(0,6)=6, the fans will get an excitement of 6.

算法:math + 思维
一道数学题,首先我们得知道gcd(a ,b) == gcd(a - b, b).
而a - b是不会改变的,所以只有b会改变,也就是说不管怎么改变,最大的gcd都是a - b.
为了达到所需的gcd,我们需要用尽可能少的操作使b成为g=a-b的倍数。有两种方法可以做到这一点–将b减少到小于或等于b的g的最大倍数,或者将b增加到大于b的g的最小倍数。显然,我们将选择这两者中的最小值。同时注意到,a mod g = b mod g,因为a=b+g。所以,如果我们用a或b来确定最小操作数并不重要

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main()
{
	int T;
	cin >> T;
	
	while(T --)
	{
		long long a,b;
		scanf("%lld%lld",&a, &b);
		
		if(a == b)
		{
			printf("0 0\n");
		}
		else
		{
			long long g = abs(a - b);
			long long t = min(b % g, g - b % g); // 也可以把b换成a 
			printf("%lld %lld\n",g, t);
		}
	}
	
	return 0;
}

B. Customising the Track

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Highway 201 is the most busy street in Rockport. Traffic cars cause a lot of hindrances to races, especially when there are a lot of them. The track which passes through this highway can be divided into n sub-tracks. You are given an array a where ai represents the number of traffic cars in the i-th sub-track. You define the inconvenience of the track as ∑i=1n∑j=i+1n|ai−aj|, where |x| is the absolute value of x.

You can perform the following operation any (possibly zero) number of times: choose a traffic car and move it from its current sub-track to any other sub-track.

Find the minimum inconvenience you can achieve.

Input
The first line of input contains a single integer t (1≤t≤10000) — the number of test cases.

The first line of each test case contains a single integer n (1≤n≤2⋅105).

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, print a single line containing a single integer: the minimum inconvenience you can achieve by applying the given operation any (possibly zero) number of times.

Example
input
3
3
1 2 3
4
0 1 1 0
10
8 3 6 11 5 2 1 7 10 4
output
0
4
21
Note
For the first test case, you can move a car from the 3-rd sub-track to the 1-st sub-track to obtain 0 inconvenience.

For the second test case, moving any car won’t decrease the inconvenience of the track.

** 算法:math + 思维**
这题只要把n条公路加起来取模于n,为0,说明可以每条路都可以一样,也就为0.
如果不为0,则为(sum % n) * (n - sum % n).

#include <iostream>
using namespace std;

const int N = 200010;

int a[N];

int main()
{
	int T;
	cin >> T;
	
	while(T --)
	{
		int n;
		cin >> n;
		int sum = 0;
		for(int i = 0; i < n; i ++)
		{
			cin >> a[i];
			sum = (sum + a[i]) % n;
		}
		
		if(sum == 0)
			cout << '0' << endl;
		else
			cout << (long long)sum * (n - sum) << endl;
	}
	
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值