Codeforces Round #829 (Div. 2) D. Factorial Divisibility

news/2024/11/24 1:31:08/

Codeforces Round #829 (Div. 2) D. Factorial Divisibility

Let’s create an array [ c n t 1 , c n t 2 , … , c n t x ] [cnt_1,cnt_2,…,cnt_x] [cnt1,cnt2,,cntx] where c n t i cnt_i cnti equals to number of elements equals to i in the initial array.

Note that a 1 ! + a 2 ! + … + a n ! a_1! +a_2! + … + a_n! a1!+a2!++an! equals to sum of k ! ⋅ c n t k k!⋅cnt_k k!cntk over all k k k from 1 1 1 to x − 1 x−1 x1, c n t x cnt_x cntx does not affect anything because x! divides x ! x! x! itself. We have to check if this sum is divisible by x ! x! x!.

Suppose there exists some k < x k<x k<x such that c n t k ≥ k + 1 cnt_k≥k+1 cntkk+1. In this case we can make two transformations: c n t k − = ( k + 1 ) cnt_k−=(k+1) cntk=(k+1) ; c n t k + 1 + = 1 cnt_{k+1}+=1 cntk+1+=1 and the sum of k!⋅cntk will not change because ( k + 1 ) ⋅ k ! = ( k + 1 ) ! (k+1)⋅k!=(k+1)! (k+1)k!=(k+1)!.

Let’s perform this operation until it is possible for all numbers from 1 1 1 to x − 1 x−1 x1. After all operations the sum of k ! k! k! c n t k cnt_k cntk will not change and for each k < x k<x k<x the inequality c n t k ≤ k cnt_k≤k cntkk will be satisfied because if c n t k ≥ k + 1 cnt_k≥k+1 cntkk+1 we could perform an operation with this element.

Let’s see what is the maximum value of sum of k ⋅ c n t k k⋅cnt_k kcntk over all k k k from 1 1 1 to x − 1 x−1 x1 after all operations. We know that c n t k ≤ k cnt_k≤k cntkk for all k k k, so the maximum value of the sum is the sum of k ⋅ k ! k⋅k! kk! over all k k k.

Note that k ⋅ k ! = ( ( k + 1 ) − 1 ) ⋅ k ! = ( k + 1 ) ⋅ k ! − k ! = ( k + 1 ) ! − k ! k⋅k!=((k+1)−1)⋅k!=(k+1)⋅k!−k!=(k+1)!−k! kk!=((k+1)1)k!=(k+1)k!k!=(k+1)!k!.

It means that the sum of such values over all k k k from 1 1 1 to x − 1 x−1 x1 equals to ( 2 ! − 1 ! ) + ( 3 ! − 2 ! ) + … + ( x ! − ( x − 1 ) ! ) (2!−1!)+(3!−2!)+…+(x!−(x−1)!) (2!1!)+(3!2!)++(x!(x1)!). Each factorial from 2 2 2 to x − 1 x−1 x1 will be added and subtracted from the sum. So the result is x ! − 1 x!−1 x!1.

So the only one case when this sum is divisible by x ! x! x! is when the sum equals to 0 0 0. It means that c n t k cnt_k cntk equals to zero for all k k k from 1 1 1 to x − 1 x−1 x1 after performing all operations.

Time complexity: O ( n + x ) O(n+x) O(n+x).

#include<bits/stdc++.h>
using namespace std;int main()
{int n,x;cin>>n>>x;vector<int> a(500010);for(int i=1;i<=n;i++){int k;cin>>k;a[k]++;}for(int i=1;i<=500000;i++){a[i+1]+=a[i]/(i+1);a[i]%=(i+1);}for(int i=1;i<=x-1;i++)if(a[i]){cout<<"No"<<endl;return 0;}for(int i=x;i<=500001;i++)if(a[i]){cout<<"Yes"<<endl;return 0;}return 0;
}

http://www.ppmy.cn/news/541267.html

相关文章

Codeforces Round #829 (Div. 2) A-E

废话&#xff1a;本来前50分钟kill完了A B C1 D的时候是前300&#xff0c;看着C2过的人很少就想着摆了&#xff0c;结果刷了会儿视频一看我都排到700了&#xff0c;这才开始做C2&#xff0c;结果到最后C2卡线过的&#xff0c;E应该能过也没时间想了。直接排名干到700了。 Code…

Codeforces Round #829 (Div. 2)部分题解A B C1 E

原题地址&#xff1a;Dashboard - Codeforces Round #829 (Div. 2) - Codeforces A. Technical Support 题意&#xff1a; 有一个只包含“Q”和"A"的字符串&#xff0c;Q代表问题&#xff0c;A代表回答&#xff0c;有问必有答&#xff0c;也就是Q后面一…

Codeforces Round #829 (Div. 2)——(ABC1)题解

一、解题思路 1.A. Technical Support——1754A 题目分析&#xff1a;题目给定了一串字符串&#xff0c;字符串包含了两种字符一个为‘Q’表示问题&#xff0c;另一个字符A表示回答问题&#xff0c;题目要求输出是否对于每个问题&#xff0c;都做出了解答&#xff0c;若是输出…

windows微信协议|PC微信协议829版

最新windows协议|PC微信协议829版采用最新算法,达到非常稳定的效果&#xff0c;自己研发&#xff0c;功能多&#xff0c;并发量高&#xff0c;稳定好用&#xff0c;不掉线&#xff0c;不封号。mmtls是基于1.3的tls协议简化修改而来&#xff0c;根据某公司的描述&#xff0c;这种…

Codeforces Round #829 (Div. 1) C.Wish I Knew How to Sort(概率dp/期望线性性)

题目 给一个长度为n(n<2e5)的01串&#xff0c;每次随机选两个不同的下标(i,j)&#xff0c; 若ai>aj&#xff0c;则交换&#xff0c;问序列排成增序的期望次数&#xff0c;答案是一个分数&#xff0c;对998244353取模 实际是t(t<1e5)组样例&#xff0c;但n的总长不超…

Codeforces Round #829 (Div. 2)E - Wish I Knew How to Sort(dp期望)1024水个题解,最近感觉没什么时间刷算法

题意&#xff1a; 给你一个01组成的序列&#xff0c;现在让你进行两两交换后&#xff0c;让序列非递减。求操作的期望 思路&#xff1a; c n t 0 cnt0 cnt0&#xff1a;我们统计有多少个0在序列中&#xff0c; 那么最终的序列要保证前 c n t 0 cnt0 cnt0个都是0&#xff0c;…

Codeforces Round #829 (Div. 2)

A. Technical Support 题目链接&#xff1a;Problem - A - Codeforces 样例输入&#xff1a; 5 4 QQAA 4 QQAQ 3 QAA 1 Q 14 QAQQAQAAQQQAAA样例输出&#xff1a; Yes No Yes No Yes题意&#xff1a;给定一个长度为n的字符串&#xff0c;字符串中的所有字符都是Q或者A&#…

Codeforces Round #829 (Div. 2) A~D

比赛链接&#xff1a;Dashboard - Codeforces Round #829 (Div. 2) - Codeforces 目录 A. Technical Support B. Kevin and Permutation C1. Make Nonzero Sum (easy version) C2. Make Nonzero Sum (hard version) D. Factorial Divisibility A. Technical Support …