http://blog.csdn.net/zhb1997/article/details/38474795
#include<cstdio> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; int n,m; ll ans; int main() { // freopen("bzoj3505.in","r",stdin); scanf("%d%d",&n,&m); ++n; ++m; ans=(ll)(n*m-2)*(ll)(n*m-1)*(ll)(n*m)/6ll; ans-=(ll)n*(ll)(m-2)*(ll)(m-1)*(ll)m/6ll; ans-=(ll)m*(ll)(n-2)*(ll)(n-1)*(ll)n/6ll; for(int i=1;i<n;++i) for(int j=1;j<m;++j) if(__gcd(i,j)>1) ans-=(ll)(__gcd(i,j)-1)*(ll)(n-i)*(ll)(m-j)*2ll; cout<<ans<<endl; return 0; }