Bzoj-2005 能量采集 gcd,递推

时间:2021-12-19 08:16:00

  题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2005

  题意:题目转换后的模型就是求Σ(gcd(x,y)), 1<=x<=n, 1<=y<=m。。

  容易想到n^2logn的方法,ΣΣ(gcd(x,y)*2-1),但是这里会超时,因此我们需要优化。我们令f[d]表示(x,y),1<=x<=n, 1<=y<=m的所有对数中gcd(x,y)=d的个数,那么容易求出所有对数中(x,y)的约数为d的个数为(n/d)*(m/d),然后减去f[i*d],i>=2就行了...

 //STATUS:C++_AC_16MS_2052KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL f[N];
int n,m; int main(){
freopen("in.txt","r",stdin);
int i,j,low;
LL ans;
scanf("%d%d",&n,&m);
low=Min(n,m);
ans=;
for(i=low;i>;i--){
f[i]=(LL)(n/i)*(m/i);
for(j=i+i;j<=low;j+=i)f[i]-=f[j];
ans+=f[i]*(i*-);
}
printf("%lld\n",ans);
return ;
}