给定n个正整数,从中挑出k个数,满足:存在某一个m(m>=2),使得这k个数模m的余数相等。
求出k的最大值,并求出此时的m。如果有多组解使得k最大,你要在此基础上求出m的最大值。
标签:sample define time ble ace through 差值 can put
1 #include<bits/stdc++.h> 2 #define clr(x) memset(x,0,sizeof(x)) 3 #define clr_1(x) memset(x,-1,sizeof(x)) 4 #define INF 0x3f3f3f3f 5 #define LL long long 6 #define pb push_back 7 #define mod 1000000007 8 #define ls(i) (i<<1) 9 #define rs(i) (i<<1|1) 10 #define mp make_pair 11 #define fi first 12 #define se second 13 using namespace std; 14 const int N=1e7+10; 15 const int M=1e5+10; 16 int inf[N],prime[N],pre[N],g[N],num[N]; 17 int tot; 18 int gcd(int a,int b) 19 { 20 int c; 21 while(b) 22 c=a%b,a=b,b=c; 23 return a; 24 } 25 void init() 26 { 27 tot=0; 28 int n=10000000; 29 for(int i=2;i<=n;i++) 30 { 31 if(!inf[i]) 32 prime[++tot]=i,pre[i]=tot; 33 for(int j=1;j<=tot && prime[j]*i<=n;j++) 34 { 35 inf[prime[j]*i]=1; 36 pre[prime[j]*i]=j; 37 if(i%prime[j]==0) break; 38 } 39 } 40 return ; 41 } 42 int a[M],b[M]; 43 int main() 44 { 45 init(); 46 int n,m,k; 47 scanf("%d",&n); 48 for(int i=1;i<=n;i++) 49 scanf("%d",a+i); 50 int p=log10(n)+5; 51 srand(time(0)); 52 m=k=0; 53 while(p--) 54 { 55 56 int pos=rand()%n+1; 57 int tmp=0; 58 int minm=0,mink=0; 59 for(int i=1;i<=n;i++) 60 { 61 b[i]=abs(a[i]-a[pos]); 62 if(!b[i]) 63 tmp++; 64 } 65 for(int i=1;i<=n;i++) 66 { 67 int t=b[i]; 68 while(t && t!=1) 69 { 70 int temp=pre[t]; 71 num[temp]++,g[temp]=gcd(g[temp],b[i]); 72 if(mink<num[temp]) mink=num[temp],minm=g[temp]; 73 else if(mink==num[temp]) minm=max(g[temp],minm); 74 while(t%prime[temp]==0) t/=prime[temp]; 75 } 76 } 77 if(mink+tmp>k) k=mink+tmp,m=minm; 78 else if(mink+tmp==k) m=max(m,minm); 79 for(int i=1;i<=n;i++) 80 { 81 int t=b[i]; 82 while(t && t!=1) 83 { 84 int temp=pre[t]; 85 num[temp]=0,g[temp]=0; 86 while(t%prime[temp]==0) t/=prime[temp]; 87 } 88 } 89 } 90 printf("%d %d\n",k,m); 91 return 0; 92 }
标签:sample define time ble ace through 差值 can put
原文地址:https://www.cnblogs.com/wujiechao/p/9188843.html