Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- void redirect(string filename) {
- freopen((filename + ".out").c_str(), "w", stdout);
- freopen((filename + ".in").c_str(), "r", stdin);
- }
- typedef long long ll;
- const ll N = 1e4, PL = 2000;
- bool is_comp[N + 1];
- vector<ll> primes;
- bool is_cached[N + 1][PL + 1];
- ll cache[N + 1][PL + 1];
- ll n;
- ll mod;
- ll f(ll cn, ll fp) {
- if (cn < 0) {
- return 0;
- }
- if (cn == 0) {
- return 1;
- }
- if (fp >= primes.size()) {
- return 1;
- }
- if (is_cached[cn][fp]) {
- return cache[cn][fp];
- }
- ll res = f(cn, fp + 1);
- for (ll exp = primes[fp]; cn - exp >= 0; exp = exp*primes[fp]) { //had modulus taken on exp updating step (mistake)
- res += (exp*f(cn - exp, fp + 1)) % mod;
- res %= mod;
- }
- cache[cn][fp] = res;
- is_cached[cn][fp] = true;
- return res;
- }
- int main() {
- redirect("exercise");
- ios::sync_with_stdio(false); cin.tie(0);
- cin >> n >> mod;
- for (ll d = 2; d*d <= n; ++d) {
- if (!is_comp[d]) {
- for (ll m = 2*d; m <= n; m += d) {
- is_comp[m] = true;
- }
- }
- }
- for (ll i = 2; i <= n; ++i) {
- if (!is_comp[i]) {
- primes.emplace_back(i);
- }
- }
- cout << f(n, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement