D. Factorial Divisibility

D. Factorial Divisibility
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You 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!=123=63!=1⋅2⋅3=6, and 5!=12345=1205!=1⋅2⋅3⋅4⋅5=120.

Input

The first line contains two integers nn and xx (1n5000001≤n≤500000, 1x5000001≤x≤500000).

The second line contains nn integers a1,a2,,ana1,a2,…,an (1aix1≤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*/ 
复制代码

 

posted @   突破铁皮  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示