bzoj3994[SDOI2015]约数个数和

http://www.lydsy.com/JudgeOnline/problem.php?id=3994

好吧表示完全不会。

建议先看一下Codeforces235E

不妨设$a\leq b$

$\sum\limits_{i=1}^{a}\sum\limits_{j=1}^{b}d(ij)$

$=\sum\limits_{gcd(i,j)=1}\left \lfloor \frac{a}{i} \right \rfloor\left \lfloor \frac{b}{j} \right \rfloor$

$=\sum\limits_{}^{}\left \lfloor \frac{a}{i} \right \rfloor \left \lfloor \frac{b}{j} \right \rfloor [gcd(i,j)==1]$

$=\sum\limits_{}^{}\left \lfloor \frac{a}{i} \right \rfloor \left \lfloor \frac{b}{j} \right \rfloor\sum\limits_{d|gcd(i,j)}\varphi (d)$

$=\sum\limits_{}^{}\left \lfloor \frac{a}{i} \right \rfloor \left \lfloor \frac{b}{j} \right \rfloor\sum\limits_{d|i,d|j}\varphi (d)$

$=\sum\limits_{1\leq d\leq a}\varphi (d)\sum\limits_{d|i}^{}\sum\limits_{d|j}^{}\left \lfloor \frac{a}{i} \right \rfloor \left \lfloor \frac{b}{j} \right \rfloor$

设$i=di'$,$j=dj'$,则:

$\sum\limits_{1\leq d\leq a}\varphi (d)\sum\limits_{i'}^{}\sum\limits_{j'}^{}\left \lfloor \frac{a}{di'} \right \rfloor \left \lfloor \frac{b}{dj'} \right \rfloor$

记$f(n)=\sum\limits_{i=1}^{n}\left \lfloor \frac{n}{i} \right \rfloor=\sum\limits_{i=1}^{n}d(i)$

我们先用线性筛求出$d(i)$,就可以进一步求出$f(n)$

则原式为:

$\sum\limits_{d=1}^{a}\varphi (d)f(\left \lfloor \frac{a}{d} \right \rfloor)f(\left \lfloor \frac{b}{d} \right \rfloor)$

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }
LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z=='-'){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
        return (neg)?-res:res; 
    }

const int n=50000;

LL mobi[n+100],summobi[n+100],d[n+100],f[n+100];

int cnt,prime[n+100];
int flag[n+100];
LL minpx[n+100];
void get()
  {
      int i,j;
      mobi[1]=1;
      d[1]=1;
      re(i,2,n)
        {
            if(!flag[i])prime[++cnt]=i,mobi[i]=-1,d[i]=2,minpx[i]=1;
            for(j=1;j<=cnt && i*prime[j]<=n;j++)
              {
                  flag[i*prime[j]]=1;
                  if(i%prime[j]==0)
                          {
                              mobi[i*prime[j]]=0;
                              d[i*prime[j]]=d[i]/(minpx[i]+1)*(minpx[i]+2);
                                minpx[i*prime[j]]=minpx[i]+1;
                            }
                        else
                          {
                              mobi[i*prime[j]]=-mobi[i];
                              d[i*prime[j]]=d[i]*2;
                              minpx[i*prime[j]]=1;
                            }
              }
        }
  }

int main()
  {
      freopen("divisor.in","r",stdin);
      freopen("divisor.out","w",stdout);
      int i;
      get();
      re(i,1,n)f[i]=f[i-1]+d[i];
      re(i,1,n)summobi[i]=summobi[i-1]+mobi[i];
      for(int T=gint();T;T--)
        {
            int a=gint(),b=gint();LL ans=0;
            if(a>b)swap(a,b);
            for(int d=1,last;d<=a;d=last+1)
              {
                  last=min(a/(a/d),b/(b/d));
                  upmin(last,a);
                  ans+=(summobi[last]-summobi[d-1])*f[a/d]*f[b/d];
              }
            PF("%I64d\n",ans);
        }
      return 0;
    }
    
View Code

 

posted @ 2015-09-23 16:21  maijing  阅读(303)  评论(0编辑  收藏  举报