Minimum Modular
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the minimum modular m (m > 0), so that for every pair of the remaining integers(ai, aj), the following unequality holds: .

Input

The first line contains two integers n and k (1  ≤ n  ≤ 5000, 0 ≤ k ≤ 4), which we have mentioned above.

The second line contains n distinct integers a1, a2, ..., an (0 ≤ ai ≤ 106).

Output

Print a single positive integer — the minimum m.

Sample test(s)
input
7 0
0 2 3 6 7 12 18
output
13
input
7 1
0 2 3 6 7 12 18
output
7

参考:http://www.cnblogs.com/Lyush/archive/2013/05/14/3077258.html

题目大意:给你n个数,你可以从中删除最多k个数,使得剩余的所有数对m取余没有同余的,求最小的m。

解题思路:首先想到暴力,从小到大枚举m,然后判断n个数中对m取模同余个数有多少,如果超出k就枚举更大的m。然而这样的话,时间复杂度为O(n*1e6)。然后在网上找了博客看,但是有些地方当时自己感觉很不好理解的,这里做下自己的解释。1.首先这里用了一个剪枝,这个剪枝能节省大量时间。因为如果有k+1个数都是对m取模同余,那么只需删除k个数,就可以让剩下的数(只剩下一个数)不同余,那么从k+1个同余的数中取出2个数组成同余对的组合数就有C(2,k+1)种,即k*k+1/2种,那么如果对m取模同余的同余对的组合数大于k*k+1/2种,说明无法删除k个数使得剩下的数不同余。2.然后暴力判断此时满足1步骤的m作为模是否能满足同余的数小于k个。

#include<bits/stdc++.h>
using namespace std;
#define max(a,b) (a)>(b)?(a):(b)
const int maxn=1e6+100;
int num[maxn];
int a[5500];
bool flag[maxn];
int dif(int a,int b){
return a>b? a-b:b-a;
}
int main(){
int n,k,i,j,maxa,m,mark,sum,cn,mod;
maxa=-1;
while(scanf("%d%d",&n,&k)!=EOF){
memset(num,0,sizeof(num));
for(i=0;i<n;i++){
scanf("%d",&a[i]);
maxa=max(a[i],maxa);
}
//首先应知道a%m==b%m --> |a-b|%m==0
for(i=1;i<n;i++){
for(j=0;j<i;j++){
//不同类型的同余对各有多少
num[dif(a[i],a[j])]++;
}
}
for(m=1;m<=maxa;m++){
sum=0;
for(i=m;i<=maxa;i+=m){
//这里i+=m的原因是,这样能保证同余对 对于对之间也都是同余的,即这样挑出的组合中所有数都是同余的。
sum+=num[i]; //
if(sum>k*(k+1)/2){ //剪枝
break;
}
}
if(sum>k*(k+1)/2){
continue;
}
cn=0,mark=0;
for(j=0;j<n&&(!mark);j++){ //暴力判断m是否满足题目的要求
mod=a[j]%m;
if(!flag[mod]){
flag[mod]=1;
}else{
cn++;
if(cn>k){
mark=1;
}
}
}
for(j=0;j<n;j++){ //还原
flag[a[j]%m]=0;
}
if(!mark){
mark=m;
break;
}
}
printf("%d\n",mark);
}
return 0;
}

  

CF 303C——Minimum Modular——————【剪枝】的更多相关文章

  1. codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)

    You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...

  2. 51nod 1217 Minimum Modular

    N个不同的数a[1],a[2]...a[n],你可以从中去掉K个数,并且找到一个正整数M,使得剩下的N - K个数,Mod M的结果各不相同,求M的最小值. Input 第1行:2个数N, K,中间用 ...

  3. 51nod 1217 Minimum Modular(数论+暴力)

    根据抽屉原理显然m>=(n-K) 于是在[n-K,max(a1..an)+1]的范围中枚举m 考虑K=0的做法... 如果a[i]≡a[j](mod m),则有m|(a[i]-a[j]),只要O ...

  4. cf 609E.Minimum spanning tree for each edge

    最小生成树,lca(树链剖分(太难搞,不会写)) 问存在这条边的最小生成树,2种情况.1.这条边在原始最小生成树上.2.加上这条半形成一个环(加上),那么就找原来这条边2端点间的最大边就好(减去).( ...

  5. NOIP2018提高组金牌训练营——数论专题

    地址 https://www.51nod.com/live/liveDescription.html#!liveId=23 1187 寻找分数 给出 a,b,c,d, 找一个分数p/q,使得a/b & ...

  6. codeforce303C-Minimum Modular-剪枝,暴力

    Minimum Modular 题意:就是在一堆数字中,每一个数字对m取模不能等于这堆数字中的其他数字,同时给了K个机会可以删除一些数字.求最小的m: 思路:我一开始完全没思路,队长说的并查集什么的不 ...

  7. Codeforces Round #339 (Div.2)

    A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. codeforces 613B B. Skills(枚举+二分+贪心)

    题目链接: B. Skills time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. [CodeForces - 614D] D - Skills

    D - Skills Lesha plays the recently published new version of the legendary game hacknet. In this ver ...

随机推荐

  1. 「HNOI2013」切糕

    题目链接 戳我 \(Solution\) 对于这道题,我们首先来看看没有\(D\)这个约束的该如何做. 我们考虑构造最小割模型. 其实直接贪心就好了,选出每条路径上的最小值就好了(路径就是将每层的同一 ...

  2. 「HAOI2016」放棋子

    题目链接 戳这 前置知识 错位排序 Solution 我们可以观察发现,每一行的障碍位置对答案并没有影响. 于是我们可以将此时的矩阵化成如下形式: \[ 1\ \ 0\ \ 0\ \ 0\\ 0\ \ ...

  3. NSRange 范围

    前言 结构体,这个结构体用来表示事物的一个范围,通常是字符串里的字符范围或者集合里的元素范围. typedef struct _NSRange { NSUInteger location; // 表示 ...

  4. git中文乱码问题

    控制台中输入:git config --global core.quotepath false

  5. svn: Can’t convert string from ‘UTF-8’ to native encoding: 解决办法

    主要原因是linux的编码发生改动 只需该为 en_US.UFT-8即可 [具体操作可以查看]https://www.cnblogs.com/sz-xioabai/p/10523423.html

  6. 计算机基础知识和tcp详解

    计算机基础知识 作为应用软件开发程序员是写应用软件的,而应用软件必须应用在操作系统之上,调用操作系统接口,由操作系统控制硬件 比如客户端软件想要基于网络发送一条消息给服务端软件,流程是: 1.客户端软 ...

  7. php代码审计10审计会话认证漏洞

    挖掘经验:遇到的比较多的就是出现在cookie验证上面,通常是没有使用session来认证,直接将用户信息保存在cookie中      Session固定攻击:黑客固定住目标用户的session i ...

  8. Java设计模式之单例设计模式 入门实例

    一.基础概念 (1).单例设计模式:保证一个类在内存中的对象唯一性. (2).应用场景:数据都存储在配置文件的对象中,多个程序对同一个配置文件的对象进行操作.一个程序要基于另一个程序操作后的结果进行操 ...

  9. Reviewing notes 2.1 of Mathematical analysis

    Chapter2 Numerical sequence and function Cartesian product set If S and T are sets,then the cartesia ...

  10. SDUT OJ 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem Descri ...