Zenyk and Marichka start playing Marichka’s favourite card game. But Zenyk forgets even how the deck
looks like. Of course, he can’t just ask Marichka.
He knows that deck contains N cards. Also there are some number of suits, let’s say it’s K. Number of
cards of each suit is the same. Suits are numbered from 1 to K.
At the beginning of the game M cards were dealt. So Zenyk knows suits of these cards. Help Zenyk to
find if it’s possible to determine the value of K uniquely. Note that the deck is valid so at least one valid
value of K exists.
Input
The first line contains 2 integers – N and M (1 ≤ N ≤ 109
, 1 ≤ M ≤ min(N, 105
)).
The second line contains M integers Ai – suit of the i-th card (1 ≤ Ai ≤ N).
Output
Print “YES” if it’s possible to determine number of suits uniquely and “NO” otherwise.
Examples
standard input standard output
36 11
1 4 2 4 4 2 4 1 4 4 4
YES
4 2
1 1
NO
在 sqrt( n ) 范围内检查即可;
#include<bits/stdc++.h>
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 1e5 + 7;
const long long mod = 1e9 + 7;
int n, m;
map<int, int> cnt;
int main(int argc, char const *argv[])
{
scanf("%d%d", &n, &m);
int x;
int maxcnt = 0;
int tmpk = 0;
for (int i = 1; i <= m; i++)
{
scanf("%d", &x);
tmpk = max(tmpk, x);
cnt[x]++;
maxcnt = max(maxcnt, cnt[x]);
}
int maxk = sqrt(n);
int ans = 0;
for (int i = 1; i <= maxk; i++)
{
if (n % i == 0)
{
int pl = i;
int ps = n / i;
if (ps >= maxcnt && pl >= tmpk)
ans++;
if (i * i != n)
{
ps = i;
pl = n / i;
if (ps >= maxcnt && pl >= tmpk)
ans++;
}
}
}
if (ans == 1)
printf("YES\n");
else
printf("NO\n");
return 0;
}