题意:
给定n个数a1,a2,…,an和数x
问a1!+a2!+…+an!是否可以被x!整除
Input
The first line contains two integers nn and xx (1 \le n \le 500\,0001≤n≤500000, 1 \le x \le 500\,0001≤x≤500000).
The second line contains nn integers a_1, a_2, \ldots, a_na1,a2,…,an (1 \le a_i \le x1≤ai≤x) — elements of given array.
Output
In the only line print "Yes" (without quotes) if a_1! + a_2! + \ldots + a_n!a1!+a2!+…+an! is divisible by x!x!, and "No" (without quotes) otherwise.
Sample 1
Inputcopy | Outputcopy |
---|---|
6 4 3 2 2 2 3 3 | Yes |
Sample 2
Inputcopy | Outputcopy |
---|---|
8 3 3 2 2 2 2 2 1 1 | Yes |
Sample 3
Inputcopy | Outputcopy |
---|---|
7 8 7 7 7 7 7 7 7 | No |
Sample 4
Inputcopy | Outputcopy |
---|---|
10 5 4 3 2 1 4 3 2 4 3 4 | No |
Sample 5
Inputcopy | Outputcopy |
---|---|
2 500000 499999 499999 | No |
题解:
1.ct[]表示小于x的a的阶乘和,用ct[] 数组记录每个给出的小于x的数的出现次数(大于等于x的一定可以整除),即 ct[a]++
2.当ct[a]>a+1时,即a出现的次数大于 a+1 时,进行操作:ct[a]=ct[a]-a-1;ct[a+1]=ct[a+1]+1;
解释:因为下标代表的是a的阶层,则 a!*(a+1)=(a+1)!.所以操作后,这n个数的总和不变。
3.如果这些小于x的数的阶乘之和可以整除x的话,那么它们的可可以表示=k*x!。k为大于0的任意整数。表现在ct数组上,即为当i<x,ct[i]==0;
#include <iostream>
#include <cstring>
using namespace std;
const long long N = 5e5 + 100;
int ct[N];
int main()
{
int n, x,v;
cin >> n >> x;
for (int i = 1; i <= n; i++)
{
cin >> v;
if(v<x)
ct[v]++;
}
int f = 1;
for (int i = 1; i < x; i++)
{
if (ct[i] >= i + 1)
{
ct[i + 1] = ct[i + 1] + ct[i]/(i+1);
ct[i] = ct[i] - ct[i] / (i + 1) * (i + 1);
}
if (ct[i] != 0)
{
f = 0;
cout << "No" << endl;
break;
}
}
if (f)
cout << "Yes" << endl;
}