Please read the new rule regarding the restriction on the use of AI tools. ×

frank1369blogger's blog

By frank1369blogger, history, 4 years ago, In English

few days ago I wrote a blog asking about if vectors can cause you TLE. and now I have faced such problem in solving this problem. and now I show you the codes that their solution is totally the same but the first one gets AC and the other one gets TLE. I only changed the vectors of the second one to arrays and it passed. please tell me if I'm wrong and TLE was for smth else.

AC solution:

//\\//\\ * * * //\\// ||
#include <bits/stdc++.h> 

#define debug(x) cerr << #x << ": " << x << endl

using namespace std;

typedef long long ll;

int md;

inline void add(int& a, int b) {
  a += b;
  if (a >= md) {
    a -= md;
  }
}

inline int mul(int a, int b) {
  return (int) ((ll) a * b % md);
}

const int N = (int) 1e4 + 10;
const int M = 2000;

bool is_prime[N];
int dp[N][M];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  freopen("exercise.in", "r", stdin);
  freopen("exercise.out", "w", stdout);
  int n;
  cin >> n >> md;
  for (int i = 0; i <= n; i++) {
    is_prime[i] = true;
  }
  is_prime[1] = is_prime[0] = false;
  for (int i = 2; i <= n; i++) {
    if (!is_prime[i]) {
      continue;
    }
    for (int j = i + i; j <= n; j += i) {
      is_prime[j] = false;
    }
  } 
  vector<int> p;
  for (int i = 0; i <= n; i++) {
    if (is_prime[i]) {
      p.push_back(i);
    }
  }
  int m = (int) p.size();
  for (int i = 0; i <= m - 1; i++) {
    dp[0][i] = 0;
    dp[1][i] = 0;
  }
  for (int i = 2; i <= n; i++) {
    int x = 2;
    while (x <= i) {
      x *= 2;
    }
    x /= 2;
    if (x == i) {
      add(dp[i][0], x);
    }
    for (int j = 1; j < m; j++) {
      add(dp[i][j], dp[i][j - 1]);
      int y = p[j];
      while (y <= i) {
        if (y == i) {
          add(dp[i][j], y);
          break;
        }
        add(dp[i][j], mul(dp[i - y][j - 1], y));
        y *= p[j];
      }
    }
  }
  int ans = 0;
  for (int i = 0; i <= n; i++) {
    add(ans, dp[i][m - 1]);
  }
  add(ans, 1);
  cout << ans << '\n';
  return 0;
}

TLE solution:

/**

  @@@@@@@  @@@@  @@@@  @@@@  @@@@  @
  @  @  @  @  @  @  @  @  @  @  @  @
  @  @  @  @  @  @  @  @  @  @  @  @
  @  @  @  @  @  @  @  @  @  @  @  @
  @  @  @  @@@@  @@@@  @@@@  @  @  @

**/
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned long long
#define db double
#define ld long double
#define str string
#define pi pair
#define tup tuple
#define vec vector
#define temp template
#define tn typename

#define ff first
#define ss second
#define SZ(x) (ll) x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define fr front
#define bc back
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mk make_pair

#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define ROF(i, a, b) for (ll i = (a); i >= (b); i--)
#define trav(a, x) for (auto &a : x)
#define popcount __builtin_popcountll
#define ctz __builtin_ctzll
#define clz __builtin_clzll
#define gcd __gcd
 
#define max(a, b) max((ll) a, (ll) b)
#define min(a, b) min((ll) a, (ll) b)

// DEBUG
#define ts to_string
temp<tn A, tn B> str ts(pi<A, B> p);
temp<tn A, tn B, tn C> str ts(tup<A, B, C> p);
str ts(const str& s) { return '"' + s + '"'; }
str ts(const char* s) { return ts((str) s); }
str ts(bool b) { return (b ? "true" : "false"); }
str ts(vec<bool> v) { bool f = true; str res = "{";
FOR(i, 0, static_cast<ll>(v.size()) - 1)
{ if (!f) res += ", "; res += ts(v[i]);
f = false; } res += "}"; return res; }
temp<size_t N> str ts(bitset<N> v) { str res = "";
trav(a, v) { res += (char) ('0' + a); } return res; }
temp<tn A> str ts(A v) { bool f = true; str res = "{";
trav(x, v) { if (!f) res += ", "; res += ts(x);
f = false; } res += "}"; return res; }
temp<tn A, tn B> str ts(pi<A, B> p) {
  return "(" + ts(p.ff) + ", " + ts(p.ss) + ")"; }
temp<tn A, tn B, tn C> str ts(tup<A, B, C> t) {
  return "(" + ts(get<0>(t)) + ", " + ts(get<1>(t)) + ", " + ts(get<2>(t)) + ")"; }
void DBG() { cerr << endl; }
temp<tn H, tn... T> void DBG(H h, T... t) { cerr << " " << ts(h); DBG(t...); }
#ifdef LOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]:", DBG(__VA_ARGS__);
#else
#define dbg(...) 42
#endif

// INPUT AND OUTPUT
temp<tn T> void re(T& x) { cin >> x; };
temp<tn H, tn... T> void re(H& h, T&... t) { re(h); re(t...); }
temp<tn T> void pr(T x) { cout << x; };
temp<tn H, tn... T> void pr(H h, T... t) { pr(h); pr(t...); }
temp<tn T> void pri(T x) { cout << x; cout << endl; }; // used for interactives
temp<tn H, tn... T> void pri(H h, T... t) { pri(h); pri(t...); }

/**
 * Name: Modular operations
 * Description:
 * used for doing modular operations
**/
ll md;
ll nrm(ll x) { while (x < (ll) 0) x += md; x %= md; return x; }
ll sum() { return (ll) 0; }
temp <tn H, tn... T> ll sum(H h, T... t) { return nrm(nrm(h) + sum(t...)); }
ll mul() { return (ll) 1; }
temp <tn H, tn... T> ll mul(H h, T... t) { return nrm(nrm(h) * mul(t...)); }
ll power(ll a, ll b) {
  ll res = 1;
  while (b) { if (b & 1) res = mul(res, a); a = mul(a, a); b >>= 1; }
  return res;
}
ll inv(ll x) { return power(x, md - 2); }

// pay attention to md's value first!
// use normalized form of modular integers for output!

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  freopen("exercise.in", "r", stdin);
  freopen("exercise.out", "w", stdout);
  ll n;
  re(n, md);
  vec<ll> is_prime(n + 1, true);
  is_prime[1] = is_prime[0] = false;
  FOR(i, 2, n) {
  	if (!is_prime[i]) {
  		continue;
		}
		for (ll j = i + i; j <= n; j += i) {
			is_prime[j] = false;
		}
	}
	vec<ll> p;
	FOR(i, 0, n) {
		if (is_prime[i]) {
			p.pb(i);
		}
	}
	// dbg(p);
	ll m = SZ(p);
	vec<vec<ll>> dp(n + 1, vec<ll>(m));
	FOR(i, 0, m - 1) {
		dp[0][i] = 0;
		dp[1][i] = 0;
	}
	FOR(i, 2, n) {
		ll x = 2;
		while (x <= i) {
			x *= 2;
		}
		x /= 2;
		if (x == i) {
			dp[i][0] = sum(dp[i][0], x);
		}
		FOR(j, 1, m - 1) {
			dp[i][j] = sum(dp[i][j], dp[i][j - 1]);
			ll y = p[j];
			while (y <= i) {
				if (y == i) {
					dp[i][j] = sum(dp[i][j], y);
					break;
				}
				dp[i][j] = sum(dp[i][j], mul(dp[i - y][j - 1], y));
				y *= p[j];
			}
		}
	}
	// dbg(dp);
	ll ans = 0;
	FOR(i, 0, n) {
		ans = sum(ans, dp[i][m - 1]);
	}
	ans = sum(ans, 1);
	pr(ans, '\n');
  return 0;
}
  • Vote: I like it
  • -28
  • Vote: I do not like it

»
4 years ago, # |
Rev. 3   Vote: I like it -28 Vote: I do not like it

vector is only a bit slower than array so I think it's the another problem.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's rather caching related issue than something related to vector itself.