原题链接:
题目描述:
Alperen has two strings, ss and tt which are both initially equal to "a".
He will perform qq operations of two types on the given strings:
- 1kx1kx — Append the string xx exactly kk times at the end of string ss. In other words, s:=s+x+⋯+xk timess:=s+x+⋯+x⏟k times.
- 2kx2kx — Append the string xx exactly kk times at the end of string tt. In other words, t:=t+x+⋯+xk timest:=t+x+⋯+x⏟k times.
After each operation, determine if it is possible to rearrange the characters of ss and tt such that ss is lexicographically smaller†† than tt.
Note that the strings change after performing each operation and don't go back to their initial states.
†† Simply speaking, the lexicographical order is the order in which words are listed in a dictionary. A formal definition is as follows: string pp is lexicographically smaller than string qq if there exists a position ii such that pi<qipi<qi, and for all j<ij<i, pj=qjpj=qj. If no such ii exists, then pp is lexicographically smaller than qq if the length of pp is less than the length of qq. For example, abdc<abeabdc<abe and abc<abcdabc<abcd, where we write p<qp<q if pp is lexicographically smaller than qq.
Input
The first line of the input contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.
The first line of each test case contains an integer qq (1≤q≤105)(1≤q≤105) — the number of operations Alperen will perform.
Then qq lines follow, each containing two positive integers dd and kk (1≤d≤21≤d≤2; 1≤k≤1051≤k≤105) and a non-empty string xx consisting of lowercase English letters — the type of the operation, the number of times we will append string xx and the string we need to append respectively.
It is guaranteed that the sum of qq over all test cases doesn't exceed 105105 and that the sum of lengths of all strings xx in the input doesn't exceed 5⋅1055⋅105.
Output
For each operation, output "YES", if it is possible to arrange the elements in both strings in such a way that ss is lexicographically smaller than tt and "NO" otherwise.
题目大意:
给定一个长度为n的正整数数组和正整数x,判断(a1!+a2!+a3!+……+an!)是否被x!整除。
解题思路:
因为(n + 1)! = (n + 1) * n!,也就是n + 1个n!可以合并为(n + 1)!,
所以我们可以记录数组中每个小于x的ai出现的数量,每出现(ai + 1)个i,我们就可以将其合并为一个ai + 1。最后只需要所有的合并操作之后,所有的ai都大于等于x,那么其阶乘和是能够整除x!的。具体实现的时候使用map对ai进行计数即可。细节详见代码。
代码(CPP):
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 5e5 + 10;
const int INF = 0x3fffffff;
int n, x;
map<int, int> mp;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed;
cout.precision(18);
cin >> n >> x;
for (int i = 1; i <= n; i++)
{
int v;
cin >> v;
if(v < x)
mp[v]++;
}
bool flag = true;
for (int i = 1; i < x; i++)
{
if(mp[i] % (i + 1) != 0) // 合并不完,还有多余的,那么最后一定有小于x的ai
{
flag = false;
break;
}
mp[i + 1] += mp[i] / (i + 1);
}
if(flag)
cout << "Yes\n";
else
cout << "No\n";
return 0;
}