Help needed in template setting

After seeing a lot of posts on templates, I decided to make my own template
After a bit of work I got this. Obviously I’ll remove the code in solve and the comments later.

#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
long long int p=1e9 +7;
bool debugging=1;
struct input{
    ll a,b;//declaring the variables inputted
    input(){
        if(debugging){
            a=rand()%100;
            b=rand()%100;
        //randomising the values
        }
        else{
            cin>>a>>b;
           //inputting the variables
        }
    }
};
long long int modpower(long long int b,long long po){
    long long int ans=1;
    while(po){
        if(po&1){
            ans*=b;
            ans%=p;
        }
        b*=b;
        b%=p;
        po>>=1;
    }
    return ans;
}
ll solve(input inp){
    ll ans;
    ans=modpower(inp.a,inp.b);
    //solving quickly
    return ans;
}
ll solvebad(input inp){
    ll ans=1;
    //solving slowly
    for(int i=0;i<inp.b;i++){
        ans*=inp.a;
        ans%=p;
    }
    return ans;
}
int output(input inp){
    //outputting if it is wrong
    cout<<inp.a<<" "<<inp.b<<"\n";
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;
	cin >>t;
    srand(time(0));
	while(t--){
	   input inp;
	   if(debugging){
	       if(solve(inp)!=solvebad(inp)){//checking for random input
	           output(inp);
	       }
	   }
	   else{
	       cout<<solve(inp)<<"\n";//solving it correctly
	   }
	}
}

I want to know if passing the input struct will cause noticeable slowdown.

Pass it by reference, if you’re worried, but I’m betting the time taken to pass a couple of extra longs is measured in nanoseconds :slight_smile:

For reference - I just converted the Editorial solution for one of my old Problems away from using globals (shudder) to passing a bunch of data - including several longs and a vector - to a function that is called very heavily. The difference in time taken is so miniscule that 50% of the time, the new code is measured as being faster than the original one :man_shrugging:

1 Like

No, I mean for like arrays, Or can i declare it globally, But I can’t figure out how to reinitialise it.

Can you give an example of what you mean? I can’t see any mention of arrays in that code you posted.

1 Like

Something like this?

#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
long long int p=1e9 +7;
int n;
bool debugging=1;
struct input{
    ll a[100],b[100];//declaring the variables inputted
    input(){
        if(debugging){
            n=100;
            for(int i=0;i<100;i++){
            a[i]=rand()%100 +1;
            b[i]=rand()%100 +1 ;
            }
        //randomising the values
        }
        else{
            cin>>n;
            for(int i=0;i<n;i++){
                cin>>a[i]>>b[i];
            }
           //inputting the variables
        }
    }
};
long long int modpower(long long int b,long long po){
    long long int ans=1;
    while(po){
        if(po&1){
            ans*=b;
            ans%=p;
        }
        b*=b;
        b%=p;
        po>>=1;
    }
    return ans;
}
ll solve(input inp){
    ll ans=1;
    for(int i=0;i<n;i++){
    ans*=modpower(inp.a[i],inp.b[i]);
    ans%=p;
    }
    //solving quickly
    return ans;
}
ll solvebad(input inp){
    ll ans=1;
    //solving slowly
    for(int j=0;j<n;j++){
    for(int i=0;i<inp.b[j];i++){
        ans*=inp.a[j];
        ans%=p;
    }
    }
    return ans;
}
int output(input inp){
    //outputting if it is wrong
    for(int i=0;i<n;i++){
    cout<<inp.a[i]<<" "<<inp.b[i]<<"\n";
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int t;
	cin >>t;
    srand(time(0));
	while(t--){
	   input inp;
	   if(debugging){
	       if(solve(inp)!=solvebad(inp)){//checking for random input
	           output(inp);
	       }
	   }
	   else{
	       cout<<solve(inp)<<"\n";//solving it correctly
	   }
	}

If you’re intent on using arrays:

    ll a[100] = {},b[100] = {};//declaring the variables inputted
1 Like

I’m sorry, but I don’t know what empty curly brackets do :pensive:

They value-initialise all elements of the array (i.e. set them to 0, in this case).

1 Like

Thanks. I’ve only learnt the basics of c++, so I know very little, So thank you for helping me.

1 Like

Using memset ?

No like i declare input inp globally, and reinitialise it for every testcase

Okay understood.
You can make a function to initialize it multiple times instead of using constructor.
Idk if a better solution exists.
Though try something like
inp = new input();

That doesn’t compile

I am looking for another method but making function should surely work.

Oh i got it, That works, thanks.

Edit : inp = *(new input())
works but it will create issues. It will read input before reading t.
Also you cannot use fast IO if you declare it globally and use constructor.

I just declared a function init instead of the constructor inside the input struct and it works now. Is it slow?

I don’t understand why do you need a struct ?
You can make init function by declaring arrays globally.
I don’t see any requirement of making struct in competitive except you want your code to look cool XD.