zoukankan      html  css  js  c++  java
  • Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) B. Divisiblity of Differences

    地址:

    题目:

    B. Divisiblity of Differences
    time limit per test
    1 second
    memory limit per test
    512 megabytes
    input
    standard input
    output
    standard output

    You are given a multiset of n integers. You should select exactly k of them in a such way that the difference between any two of them is divisible by m, or tell that it is impossible.

    Numbers can be repeated in the original multiset and in the multiset of selected numbers, but number of occurrences of any number in multiset of selected numbers should not exceed the number of its occurrences in the original multiset.

    Input

    First line contains three integers nk and m (2 ≤ k ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — number of integers in the multiset, number of integers you should select and the required divisor of any pair of selected integers.

    Second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the numbers in the multiset.

    Output

    If it is not possible to select k numbers in the desired way, output «No» (without the quotes).

    Otherwise, in the first line of output print «Yes» (without the quotes). In the second line print k integers b1, b2, ..., bk — the selected numbers. If there are multiple possible solutions, print any of them.

    Examples
    input
    3 2 3
    1 8 4
    output
    Yes
    1 4
    input
    3 3 3
    1 8 4
    output
    No
    input
    4 3 5
    2 7 7 7
    output
    Yes
    2 7 7

    思路:
      如果(a-b)%m==0 ==> a%m = b%m.
      所以判断求出每个数膜m余多少就好了。
     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define MP make_pair
     6 #define PB push_back
     7 typedef long long LL;
     8 typedef pair<int,int> PII;
     9 const double eps=1e-8;
    10 const double pi=acos(-1.0);
    11 const int K=1e6+7;
    12 const int mod=1e9+7;
    13 
    14 int n,m,k,a[K],cnt[K];
    15 
    16 int main(void)
    17 {
    18     cin>>n>>k>>m;
    19     for(int i=1,x;i<=n;i++)
    20         scanf("%d",a+i),cnt[a[i]%m]++;
    21     for(int i=0;i<m;i++)
    22     if(cnt[i]>=k)
    23     {
    24         printf("Yes
    ");
    25         for(int j=1,num=0;j<=n;j++)
    26         if(a[j]%m==i)
    27         {
    28             printf("%d ",a[j]);
    29             if(++num==k)
    30                 return 0;
    31         }
    32     }
    33     printf("No
    ");
    34     return 0;
    35 }
  • 相关阅读:
    【Cocos2d-X游戏实战开发】捕鱼达人之开发前准备工作(一)
    NetBeans + Xdebug 调试WordPress
    【Cocos2d-X游戏实战开发】捕鱼达人之单例对象的设计(二)
    源代码静态分析工具
    Flash Builder 条件编译的实现
    Maven插件之portable-config-maven-plugin(不同环境打包)
    生成8位随机不重复的数字编号
    【剑指Offer学习】【面试题63:二叉搜索树的第k个结点】
    51nod 1413:权势二进制
    leetcode_Isomorphic Strings _easy
  • 原文地址:https://www.cnblogs.com/weeping/p/7680505.html
Copyright © 2011-2022 走看看