D. Factorial Divisibility
D. Factorial Divisibility
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputYou are given an integer xx and an array of integers a1,a2,…,ana1,a2,…,an. You have to determine if the number a1!+a2!+…+an!a1!+a2!+…+an! is divisible by x!x!.
Here k!k! is a factorial of kk — the product of all positive integers less than or equal to kk. For example, 3!=1⋅2⋅3=63!=1⋅2⋅3=6, and 5!=1⋅2⋅3⋅4⋅5=1205!=1⋅2⋅3⋅4⋅5=120.
Input
The first line contains two integers nn and xx (1≤n≤5000001≤n≤500000, 1≤x≤5000001≤x≤500000).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤x1≤ai≤x) — elements of given array.
Output
In the only line print "Yes" (without quotes) if a1!+a2!+…+an!a1!+a2!+…+an! is divisible by x!x!, and "No" (without quotes) otherwise.
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 outputCopy No input 2 500000 499999 499999 output No
#include <iostream> #include <cstring> #include <algorithm> #include <map> using namespace std; const int N=5e5+10; int n,x; map<int,int>p; int main(){ cin>>n>>x; for(int i=1;i<=n;i++){ int a; cin>>a; p[a]++;//记录每一位的系数 } for(int i=1;i<=x;i++){ p[i+1]+=p[i]/(i+1);//逢i+1进1 p[i]%=i+1;//得到余数 } for(int i=1;i<x;i++){ if(p[i]){//如果有一位不为空则不能整除 cout<<"No"; return 0; } } cout<<"Yes"; } /*核心思想 把a!看成一个数的第a位,即转换为进制 如果要想被x!整除,即小于第x位其余位的系数都必须为零,才能被整除 第i位和第i+1位为逢i+1进1*/