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
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
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();
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 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.