AtCoder Regular Contest 148 A - mod M
作者:互联网
题面
You are given a sequence \(A = (A_1, A_2, ..., A_N)\).
You may perform the following operation exactly once.
Choose an integer \(M\) at least \(2\). Then, for every integer \(i\) (\(1 \leq i \leq N\)), replace \(A_i\) with the remainder when \(A_i\) is divided by \(M\).
For instance, if \(M = 4\) is chosen when \(A = (2, 7, 4)\), \(A\) becomes \((2 \bmod 4, 7 \bmod 4, 4 \bmod 4) = (2, 3, 0)\).
Find the minimum possible number of different elements in \(A\) after the operation.
Constraints
- \(2 \leq N \leq 2 \times 10^5\)
- \(0 \leq A_i \leq 10^9\)
- All values in the input are integers.
简要题意
给出一个长度为 \(N\) 的序列 \(A\),你需要找到一个整数 \(M(M \geq 2)\),使得所有 \(A_i \bmod m\) 的值种数最小,输出种数。
如 \(A=[1,4,8]\),则可以取 \(M=2\),\(A_1 \bmod M=1,A_2 \bmod M=0,A_3 \bmod M=0\),共 \(2\) 种,这是最优的。
\(2 \leq N \leq 2 \times 10^5,0 \leq A_i \leq 10^9\)
思路
这道题答案只可能是 \(1\) 或 \(2\)。因为如果答案大于 \(2\),我们可以取 \(M=2\) 这样种数最多是 \(2\)。
先排序。
不难发现,答案为 \(1\) 则代表存在 \(M\),使得 \(A_1 \equiv A_2 \equiv \cdots \equiv A_{n-1} \equiv A_n \pmod{M}\),根据同余定义可知 \(A_2-A_1 \mid A_3-A_2 \mid \cdots A_{n-1}-A_{n-2} \mid A_n-A_{n-1}\), 可以推出 \(\gcd\limits_{i=2}^{n}{(A_i-A_{i-1})}=m\)。
所以如果 \(\gcd\limits_{i=2}^{n}{(A_i-A_{i-1})} \gt 1\),答案是 \(1\),否则是 \(2\)。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[1000005];
int rrr;
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
rrr=a[2]-a[1];
for(int i=3;i<=n;i++){
rrr=__gcd(rrr,a[i]-a[i-1]);
}
if(rrr!=1){
cout<<1;
}
else{
cout<<2;
}
return 0;
}
标签:AtCoder,10,int,bmod,mid,leq,Regular,equiv,mod 来源: https://www.cnblogs.com/zheyuanxie/p/arc148a.html