首页 > 其他分享 >NEUOJ 1207(Birthday present-前缀和)

NEUOJ 1207(Birthday present-前缀和)

时间:2022-10-24 18:04:49浏览次数:50  
标签:ch return int 1207 long Birthday NEUOJ ll define


给你一个数组a,给你一个k,你可以讲每个数减去不超过k,要求最后的GCD最大,求这个gcd
1 ≤ n ≤ 3·1e5; 1 ≤ k ≤ 1e6
1 ≤ ai ≤ 1e6

显然min(ai)<=k时,答案为min(ai),
否则,每个数都对应一段长度为k的可行区间,答案至少为k。
考虑gcd=g>k,则此时可能的ai,可能为[g,g+k],[2g,2g+k],⋯等,区间数为106g
复杂度为O(n(n+n/2+n/3+...+n/n))=O(nlogn)

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
#define
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
#define
const int MA=1E6;
int a[MAXN];
int f[MAXN];
int main()
{
// freopen("D.in","r",stdin);
// freopen(".out","w",stdout);
int n,k;
while(cin>>n>>k) {
For(i,n) a[i]=read();
int ans=k;
sort(a+1,a+1+n);
n=unique(a+1,a+1+n)-a-1;
MEM(f)
For(i,n) f[a[i]]++;
For(i,1e6) f[i]+=f[i-1];
if (a[1]<=k) cout<<a[1]<<endl;
else {

for(int i=a[n];i>k;i--) {
int p=0;
for(int j=i;j<=a[n];j+=i) p+=f[min(MA,j+k)]-f[j-1];
if (p==n) {
ans=i;
break;
}
}
cout<<ans<<endl;
}
}
return 0;
}


标签:ch,return,int,1207,long,Birthday,NEUOJ,ll,define
From: https://blog.51cto.com/u_15724837/5790750

相关文章

  • Happy Second Birthday Jenkins X!
    始于2019年初的JenkinsX项目在去年的1月14号庆祝了它的第一个生日,这对任何开源项目来说都是一件大事,我们刚刚又庆祝了它的第二个生日。JenkinsX的两周年!JenkinsX已......
  • 双哈希_Birthday_Cake
    BirthdayCake思路:找到每个串的公共前后缀,统计公共前后缀之间的字符串的hash值,并判断所给n个串中是否存在符合条件的串eg:abbddab对于该串,我们不难发现,公共前后缀是ab,公......
  • H - Mr. Panda and Birthday Song Gym - 101775H
    题意:给你一个长度不大于1e6的字符串,由'a'-'z'或‘?’组成,且‘?’可转化为任意小写字母。和两个数x,y。现在有两个条件:字符串中存在任意一个长度为x的子串均为元音,或存在......
  • MathProblem 37 Common birthday problem
    Whatistheminimumnumberofpeopledoyouneed,chosenatrandom,sothatthereisatleasta50%chancethatatleasttwohavethesamebirthday.Assumetha......