How to compute the value of following expression efficiently?
$$\sum_{x=1}^N\sum_{y=1}^N \frac{xy}{\gcd(x,y)^2}$$
I tried by my self but I am getting TLE with my solution.
Problem from HackerEarth.
How to compute the value of following expression efficiently?
$$\sum_{x=1}^N\sum_{y=1}^N \frac{xy}{\gcd(x,y)^2}$$
I tried by my self but I am getting TLE with my solution.
Problem from HackerEarth.
On
You can halve your computations by noticing the sum is symmetric in $x$ and $y$ so that:
$$S=N+2\sum_{1\le x<y\le N}\frac{xy}{\gcd(x,y)^2}$$
Since you have the dynamic-programming tag, I'll also mention that the $\gcd$ can be computed dynamically. It suffices to use
$$\gcd(x,y)=\begin{cases}x,&x=y\\\gcd(y,x),&x>y\\\gcd(x,y-x),&x<y\end{cases}$$
or the usual $\gcd(x,y)=\gcd(y,x\%y)$. By caching the results here, a lot of recomputation can be avoided.
below is my solution for the problem. but I am getting TLE in some of the test cases. could anyone help me on this to improve the complexity?
include
define ll long long
using namespace std;
const unsigned ll MOD = 1000000007;
ll gcd(ll a, ll b) {
}
ll compute(ll n) {
}
int main() {
}