专栏文章
Solution to CF1749D Counting Arrays
CF1749D题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mind9qb8
- 此快照首次捕获于
- 2025/12/02 00:31 3 个月前
- 此快照最后确认于
- 2025/12/02 00:31 3 个月前
Observe that an arbitrary array always has a removal sequence of , for always holds. Therefore, as long as there exists an index , we can obtain another removal sequence by removing the first element until is shifted to position , removing the -th element, and removing the first element until every element is removed.
Thus, the problem reduces to counting the number of arrays of length from to , where each is an integer from to , satisfying the aforementioned condition.
Consider each length of separately.
Assume that the length of is , the number of arrays satisfying the condition equals minus the number of arrays such that
Let denote the number of arrays satisfying the condition above. To compute , we consider each separately.
Let be the number of integers which can be. It is obvious that , because there is no constraint on it.
Since for every , must be a multiple of every prime number less than or equal to . Let where is a prime number. We have .
The condition holds for every , so .
The answer is the sum of over all .
Time complexity: due to the use of exponentiation by squaring.
Implementation
CPP#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
constexpr int N = 3e5 + 5, mod = 998244353;
int qpow(int a, int b) {
a %= mod;
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return res;
}
int n; ll m;
ll c[N];
bool notprime[N];
vector<int> primes;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 2; i <= n; i++) {
if (!notprime[i]) primes.emplace_back(i);
for (int j = 0; primes[j] * i <= n; j++) {
notprime[primes[j] * i] = 1;
if (i % primes[j] == 0) break;
}
}
ll lc = 1;
for (int i = 2; i <= n; i++) {
if (!notprime[i]) {
if (lc <= m / i) lc *= i;
else lc = m + 1;
}
c[i] = m / lc;
}
int ans = 0, prod = m % mod;
for (int i = 2; i <= n; i++) {
prod = c[i] % mod * prod % mod;
ans = ((ans + qpow(m % mod, i)) % mod - prod + mod) % mod;
}
cout << ans << "\n";
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...