Codeforces Round #730 (Div. 2) A~B

A. Exciting Bets

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard 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:

  1. Increase both \(a\) and b by \(1\).
  2. 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⋅10^3)\) — the number of test cases.

The first and the only line of each test case contains two integers \(a\) and \(b(0≤a,b≤10^{18})\).

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\).


题目大意

你有两个数 \(a,b\),可以进行无数次操作,每次操作可以选择:

  1. \(a,b\) 同时 \(+1\)
  2. \(a,b\) 同时 \(-1\)(需要确保操作前 \(a,b>0\)

定义兴奋值为 \(gcd(a,b)\),特殊的,\(gcd(x,0)=x,x\geq 0\)

问经过一定数量的操作后,兴奋值的最大值 和 得到此兴奋值所需的最小操作次数

特殊的,如果兴奋值可以为无穷大,则输出 0 0

PZ's Solution

  1. 优先假设 \(a>b\),则兴奋值一定为 \(a-b\),因为 \(a-b \geq gcd(a,b)\) ,而兴奋值一定要取最大值

证明:因为兴奋值为 \(gcd(a,b)\),则 \(a=Q \times A_1 \times \cdots \times A_n\)\(b=Q \times B_1 \times \cdots \times B_m\)

其中 \(Q=gcd(a,b)\)\(A_n\)\(B_m\) 均为它们自己的质因子(而在对方中不存在),则 \(a-b=Q(A_1\times \cdots \times A_n - B_1 \times \cdots \times B_m)\)

因为\(a>b\),提出公共的 \(Q\) 后依然大,故 $A_1\times \cdots \times A_n - B_1 \times \cdots \times B_m \geq 1 $(讨论结果均为整数)

故可以得到 \(a-b \geq gcd(a,b)\)

这里可以发现,\(a-b\)是永远不变的,而 \(a,b\) 经过操作后得到 新 \(gcd(a,b)\) 也肯定不会超过 \(a-b\) ,所以 \(a-b\) 即为最大值

  1. 设兴奋值为 \(ans_{excitement}=a-b\) ,则需要分情况讨论:
  • \(ans_{excitement}>b\),则可以选择让 \(b\) 减去 \(b\) 或者 让 \(b\) 加上 \(ans_{excitement}-b\)

\[a-b=ans_{excitement},\quad b-b=0,\quad gcd(ans_{excitement},0)=ans_{excitement} \]

\[a+ans_{excitement}-b=2\times ans_{excitement}, \quad b+ans_{excitement}-b=ans_{excitement},\quad gcd(2\times ans_{excitement},ans_{excitement})=ans_{excitement} \]

  • \(ans_{excitement} \leq b\),则可以选择让 \(b\)减去 \(b\%ans_{excitement}\) 或者让 \(b\) 加上 \(ans_{excitement}-b\%ans_{excitement}\)

如果 \(a-b=km\),则当 \(m=0\) 时有 \(a=b\),不然就有

\[a \% m =a - \lfloor \frac{a}{m}\rfloor m =b+km-\lfloor\frac{b+km}{m}\rfloor m =b-\lfloor \frac{b}{m}\rfloor m =b \% m \]

又有\(gcd(a,b)=gcd(b,a-b)=gcd(a,a-b)\)

显然,\(ans_{excitement}=a-b=km\),则有

\[gcd(a-b\%ans_{excitement},b-b\%ans_{excitement})=gcd(a-a\%ans_{excitement},ans_{excitement})=ans_{excitement} \]

\[gcd(a+ans_{excitement}-b\%ans_{excitement},b+ans_{excitement}-b\%ans_{excitement})=gcd(a+a\%ans_{excitement},ans_{excitement})=ans_{excitement} \]

  1. 特殊地,若 \(a=b\),则有 \(gcd(a,b)=a\),则可以通过无限次 \(+1\) 让兴奋值达到无穷大,按照要求输出 0 0

PZ.cpp

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
#define ll long long
int main(){
    int T; scanf("%d",&T); while(T--){
        ll a,b;
        scanf("%lld %lld",&a,&b);
        if(a<b) swap(a,b);
        ll ans_excitement=a-b;
        if(ans_excitement==0) puts("0 0");
        else if(ans_excitement>b) printf("%lld %lld\n",ans_excitement,min(ans_excitement-b,b));
        else if(ans_excitement<=b) printf("%lld %lld\n",ans_excitement,min(ans_excitement-b%ans_excitement,b%ans_excitement));
    }
    return 0;
}

B. Customising the Track

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard 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 \(a_i\) represents the number of traffic cars in the \(i\)-th sub-track. You define the inconvenience of the track as \(\sum_{i=1}^n \sum_{j=i+1}^n |a_i-a_j|\), 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⋅10^5)\).

The second line of each test case contains \(n\) integers \(a_1,a_2,…,a_n(0≤a_i≤10^9)\).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2⋅10^5\).

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.


题目大意

给你一个数组\(a\)\(a_i\)表示某条道路的车辆数量,定义堵车程度为 \(\sum_{i=1}^n \sum_{j=i+1}^n |a_i-a_j|\),而你现在可以进行任意次操作,每次操作将任意道路上的一辆车移动到任意一条道路上,问堵车程度最小可以是多少?

PZ's Solution

  1. 显然,如果 \(a_i=a_j\),则堵车程度自然就为 \(0\),所以很自然就能想到 让每个\(a_i\) 都能变为 \(\frac{\sum_{i=1}^n a_i}{n}\)

  2. 但同样显然,\(\frac{\sum_{i=1}^n a_i}{n}\)可能不是整数,所以只能先让每个数变为 \(\lfloor\frac{\sum_{i=1}^n a_i}{n}\rfloor\),则会剩下 \((\sum_{i=1}^n a_i )\%n\) 需要进行分配

  3. 最后显然,将 \((\sum_{i=1}^n a_i )\%n\) 分别给 \((\sum_{i=1}^n a_i) \%n\) 条道路,这样 这 \((\sum_{i=1}^n a_i) \%n\) 条道路相对于剩余的 \(n- (\sum_{i=1}^n a_i )\%n\) 条道路都多一辆车,则答案显然为 \((\sum_{i=1}^n a_i )\%n \times [n- (\sum_{i=1}^n a_i )\%n]\)

PZ.cpp

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
const int N=2e5+5;
int a[N];
int main(){
    int T; scanf("%d",&T); while(T--){
        int n;
        ll a_sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;++i){
            scanf("%d",&a[i]);
            a_sum+=a[i];
        }
        if(a_sum%n==0) puts("0");
        else{
            ll a_should_be=a_sum/n,a_sum_rest=a_sum%n;
            ll ans=a_sum_rest*(n-a_sum_rest);
            printf("%lld\n",ans);
        }
    }
    return 0;
}
posted @ 2021-07-08 15:47  PotremZ  阅读(289)  评论(2编辑  收藏  举报