Equalize the Remainders

You are given an array consisting of n integers a1,a2,…,an, and a positive integer m. It is guaranteed that m is a divisor of n

.

In a single move, you can choose any position i

between 1 and n and increase ai by 1

.

Let's calculate cr

(0≤r≤m−1) — the number of elements having remainder r when divided by m. In other words, for each remainder, let's find the number of corresponding elements in a

with that remainder.

Your task is to change the array in such a way that c0=c1=⋯=cm−1=nm

.

Find the minimum number of moves to satisfy the above requirement.

Input

The first line of input contains two integers n

and m (1≤n≤2⋅105,1≤m≤n). It is guaranteed that m is a divisor of n

.

The second line of input contains n

integers a1,a2,…,an (0≤ai≤109

), the elements of the array.

Output

In the first line, print a single integer — the minimum number of moves required to satisfy the following condition: for each remainder from 0

to m−1, the number of elements of the array having this remainder equals nm

.

In the second line, print any array satisfying the condition and can be obtained from the given array with the minimum number of moves. The values of the elements of the resulting array must not exceed 1018

.

Examples

Input

6 3
3 2 0 6 10 12

Output

3
3 2 0 7 10 14 

Input

4 2
0 1 2 3

Output

0
0 1 2 3 


 

题意:给你n个数和一个x,你可以使该数组其中一个数增加1,这算一次操作,

问你最少多少次操作,能使得数组中的所有数对x求余后  1~(x-1)出现n/x次(保证x能整除n)

看题解之后的理解:

思路:对每个数求余,若所求的余数不多,就不改变原值,若多了,就将原值改变,

怎样改变?因为只能将值增加,即只能将余数往上加,即找它最近的比它大的余数,若不存在,说明这个周期找完了,从下个周期找 现存最小的余数

用set记录还剩多少个余数,再开个数组记录每个余数出现的个数,当余数出现n/m次,就将余数从set中去掉,再找该余数需要变换到的值,用(x+m-d)%m求出所需步数。

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define eps 1e-8
#define PI acos(-1)
#define INF 0x3f3f3f3f
using namespace std;
const int N=200000 + 10 ;
typedef long long int LL;
const int dir[4][2]= { {1,0},{0,1},{-1,0},{0,-1} };
 LL a[N],c[N];
int GCD(int a,int b)
{
    return b ? GCD(b,a%b) : a;
}

int main()
{
    sets;
    LL i,j,n,m,ans=0;
    cin>>n>>m;
    for(i=0; i*s.rbegin())
            x=*s.begin();
        else
            x=*s.lower_bound(d);
        if(++c[x]==n/m) s.erase(x);
        ans+=(x+m-d)%m;
        a[i]+=(x+m-d)%m;
    }
    printf("%lld\n",ans);
    for(i=0; i

 

你可能感兴趣的:(思维题)