- #1
Saitama
- 4,243
- 93
Here is the problem I am trying to solve: SPOJ.com - Problem IITD4
Following is what I tried:
..but this gives TLE. What optimisations should I try?
Thanks!
Following is what I tried:
Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#define MOD 1000000007
using namespace std;
typedef long long ll;
ll modularPower(ll base, ll exponent)
{
ll res=1;
while(exponent)
{
if(exponent%2)
res=(res*base)%MOD;
exponent/=2;
base=(base*base)%MOD;
}
return res;
}
ll f(ll i, ll k)
{
ll j, u, sum=0;
u=sqrt(i);
for(j=1; j<=u; ++j)
{
if(i%j==0)
{
sum=(sum+modularPower(j, k));
sum=(sum+modularPower(i/j, k));
}
}
if(u*u==i)
{
sum=(sum-modularPower(u, k)+MOD)%MOD;
}
return sum;
}
int main()
{
int T, a, b, k, i;
scanf("%d", &T);
while(T--)
{
ll ans=0;
scanf(" %d %d %d", &a, &b, &k);
for(i=a; i<=b; ++i)
ans=(ans+f(i,k))%MOD;
cout<<ans<<endl;
}
}
..but this gives TLE. What optimisations should I try?
Thanks!