题意
给定n个数a1,a2,…,an和数x
问a1!+a2!+…+an!是否可以被x!整除
其中k!=k*(k-1)*…*1
思路
对于ai>=x,显然ai!能被x!整除,我们关注ai<x的情况。
- 统计1到x-1的个数。
- 从小到大遍历1到x-1,对于i<x,如果count(i)>=i+1,则令count(i)-1,count(i+1)+1。
通过上述操作,有count(i)<=i, 1<=i<x。
我们来证明count(1)*1!+count(2)2!+…count(x-1)(x-1)! < x!
参考
因为k*k!=(k+1-1)*k=(k+1)! - k!
count(i)<=i, 1<=i<x
所以上述不等式,最大值为
11! + 22! + … (x-1)*x! = (2!-1!)+(3!-2!)+…(x!-(x-1)!)=x!-1<x!
因此,如果count(1),count(2),.count(x-1)不全为0,那么有count(1)*1!+count(2)2!+…count(x-1)(x-1)! < x!,此时a1!+a2!+…+an!不能被x!整除。
代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pcc pair<char, char>
#define pii pair<int, int>
#define inf 0x3f3f3f3f
const int maxn = 500010;
int n, mp[maxn], x;
void solve() {
scanf("%d%d", &n, &x);
for (int i = 0, v; i < n; ++i) {
scanf("%d", &v);
++mp[v];
}
bool flag = true;
for (int i = 1; i < x; ++i) {
if (!mp[i]) {
continue;
}
if (mp[i] % (i + 1) != 0) {
flag = false;
break;
}
mp[i+1] += mp[i] / (i + 1);
}
printf("%s\n", flag ? "Yes" : "No");
}
int main() {
int t = 1;
// scanf("%d", &t);
int cas = 1;
while (t--) {
// printf("cas %d:\n", cas++);
solve();
}
}
GZH
对方正在debug