Solution to CF1749D Counting Arrays
Observe that an arbitrary array
Thus, the problem reduces to counting the number of arrays of length from
Consider each length of
Assume that the length of
Let
Let
Since for every
The condition holds for every
The answer is the sum of
Time complexity:
:::success[Implementation]
#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;
}
:::