Calculating the sum, of the mod Q, position multiplied, sort sequence, of Fibonacci numbers
Complexity

The main issue with your application is the calculation of the "Fibonacci" number.

Here you are looping over the range [0,n). Each time you are calling fib() it recalculates the Fib values from scratch.

        for (int i = 0; i &lt; n; i++)
            S1[i] = fib(i+1);


The complexity of fib is O(k^n) (Approx). You put this in a loop so your complexity is O(n^n). Which is fine for small fixed amount of numbers (fib is simple and usually n is small (less than 100)). But when n is unbound like your problem this becomes a problem.

Fib is also relatively simple and can be done in O(n).

        S[0] = 1;
        S[1] = 1;
        for (int i = 2; i &lt; n; i++)
            S1[i] = S[i - 1] + S[i - 2];


That's it. Complexity now O(n).

Range

You don't address the problem with range. Thee Fibonacci sequence generates numbers that exceed 64 bits in less than 100 iterations. You don't address this in your code.

Now if we look at the problem statement we modulus everything by Q. So we only need the part of number below Q and thus overflow does not matter.

Now you are thinking I am saying it is a problem but then I say its not a problem. What gives. The problem is that this is not expressed in the code. Somebody reading the code at a later time is going to say this is wrong because you overflow the integers. What you need to to do is explain what you are doing in the code so that a reader knows that overflow is not an issue.

        S[0] = 1;
        S[1] = 1;
        // Note n can be large.
        // And this will can potentially overflow
        // But this is not an issue in this case because .....
        // Place explanation of why it works.
        for (int i = 2; i &lt; n; i++)
            S1[i] = S[i - 1] + S[i - 2];


Separation of concerns

Your code should be divided into parts that do business logic and those that do resource management. These two parts of the code should be separated otherwise things become very complicated quickly.

In your code you are doing both in the main loop. You are doing the calculations and performing all the memory management in the same place.

Your code has STUFF (the business logic) intertwined with the resource management. This is bad design.

// STUFF

    int *tmp = new int[n];
    int *count = new int[10]; 

    // STUFF

    delete[]tmp;
    delete[]count;

// STUFF


Also this is really a very C way of writting the code. In modern C++ we practically never need to deal with pointers as these are handeled by other higher level constructs for us (usually a container or a smart pointer).

In this case we can replace this memory management with a std::vector and achieve the same result.

// STUFF

    std::vector&lt;int&gt; tmp(n);
    std::vector&lt;int&gt; count(10); 

    // STUFF

// STUFF


Sorting (using radex)

The radix sort is fun. If the number of buckets is the same as the range of your elements then you don't actually need to do any sorting.

In your case the range is [0,Q) if Q is a reasonable number you can create Q different buckets. Then you just count the number of each element (rather than tracking each one). This makes the sort O(n).

 std::vector&lt;int&gt;   count(Q);
 for(int loop = 0;loop &lt; n; ++loop) {
     ++count[calcNextFib(loop) % Q];
 }


Naming

You use some very short names for variables.

    int i, j, k;


This makes reading the code very hard. The current best practice is called self documenting code. This is where you use variables names to explain what you are doing thus making the code self documenting without the need for extra variables.

Also don't declare the variables at the top of the function declare them at the point where they are about to be used. This becomes more important when you start using complex objects that have constructors but is a good habit to get into with all variables.

Code Review

Stop doing this.

using namespace std;


Its a bad habbit that will get you in trouble latter in life. Always use the std:: prefix in your code.

(Why is “using namespace std” considered bad practice?)[https://stackoverflow.com/q/1452721/14065)

Passing C-Arrays is frowned upon.

int maxbit(int data[], int n) 


The more standard way is to use `iterators. Pass the begin and end of a section as the parameters. Remember that iterators behave like pointers so it is still relatively easy to write your code with them.

You don't need to write everything from scratch:

    int maxData = data[0];      

    for (int i = 1; i &lt; n; ++i)
    {
        if (maxData &lt; data[i])
            maxData = data[i];
    }


We have a whole library of algorithms that do this:

    auto maxIter = std::max_element(begin , end);
    int maxData  = *maxIter;


A bit of white space vertically to help logically break up the code would be nice:

int main() {
    int T,n,Q;
    cin &gt;&gt; T;
    while (T--) {
        cin &gt;&gt; n &gt;&gt; Q;
        int* S1 = new int[n];
        int* A = new int[n];
        for (int i = 0; i &lt; n; i++)
            S1[i] = fib(i+1);
        for (int i = 0; i &lt; n; i++)
            A[i] = S1[i] % Q;