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

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

Input

第1行:2个数N, K,中间用空格分隔,N表示元素的数量,K为可以移除的数的数量(1 <= N <= 5000, 0 <= K <= 4, 1 <= a[i] <= 1000000)。

Output

输出符合条件的最小的M。

Input示例

5 1
1
2
10
11
12

Output示例

4——————————————————————————————————————————跟着tjm大爷写了一波首先 根据抽屉原理显然m>=(n-K)那么我们可以枚举(n-k)到mx的所有情况当然这样肯定会T怎样去计算几个数同余的情况呢如果v[i]≡v[j](mod m),则有m|(v[i]-v[j]),因此我们可以n^2预处理一波每次枚举到一个m我们可以计算他所有的倍数在 n 个数差中出现的情况(cnt)如果cnt>=(k+1)*k/2 那么至少有k+1个数同余(这是最坏情况下)加了这样一波剪枝之后就完全可以AC辣

一开始数组开小T了(我居然不知道o2下数组开小了会T QAQ) 改了一波之后还是蛮快的2333

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int M=1e5+7,N=1000007;
int read(){
    int ans=0,f=1,c=getchar();
    while(c<‘0‘||c>‘9‘){if(c==‘-‘) f=-1; c=getchar();}
    while(c>=‘0‘&&c<=‘9‘){ans=ans*10+(c-‘0‘); c=getchar();}
    return ans*f;
}
int n,k,mx,v[M],vis[N],f[N];
int main()
{
    n=read(); k=read();
    if(n+1<=k) return puts("1"),0;
    for(int i=1;i<=n;i++) v[i]=read(),mx=max(mx,v[i]);
    sort(v+1,v+1+n);
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            f[v[j]-v[i]]++;
    for(int i=n-k;i<=mx;i++){
        int cnt=0;
        for(int j=1;i*j<=mx;j++) cnt+=f[i*j];
        if(cnt>(k*(k+1)>>1)) continue;
        cnt=0;
        for(int j=1;j<=n;j++){
            int now=v[j]%i;
            if(vis[now]!=i) vis[now]=i;
            else cnt++;
        }
        if(cnt<=k) return printf("%d\n",i),0;
    }
    return 0;
}

 
时间: 2024-08-07 05:18:05

51nod 1217 Minimum Modular(数论+暴力)的相关文章

uva 10560 - Minimum Weight(数论)

题目连接:uva 10560 - Minimum Weight 题目大意:给出n,问说至少需要多少个不同重量的砝码才能称量1~n德重量,给出所选的砝码重量,并且给出k,表示有k个重量需要用上述所选的砝码测量. 解题思路:重量为1的砝码肯定要选,它可以表示到1的重量,那么下一个砝码的重量肯定选择3(2?1+1),这样1,3分别可以用一个砝码表示,而2,4分别为3-1和3+1,这样1~4的重量也都可以表示.于是有公式ai=si?1?2+1. #include <cstdio> #include &

数论/暴力 Codeforces Round #305 (Div. 2) C. Mike and Frog

题目传送门 1 /* 2 数论/暴力:找出第一次到a1,a2的次数,再找到完整周期p1,p2,然后以2*m为范围 3 t1,t2为各自起点开始“赛跑”,谁落后谁加一个周期,等到t1 == t2结束 4 详细解释:http://blog.csdn.net/u014357885/article/details/46044287 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #in

UVA 617 - Nonstop Travel(数论+暴力枚举)

题目链接:617 - Nonstop Travel 题意:给定一些红绿灯,现在速度能在30-60km/h之内,求多少个速度满足一路不遇到红灯. 思路:暴力每一个速度,去判断可不可以,最后注意下输出格式即可 代码: #include <stdio.h> #include <string.h> #include <math.h> const double esp = 1e-6; int n, vis[105]; struct D { double l; int g, y,

51nod 1732 婚姻介绍所 (暴力 / DP)

题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1732 题目: 51nod除了在做OJ之外,还开展了很多副业.婚姻介绍所就是其中之一. 对于一个客户,我们可以使用一个字符串来描述该客户的特质. 假设现在我们有两个客户A和B. A的特质字符串为:abcdefg B的特质字符串为:abcxyz 则A和B的匹配度f(A, B)为A和B的最长公共前缀的长度,即len('abc') = 3 由于最近51nod经费紧张,

Gym 100917C Constant Ratio 数论+暴力

题目: Description standard input/outputStatements Given an integer n, find out number of ways to represent it as the sum of two or more integers ai with the next property: ratio ai / ai - 1is the same positive integer for all possible i > 1. Input Inpu

HDU 1394 Minimum Inversion Number(暴力/线段树)

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=1394 暴力(Time:453ms) #include<stdio.h> #define MAXN 5005 int a[MAXN]; int main() { int n,i,j; while(~scanf("%d",&n)) { int ans=0x3f3f3f3f; for(i=0; i<n; i++) scanf("%d",&a

51nod - 1363 - 最小公倍数之和 - 数论

https://www.51nod.com/Challenge/Problem.html#!#problemId=1363 求\(\sum\limits_{i=1}^{n}lcm(i,n)\) 先换成gcd: \(\sum\limits_{i=1}^{n}\frac{i*n}{gcd(i,n)}\) 显而易见,枚举g: $ n * \sum\limits_{g|n} \frac{1}{g} \sum\limits_{i=1}^{n} i*[gcd(i,n)==g] $ 提g,没有下整符号: $

codeforces 113C C. Double Happiness(数论+暴力)

题目链接: codeforces 113C 题目大意: 找出在[l,r]中的素数t,满足t=a2+b2(a,b为任意正整数),输出这种素数的数量. 题目分析: 首先筛出3?108所有为素数的奇数(偶数除了2都不可能是素数,为了节约内存) 然后枚举范围内的每一个数,判断这个数是不是4*k+1的形式的素数,如果是,那么这个数能够划分成a2+b2的形式,否则不行. AC代码: #include <iostream> #include <cstring> #include <cstd

Uva 10892 LCM Cardinality (数论/暴力)

题意:给出数n,求有多少组A,B的最小公约数为n; 思路:3000ms,直接暴力寻找,找到所有能把n整除的数 pi, 枚举所有pi 代码: #include <iostream> #include <cstdio> #include <vector> #define ll long long using namespace std; ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } int