BZOJ2694 Lcm

BZOJ2694 Lcm


这题呀,其实除了最后筛积性函数的时候比较困难,剩下的都是套路……
首先要想到的是所有满足条件的\(\mu(gcd(i, j)) \neq 0\),然后就是暴推了。
首先得到的式子是这样的

\[ans = \sum x * \mu(x) ^ 2 \sum _ {d = 1} ^ {\lfloor \frac{n}{x} \rfloor} \mu(d) * d ^ 2 * S(\lfloor \frac{n}{xd} \rfloor) * S(\lfloor \frac{m}{xd} \rfloor) \]

然后用\(T = xd\)替换

\[ans = \sum _ {T = 1} ^ {n} S(\frac{n}{T}) * S(\frac{m}{T}) * \sum_{x | T, \mu(x) \neq 0} x * \mu(\frac{T}{x}) * (\frac{T}{x}) ^ 2 \]

前面的可以数论分块,用狄利克雷卷积易证后面的是一个积性函数,所以可以\(O(n)\)预处理。
至于怎么预处理我实在没推出来,各位可以看看路由器大佬的博客

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 4e6 + 5;
//const ll mod = (1 << 30);
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int v[maxn], prm[maxn];
ll f[maxn], sum[maxn];
void init()
{
  f[1] = 1;
  for(int i = 2; i < maxn; ++i)
    {
      if(!v[i]) v[i] = i, prm[++prm[0]] = i, f[i] = i - i * i;
      for(int j = 1; j <= prm[0] && i * prm[j] < maxn; ++j)
	{
	  v[i * prm[j]] = prm[j];
	  if(i % prm[j] == 0)
	    {
	      int x = i / prm[j];
	      if(x % prm[j]) f[i * prm[j]] = -prm[j] * prm[j] * prm[j] * f[x];
	      else f[i * prm[j]] = 0;
	      break;
	    }
	  else f[i * prm[j]] = f[i] * f[prm[j]];
	}
    }
  for(int i = 1; i < maxn; ++i) sum[i] = sum[i - 1] + f[i];
}

ll s(ll n)
{
  return n * (n + 1) / 2;
}
ll solve(int n, int m)
{
  ll ret = 0;
  int Min = min(n, m);
  for(int l = 1, r; l <= Min; l = r + 1)
    {
      r = min(n / (n / l), m / (m / l));
      ret = ret + (sum[r] - sum[l - 1]) * s(n / l) * s(m / l);
    }
  return ret;
}

int main()
{
  init();
  int T = read();
  while(T--)
    {
      int n = read(), m = read();
      write(solve(n, m) & ((1 << 30) - 1)), enter;
    }
  return 0;
}
上一篇:Gram矩阵是什么


下一篇:5.20爬虫——Mu