zoukankan      html  css  js  c++  java
  • CodeForces

    Divisiblity of Differences

    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 n, k 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.


    Example
    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 

    题意:在n个数中找到k个数,k个数任意两个数的差都是m的倍数。

    思路:由同余定理可知。n1=k1*m+r1;n2=k2*m+r2;当r1=r2时,(n1-n2)%m就等于0;

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<map>
    #include<string.h>
    using namespace std;
    int a[100010],b[100010];
    int main()
    {
        int  n,k,m;
        while(~scanf("%d%d%d",&n,&k,&m))
        {
            for(int i=0; i<n; i++)
            {
                scanf("%d",&a[i]);
            }
            int flag=0;
            for(int i=0; i<n; i++)
            {
                int x=a[i]%m;
                b[x]++;
                if(b[x]==k)
                {
                    printf("Yes
    ");
                    int j=i;
                    while(j>=0)
                    {
                        if(a[j]%m==x)
                       printf("%d ",a[j]);
                       j--;
                    }
                    printf("
    ");
                    flag=1;
                    break;
                }
            }
            if(flag==0)
                printf("No
    ");
        }
    }
    
    



  • 相关阅读:
    sql server 全文搜索,同义词,链接服务器
    行版本控制,解决死锁问题
    SqlServer 官方教程文档
    变态强大的EF Core 拓展库 ELinq
    Jenkinsfile 学习
    Dockerfile+Jenkinsfile+GitLab轻松实现.NetCore程序的CI&CD
    JMeter 从入门到精通
    LinqPad工具
    Angular 部署到IIS上出现404问题
    es search教程
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053318.html
Copyright © 2011-2022 走看看