1. 程式人生 > 其它 >Codeforces Round #730 (Div. 2) A~B

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