0
#include <algorithm>
using namespace std;

typedef pair<long, long> num;

long mod;

num operator* (num a, num b){
    long r = a.first * b.first - a.second * b.second;
    r %= mod;
    if (r < 0) r += mod;
    
    long i = a.second * b.first + a.first * b.second;
    i %= mod;
    if (i < 0) i += mod;
    
    return {r, i};
}

num raise(num z, long n){
    if (n == 1) return z;
    auto tmp = raise(z, n/2);
    if (n%2) return tmp * tmp * z;
    return tmp*tmp;
}

int main() {
    long q;
    cin >> q;
    while (q--){
        long a, b, k, m;
        cin >> a >> b >> k >> m;
        num z(a, b);
        mod = m;
        num r = raise(z, k);
        cout << r.first << ' ' << r.second << endl;
    }
    
}

can someone tell how "num operator*" and "num raise" methods are working ??

i m confused , tried hard but still don't know how it is working and don't know

what operator* function means and also don't know its working , can someone please clear my doubt??

  • What do you mean by "how "num operator*" and "num raise" methods are working"? What exactly you cannot understand? – Dmitry Kuzminov Jun 30 '20 at 05:02
  • Those are multiplication and power functions for [Gaussian integers](https://en.wikipedia.org/wiki/Gaussian_integer) [modulo m](https://en.wikipedia.org/wiki/Modular_arithmetic#Integers_modulo_n). – dxiv Jun 30 '20 at 05:05
  • num operator* (num a, num b){ long r = a.first * b.first - a.second * b.second; r %= mod; if (r < 0) r += mod; long i = a.second * b.first + a.first * b.second; i %= mod; if (i < 0) i += mod; return {r, i}; } // how it is called through main function ? – All Happy Jun 30 '20 at 05:06
  • We are discussing a hacker rank problem. So presumably the shown code is written by you? Why then don't you understand the purpose of those things? – Yunnosch Jun 30 '20 at 05:20
  • Concerning "what operator* function means".. you are aware that in C++ you can make your own operator implementations, aren't you? – Yunnosch Jun 30 '20 at 05:22
  • For finding out what those things do, you did try to debug and observe step by step what they do, didn't you? Can you describe what they do? What the relation between their input and their output is? Do you see any realtion to details of the hackerrank problem? – Yunnosch Jun 30 '20 at 05:23
  • In Soviet Russia, peasants exponentiate YOU. One good tool to help understand code is to step through it with a debugger and see what's going on as it's going on. – user4581301 Jun 30 '20 at 05:27
  • i m sorry its not my code , i have started coding 2 weeks ago and finding it very difficult to understand – All Happy Jun 30 '20 at 05:28
  • @AllHappy Instead of looking at what is quite complicated code you would be better off buying and studying a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – john Jun 30 '20 at 05:50
  • thankyou so much guys for your genuine comments , surely i will follow your adivse – All Happy Jun 30 '20 at 06:08

0 Answers0