Initially, I run below code on codechef IDE and I got runtime error while I replace data type of test-case variable (i.e, t) to int (from long long), I get no error. Why?
[error code]
#include<iostream> #define ll long long using namespace std; const ll mod = 1e9+7; ll fast_sq(ll base, ll exp) { ll P=1; while(exp>0){ if(exp %2==1) P=((P%mod)*(base%mod))%mod; base=((base%mod)*(base%mod))%mod; exp/=2; } return P; }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cout.tie(0);
ll t;
ll n, a;
cin>>t;
while (t--)
{
cin>>n>>a;
ll ans=0;
ll prod;
int start=1;
for(int i=1; i<=n; i++){
prod = fast_sq(a, start);
start+=2;
//cout<<prod<<endl;
ans=(ans+ prod)%mod;
//preT=temp;
a=(a * prod)%mod;
}
cout<<ans<<endl;
}
return 0;
}
// // // //
[correct code]
#include <iostream> #define ll long long using namespace std; const ll mod = 1e9+7; ll fast_sq(ll base, ll exp) { ll P=1; while(exp>0)
{
if(exp %2==1) P= ((P%mod) * (base%mod))%mod; base=((base%mod)*(base%mod))%mod; exp/=2; } return P; }
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//cout.tie(0);
int t; // correction
ll n, a;
cin>>t;
while (t--)
{
cin>>n>>a;
ll ans=0;
ll prod;
int start=1;
for(int i=1; i<=n; i++){
prod = fast_sq(a, start);
start+=2;
//cout<<prod<<endl;
ans=(ans+ prod)%mod;
//preT=temp;
a=(a * prod)%mod;
}
cout<<ans<<endl;
}
return 0;
}