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

In a single move, you can choose any position ii between 11 and nn and increase aiai by 11 .

Let's calculate crcr (0≤r≤m−1)0≤r≤m−1) — the number of elements having remainder rr when divided by mm . In other words, for each remainder, let's find the number of corresponding elements in aa with that remainder.

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

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

Input

The first line of input contains two integers nn and mm (1≤n≤2⋅105,1≤m≤n1≤n≤2⋅105,1≤m≤n ). It is guaranteed that mm is a divisor of nn .

The second line of input contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤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 00 to m−1m−1 , the number of elements of the array having this remainder equals nmnm .

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 10181018 .

Examples

Input
  1. 6 3
    3 2 0 6 10 12
Output
  1. 3
    3 2 0 7 10 14
Input
  1. 4 2
    0 1 2 3
Output
  1. 0
    0 1 2 3
  2.  
  3. 分析:
    题目意思
    首先给出nm
    第二行给出n个数
    要求这n个数分为m组,组内n/m个数,要求第一组数%m==0,第二组数%m==1
    m组数%m==m-1,对于不满足要求的数,每次可以加1,问你最少的操作次数和所有操作完成之和的n个数
    拿样例来说吧
    3 2 0 6 10 12
    n==6,m==3
    6个数,分为3组,每组两个
    第一组%3==0
    第二组%3==1
    第三组%3==2
    30一组,%3==0
    76+1=7)和10一组 %3==1
    21412+2=14)一组 %3==2
    最少操作次数3
    操作之后的数组:
    3 2 0 7 10 14
  4.  
  5. 做法:
    利用set把可能的余数0m-1存起来(都是%m嘛),这个叫做目标余数
    然后从原数组第一个开始遍历
    x=a[1]%mx叫做原余数
    找到和原余数最近的没有用过的目标余数(在set里面找)
    原余数可能会大于目标余数(目标余数只能出现n/m次,因为一组只能由n/m个数,达到n/m次数的目标余数就会删除)
    那么这时候目标余数就是取set里面第一个
  6.  
  7. 取第一个的原因:对数组里面的数只能执行加的操作,比如现在原余数是2,但是set里面最大的目标余数都只有1(目标余数2出现了n/m次,被删了),因为只有加的操作,由要使得
    原余数2变成set里面有的最近的目标余数,所以就取set里面第一个,比如5%3==22是原余数,找到最近的目标余数这样进行操作的次数就会最少,如果目标余数里面还有2的话,就不要进行加操作了,直接取目标余数就好了,但是现在目标余数没有2了,最大只有1
    ,所以我们目标余数取0(如果0还有的话,反正取set里面第一个(set默认升序)),取第一个的概念:有点环的概念
  8.  
  9. 取过的目标余数就标记一下,满足了出现n/m次就从set里面删除
  1. #include <iostream>
  2. #include <cstdio>
  3. #include<stdio.h>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<math.h>
  7. #include<memory>
  8. #include<queue>
  9. #include<vector>
  10. #include<set>
  11. using namespace std;
  12. typedef long long LL;
  13. #define max_v 200005
  14. int n,m;
  15. LL a[max_v];
  16. LL c[max_v];
  17. LL sum;
  18. int main()
  19. {
  20. while(~scanf("%d %d",&n,&m))
  21. {
  22. set<int> s;
  23. set<int>::iterator it;
  24. memset(c,,sizeof(c));
  25. sum=;
  26.  
  27. for(int i=;i<m;i++)
  28. s.insert(i);
  29.  
  30. for(int i=;i<=n;i++)
  31. {
  32. scanf("%lld",&a[i]);
  33. int x=a[i]%m;
  34. int y;
  35. if(x>*s.rbegin())//s中删除某个后,可能余数会大于s里面最大的,那么余数肯定选择s里面第一个
  36. y=*s.begin();
  37. else
  38. y=*s.lower_bound(x);//大于等于x的第一个
  39. c[y]++;//标记位加1
  40. if(c[y]==n/m)//该余数用过了n/m次
  41. s.erase(y);//删除用过了n/m次的余数
  42. a[i]+=((y-x)+m)%m;
  43. sum+=((y-x)+m)%m;
  44. }
  45. printf("%lld\n",sum);
  46. for(int i=;i<=n;i++)
  47. printf("%lld ",a[i]);
  48. printf("\n");
  49. }
  50. return ;
  51. }
  1.  

CodeForces - 999D Equalize the Remainders (模拟+set)的更多相关文章

  1. Codeforces 999D Equalize the Remainders (set使用)

    题目连接:Equalize the Remainders 题意:n个数字,对m取余有m种情况,使得每种情况的个数都为n/m个(保证n%m=0),最少需要操作多少次? 每次操作可以把某个数字+1.输出最 ...

  2. D. Equalize the Remainders set的使用+思维

    D. Equalize the Remainders set的学习::https://blog.csdn.net/byn12345/article/details/79523516 注意set的end ...

  3. CodeForces.158A Next Round (水模拟)

    CodeForces.158A Next Round (水模拟) 题意分析 校赛水题的英文版,坑点就是要求为正数. 代码总览 #include <iostream> #include &l ...

  4. D. Equalize the Remainders (set的基本操作)

    D. Equalize the Remainders time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  5. D. Equalize the Remainders 解析(思維)

    Codeforce 999 D. Equalize the Remainders 解析(思維) 今天我們來看看CF999D 題目連結 題目 略,請直接看原題 前言 感覺要搞個類似\(stack\)的東 ...

  6. Codeforces 747C:Servers(模拟)

    http://codeforces.com/problemset/problem/747/C 题意:有n台机器,q个操作.每次操作从ti时间开始,需要ki台机器,花费di的时间.每次选择机器从小到大开 ...

  7. Codeforces 740A. Alyona and copybooks 模拟

    A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...

  8. Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  9. CodeForces 670 A. Holidays(模拟)

    Description On the planet Mars a year lasts exactly n days (there are no leap years on Mars). But Ma ...

随机推荐

  1. K:栈相关的算法

    本博文总结了常见的应用栈来进行实现的相关算法 ps:点击相关问题的标题,即可进入相关的博文进行查看其算法的思想及其实现,这篇博文更多的是作为目录使用 大数加法:在java中,整数是有最大上限的.所谓大 ...

  2. 如何使用canvas进行2d绘图

    canvas 的 2D context 可以绘制简单的 2D 图形.它的 2D context 坐标开始于 <canvas> 元素的左上角,原点坐标是(0,0).所有的坐标值都基于这个原点 ...

  3. 微服务架构之spring cloud zipkin

    Spring Cloud Zipkin是微服务的链路跟踪组件,帮助详细了解一次request&response的总计时,及每个微服务的消耗时间.微服务名称.异常信息等等过程信息. (一) 版本 ...

  4. Maven-pom-configuration

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  5. 131.005 Unsupervised Learning - Cluster | 非监督学习 - 聚类

    @(131 - Machine Learning | 机器学习) 零. Goal How Unsupervised Learning fills in that model gap from the ...

  6. 【疑难杂症01】TypeError: alert is not a function

    一.背景 话说今天在调试js的时候,碰到一个很奇怪的问题,现记录一下.当使用alert()函数弹出提示时,总是报错,你没看错,alert函数报错了. 二.详细说明 当时正在做一个关于告警的页面展示功能 ...

  7. JS Error 内置异常类型 处理异常 Throw语句

    Exceptional Exception Handling in JavaScript       MDN资料 Anything that can go wrong, will go wrong. ...

  8. 京东原来你运用的这玩意,不错,我也要!! ContainerDNS

    转自社区 ContainerDNS 本文介绍的 DNS 命名为 ContainerDNS,作为京东商城软件定义数据中心的关键基础服务之一,具有以下特点: 分布式,高可用 自动发现服务域名 后端探活 易 ...

  9. Sqlserver新建随机测试数据

    USE Test --使用数据库Test(如果没有则需要新建一个) ----1.新建一个users表 create table users( uId int primary key identity( ...

  10. maven将依赖的包一起打包

    把以下内容输入到pom中即可 <build> <plugins> <!-- 将项目的依赖包复制到 target/lib --> <plugin> < ...