D. Factorial Divisibility

D. Factorial Divisibility

You are given an integer x and an array of integers a1,a2,,an. You have to determine if the number a1!+a2!++an! is divisible by x!.

Here k! is a factorial of k — the product of all positive integers less than or equal to k. For example, 3!=123=6, and 5!=12345=120.

Input

The first line contains two integers n and x (1n500000,1x500000).

The second line contains n integers a1,a2,,an (1aix) — elements of given array.

Output

In the only line print "Yes" (without quotes) if a1!+a2!++an! is divisible by x!, and "No" (without quotes) otherwise.

Examples

input

6 4
3 2 2 2 3 3

output

Yes

input

8 3
3 2 2 2 2 2 1 1

output

Yes

input

7 8
7 7 7 7 7 7 7

output

No

input

10 5
4 3 2 1 4 3 2 4 3 4

output

No

input

2 500000
499999 499999

output

No

Note

In the first example 3!+2!+2!+2!+3!+3!=6+2+2+2+6+6=24. Number 24 is divisible by 4!=24.

In the second example 3!+2!+2!+2!+2!+2!+1!+1!=18, is divisible by 3!=6.

In the third example 7!+7!+7!+7!+7!+7!+7!=77!. It is easy to prove that this number is not divisible by 8!.

 

解题思路

  这题还是比较难想的。开个数组统计每个ai的出现次数,数组[cnt1,cnt2,,cntx]分别表示某个数字i出现的次数cnti(因为1aix,因此统计的数就是1x)。那么i=1nai!就是i=1xcntii!。这里我们只累加到x1,即i=1x1cntii!,这是因为我们只关心这个和是否能被x!整除,又因为cntxx!x!整除,因此只需判断i=1x1cntii!能否被x!整除就可以了。

  因为(i+1)i!=(i+1)!,因此如果存在某个cntii+1,那么我们让cnti=i+1cnti+1+=1,重复这个过程直到cnti<i+1。从1枚举x,进行上面的操作,最后保证对于任意一个数i,有cnti<i+1

  断言:对于1ix1,如果存在某个cnti0,那么i=1x1cntii!不能被x!整除。注意到执行上面的操作后有cnti<i+1,因此有i=1x1cntii!i=1x1ii!因为ii!=((i+1)1)i!=(i+1)i!i!=(i+1)!i!,因此i=1x1cntii!i=1x1ii!=11!+22!++(x1)(x1)!=(2!1!)+(3!2!)++(x!(x1)!)=x!1i=1x1cntii!<x!,自然不能被x!整除(很关键的一点)。

  因此只要存在一个cnti0,那么i=1x1cntii!就不能被x!整除,即i=1nai!不能被x!整除。

  AC代码如下:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N = 5e5 + 10;
 5 
 6 int cnt[N];
 7 
 8 int main() {
 9     int n, m;
10     scanf("%d %d", &n, &m);
11     while (n--) {
12         int x;
13         scanf("%d", &x);
14         cnt[x]++;
15     }
16     bool flag = true;
17     for (int i = 1; i < m; i++) {
18         if (cnt[i] % (i + 1)) {
19             flag = false;
20             break;
21         }
22         cnt[i + 1] += cnt[i] / (i + 1);
23     }
24     printf("%s", flag ? "YES" : "NO");
25     
26     return 0;
27 }
复制代码

 

参考资料

  Codeforces Round #829 Editorial:https://codeforces.com/blog/entry/108336

posted @   onlyblues  阅读(104)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示