extruder's blog

By extruder, history, 31 hour(s) ago, In English

I am using the idea, x < b and when x = r * lcm + [0, b — 1] ==> (x % a) % b = (x % a) % a So, I am using this idea to solve the problem, but my code is failing I don't know why it is missing some cases please help me with this Problem link, Submission Link


#include <iostream> using namespace std; #define ws ' ' #define ll long long ll gcd(ll a, ll b) { if (b > a) swap(a, b); while (b != 0) { ll temp = b; b = a % b; a = temp; } return a; } ll f(ll _lcm, ll b, ll r) { ll ans = min(r, b - 1); ll d = (r - b + 1) / _lcm; if (d != 0) { ans += d * b; if ((d + 1) * _lcm <= r) ans += (r - (d + 1) * _lcm + 1); } return ans; } void solve() { ll a, b, q; cin >> a >> b >> q; ll _lcm = (a * b) / (gcd(b, a)); while (q--) { ll l, r; cin >> l >> r; ll ans = r - l + 1; ans -= f(_lcm, b, r); if (l > 0) ans += f(_lcm, b, l - 1); cout << ans << ws; } cout << endl; } int main() { ios::sync_with_stdio(false); cin.tie(__null); int t = 1; cin >> t; for (int _ = 0; _ < t; _++) solve(); return 0; }

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By extruder, 3 days ago, In English

Link to the problem You are given a tetrahedron. Let's mark its vertices with letters A, B, C and D correspondingly.

your task is to count the number of ways in which the ant can go from the initial vertex D to itself in exactly n steps. In other words, you are asked to find out the number of different cyclic paths with the length of n from vertex D to itself. As the number can be quite large, you should print it modulo 1000000007 (1e9 + 7).

Idea is simple,

Base case?
For a general i?
Think a little bit (relation between A, B and C)

Use the above mentioned recursive relations to find out the answer

Code

Optimized code can be found here (DP solution Click here for code)

What if n<= 1e18? Think about the optimizing the above equations in matrix form, u might have done this for fibonacci series

Further optimized code (Click here for code)

Solutions are discussed in this video

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it