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. C# 给图片添加透明的文字、图片水印

    #region 添加水印 /// <summary> /// 添加文字水印 /// </summary> /// <param name="image" ...

  2. docker run hello-world失败

    提示镜像拉取失败,解决方案 到 https://cr.console.aliyun.com/   注册一个账户 列表中就有 加速器 启动  Docker 端  右键  选择配置 在Docker Dae ...

  3. Python的特殊属性和魔法函数

    python中有很多以下划线开头和结尾的特殊属性和魔法函数,它们有着很重要的作用. 1.__doc__:说明性文档和信息,python自建,不需要我们定义. # -*- coding:utf- -*- ...

  4. 玩转php缓存memcache

    记录 一.本地安装 链接:https://www.cnblogs.com/jkko123/p/6294669.html 二.linux安装

  5. winform程序使用clickonce方式发布之后点击安装没反应

    可能是少了index.html和web.config两个文件,这两个文件为什么没有在发布的时候生成,还有怎么影响安装的后续研究

  6. centos7用docker安装elasticsearch5.6.13的主从

    说明: 准备2台机器,我这里有192.168.0.170 和 192.168.0.169 192.168.0.170 作为master 192.168.0.169 作为普通node 一.环境1.doc ...

  7. 在Delphi中获取和修改文件的时间

    转载自 http://www.cnblogs.com/jieke/archive/2013/01/11/2855782.html 本文介绍了在Delphi中利用系统函数和Windows API函数调用 ...

  8. 百度编辑器 Ueditor使用记录

    Ueditor官网: http://fex.baidu.com/ueditor/#dev-bale_width_grunt UeditorAPI文档: https://ueditor.baidu.co ...

  9. ThinkPHP5.0中Request请求对象的使用和常用的操作

    request的使用 第一种方法 在控制器头部添加request引用 然后在方法里调用 ‘instance’类 然后在调用方法: public function index($name='name') ...

  10. ASP.NET后台取html控件值方式

    1.Request.Form[“cbName”]: 可以在后台取到所有为name 为的控件的value值 2.可以通过 把html控件的值付给HiddenField,然后后台调用 3.就是自定义属性 ...