This question is similar to Project Euler #48 but constraints are different:
$$N < 2000000$$
Just in case the link is unavailable, here is the problem statement:
We've to print
$$\left( \sum_{i=1}^N i^i \right) \mod 10^{10}$$
I've tried the following, but I need something faster than that, because the execution time limit is 2s, and this program can only compute values until \$ N=30000\$ (approx) in the given time limit.
#include <iostream>
using namespace std;
#define Mod 10000000000
int main()
{
int N;
cin>>N;
long long Temp,Sum=0;
for( int ii=1 ; ii<=N ; ii++ )
{
if(ii%10==0)
{
continue;
}
Temp=1;
for( int jj=1 ; jj<=ii ; jj++ )
{
Temp*=ii;
Temp=Temp%Mod;
}
Sum+=Temp;
Sum=Sum%Mod;
}
cout<<Sum;
return 0;
}
I've added
if(N%10==0)
{
continue;
}
because numbers of form $$ (k*10)^{k*10}=k^{k*10}*10^{k*10}=k^{k*10}*10^{k}*10^{10} $$ are not at all going to contribute to the answer.
Note: code is compiled using g++ 4.8.2, C++11 mode