#include
#include<stdio.h> #include #include
using namespace std;
typedef long long int ll;
ll power(ll x, ll e)
{
ll res;
if (e == 0)
{
res = 1;
}
else if (e == 1)
{
res = x;
}
else
{
res = power(x, (e / 2));
res = res * res;
if ((e) % 2)
res = res * x;
}
//cout<<"res4=\n"<<res;
return res;
}
int main()
{
ll t;
scanf("%lld",&t);
while(t--)
{
ll n=0,m,o;
int k=0;
scanf("%lld%d",&n,&k);
ll res=power(n,n);
cout.precision(k);
long double intpart;
ll a=0,b=0;
int p=0,c=0;
p=power(10,k);//cout<<"p="<<p<<endl;
o=power(n,n);m=o;
int last=0;
last=o%p;
int first=floor( pow( 10.0, modf( n * log10((double)n), &intpart ) + k - 1 ) );
printf("%d %d",first,last);
if(last==0)
{ k=k-1;
while(k--)
printf("%d",0);}
printf("\n");
}
return 0;
}
Below I tried to format your code to something readable (I am not allowed to edit your question).
If you look at the problem limits, you will see that N is at most 10^9. Think about how large 10^9^(10^9) is. A ‘long long’ is at most 2^63-1 (about 18 digits).
The result of N^N will not fit in a ‘long long’. Try to find another solution…
include <iostream>
include <stdio.h>
include <cmath>
include <cstdio>
using namespace std;
typedef long long int ll;
ll power(ll x, ll e)
{
ll res;
if (e == 0)
{
res = 1;
}
else if (e == 1)
{ res = x; }
else
{
res = power(x, (e / 2));
res = res * res;
if ((e) % 2) res = res * x;
}
//cout<<"res4=\n"<<res;
return res;
}
int main()
{
ll t;
scanf("%lld",&t);
while(t--)
{
ll n=0,m,o;
int k=0;
scanf("%lld%d",&n,&k);
ll res=power(n,n);
cout.precision(k);
long double intpart;
ll a=0,b=0;
int p=0,c=0; p=power(10,k);
//cout<<"p="<<p<<endl;
o=power(n,n);
m=o;
int last=0;
last=o%p;
int first=floor( pow( 10.0, modf( n * log10((double)n), &intpart ) + k - 1 ) );
printf("%d %d",first,last);
if(last==0)
{
k=k-1;
while(k--)
printf("%d",0);
}
printf("\n");
}
return 0;
}