D. Factorial Divisibility
D. Factorial Divisibility
You are given an integer and an array of integers . You have to determine if the number is divisible by .
Here is a factorial of — the product of all positive integers less than or equal to . For example, , and .
Input
The first line contains two integers and .
The second line contains integers — elements of given array.
Output
In the only line print "" (without quotes) if is divisible by , and "" (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 . Number is divisible by .
In the second example , is divisible by .
In the third example . It is easy to prove that this number is not divisible by .
解题思路
这题还是比较难想的。开个数组统计每个的出现次数,数组分别表示某个数字出现的次数(因为,因此统计的数就是)。那么就是。这里我们只累加到,即,这是因为我们只关心这个和是否能被整除,又因为被整除,因此只需判断能否被整除就可以了。
因为,因此如果存在某个,那么我们让,,重复这个过程直到。从枚举,进行上面的操作,最后保证对于任意一个数,有。
断言:对于,如果存在某个,那么不能被整除。注意到执行上面的操作后有,因此有因为,因此即,自然不能被整除(很关键的一点)。
因此只要存在一个,那么就不能被整除,即不能被整除。
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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/16834339.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效