Do not know what algorithm.
A plant (I,J) is first asked to connect to the energy collector with several plants.
The answer is (GCD (i,j)-1), set I ' = I/GCD (i,j), J ' =j/gcd (I,J).
Then these plants are (i ' k,j ' k) 1<=k<gcd (i,j) and k are integers.
So the answer is to ask for a lot of gcd.
Because both N and M are large, we can enumerate gcd.
A number I as the number of GCD is equal to (n/i) * (m/i) The number of –gcd greater than I.
We can also easily find out the number of gcd larger than I.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN =100000+Ten;Long LongANS[MAXN],CNT[MAXN];Long LongM,n,res;intMain () {scanf ("%lld%lld",&n,&m); if(n>m) swap (N,M); for(intI=2; i<=n;i++) cnt[i]= (n/i) * (m/i); for(inti=n;i>=2; i--) {Ans[i]=Cnt[i]; for(intj=2; i*j<=n;j++) Ans[i]-=ans[i*J]; } for(intI=2; i<=n;i++) res+= (i-1)*Ans[i]; printf ("%lld\n", res*2+n*m); return 0;}
BZOJ2005: [Noi2010] Energy harvesting