BZOJ3994: [SDOI2015] Sum of approximations
Description
Let d(x) be the number of divisors of x, given N and M, find $$
\sum_{i=1}^N\sum_{j=1}^Md(ij)$$
Input
The input file contains multiple sets of test data.
The first line, an integer T, represents the number of groups of test data.
The next T lines, each line with two integers N, M.
Output
T lines, each with an integer representing the answer you are looking for.
Sample Input
2
7 4
5 6
7 4
5 6
Sample Output
110
121
121
HINT
1<=N, M<=50000
1<=T<=50000
Problem solving Here!
When I saw two summation symbols, I immediately thought of Mobius inversion.
But what about that d(ij)?
No problem, we have:
d(ij)=\sum_{x|i}\sum_{y|j}[gcd(x,y)==1]
so,
ans=\sum_{i=1}^n\sum_{j=1}^m\sum_{u|i}\sum_{v|j}[gcd(u,v)==1]
=\sum_{i=1}^n\sum_{j=1}^m[\frac{n}{i}][\frac{m}{j}][gcd(i,j)==1]
Not finished. . .
Attached code:
#include<iostream> #include<algorithm> #include<cstdio> #define MAXN 50010 using namespace std; int k=0,prime[MAXN],mu[MAXN],sum[MAXN]; long long f[MAXN]; bool np[MAXN]; inline int read(){ int date=0,w=1;char c=0; while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();} while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();} return date*w; } void make(){ int m=MAXN-10; mu [1] = 1; for(int i=2;i<=m;i++){ if(!np[i]){ prime [++ k] = i; mu [i] = - 1; } for(int j=1;j<=k&&prime[j]*i<=m;j++){ np[prime[j]*i]=true; if(i%prime[j]==0)break; else mu [prime [j] * i] = - mu [i]; } } for(int i=1;i<=m;i++)sum[i]=sum[i-1]+mu[i]; for(int i=1;i<=m;i++){ long long s=0; for(int j=1,last=1;j<=i;j=last+1){ last=i/(i/j); s+=(long long)(last-j+1)*(i/j); } f[i]=s; } } void work(){ int n,m; long long ans=0; n=read();m=read(); if(n>m)swap(n,m); for(int i=1,last=1;i<=n;i=last+1){ last=min(n/(n/i),m/(m/i)); ans+=(long long)(sum[last]-sum[i-1])*f[n/i]*f[m/i]; } printf("%lld\n",ans); } int main(){ int t=read(); make(); while(t--)work(); return 0; }