MATBREAK APRIL cook-off

#include
#include <math.h>
using namespace std;

int main() {
long long int i,j,n,t,x,p1,p2,sum;
cin>>t;
while(t–)
{
cin>>n>>x;
p1=x;
p2=1;
sum=0;
for(i=0;i<n;i++)
{
p1=(p1p2)%(1000000007);
p2=pow(p1,2
i+1);
sum=sum+p2%(1000000007);
}
cout<<sum%(1000000007)<<"\n";
}

return 0;

}
im getting WA for this please help

Since in this question the value of ‘n’ limits to 10^5 and value of ‘A’ limits to 10^9. so in your inner for loop you are calculating power of a large number raised to another large number multiple times. This will give you the wrong answer because of the limitation of double (power had return type double).
so, you need to get a better algorithm to find the power.

this would help…

long long modpow(long long base,long long exp,long long modulus)
{
base %= modulus;
long long result = 1;
while (exp > 0)
{
if(exp&1)result=(result*base)%modulus;
base = (base * base) % modulus;
exp>>=1;
}
return result;
}

There is no * sign when you are assigning value to p1 and pow should be calculated with modulus

n could be as high as 10^5, so
p2=pow(p1,2* i+1);
This line of your code may result into overflow, hence losing some data
Check out my solution
https://www.codechef.com/viewsolution/32162799

#include
#include <math.h>
using namespace std;
long long power(long long base,long long exp,long long modulus)
{
base %= modulus;
long long result = 1;
while (exp > 0)
{
if(exp&1)result=(resultbase)%modulus;
base = (base * base) % modulus;
exp>>=1;
}
return result;
}
int main() {
long long int i,j,n,t,x,p1,p2,sum,k;
cin>>t;
while(t–)
{
cin>>n>>x;
p1=x;
p2=1;
sum=0;
for(i=0;i<n;i++)
{
p1=(p1
p2);
// cout<<p1<<"\n";

        //p2=pow(p1,2*i+1);
        p2=power(p1,2*i+1,1000000007);
       // cout<<p2<<"\n";
        sum=sum+p2%1000000007;
       // cout<<p2<<"\n";
    }
    cout<<sum<<"\n";
}

return 0;

}
still gives wrong answer

use p1=(p1*p2)%1000000007
it should work.

you can check my solution here…
https://www.codechef.com/viewsolution/32152365