[国家集训队]和与积

Problem

  • 给出 n n n ,统计满足以下条件的数对 ( a , b ) (a,b) (a,b) 的个数:
    1. 1 ≤ a &lt; b ≤ n 1≤a&lt;b≤n 1a<bn
    2. a + b ∣ a b a+b|ab a+bab
  • n &lt; 2 31 n&lt;2^{31} n<231

Solution

  • d = g c d ( a , b ) , a = d i , b = d j d=gcd(a,b),a=di,b=dj d=gcd(a,b)a=dib=dj,那么根据题意有:

  • ( d i + d j ) ∣ d 2 i j (di+dj)|d^2ij (di+dj)d2ij

  • 即:

  • ( i + j ) ∣ i j d (i+j)|ijd (i+j)ijd

  • 又因为:

  • g c d ( i , j ) = 1 gcd(i,j)=1 gcd(i,j)=1

  • 所以:

  • i j % ( i + j ) ! = 0 ij\%(i+j)!=0 ij%(i+j)!=0

  • 那么:

  • ( i + j ) ∣ d (i+j)|d (i+j)d

  • 那么问题转化为求满足以下条件的三元组 ( i , j , d ) (i,j,d) (i,j,d) 的个数:
    1. g c d ( i , j ) = 1 gcd(i,j)=1 gcd(i,j)=1
    2. i &lt; j i&lt;j i<j
    3. d i , d j ≤ n di,dj≤n di,djn

  • a n s = ∑ i = 1 n ∑ j = 1 i − 1 ⌊ ⌊ n i ⌋ i + j ⌋ [ g c d ( i , j ) = 1 ] ans=\sum_{i=1}^{\sqrt{n}}\sum_{j=1}^{i-1}\lfloor{\frac{\lfloor{\frac{n}{i}}\rfloor}{i+j}}\rfloor [gcd(i,j)=1] ans=i=1n j=1i1i+jin[gcd(i,j)=1]

  • a n s = ∑ i = 1 n ∑ j = 1 i − 1 ⌊ n i ( i + j ) ⌋ ∑ k ∣ g c d ( i , j ) μ ( k ) ans=\sum_{i=1}^{\sqrt{n}}\sum_{j=1}^{i-1}\lfloor{\frac{n}{i(i+j)}}\rfloor\sum_{k|gcd(i,j)}μ(k) ans=i=1n j=1i1i(i+j)nkgcd(i,j)μ(k)

  • i = x k , j = y k i=xk,j=yk i=xkj=yk,那么:

  • a n s = ∑ k = 1 n μ ( k ) ∑ x = 1 ⌊ n k ⌋ ∑ y = 1 x − 1 ⌊ ⌊ n x k 2 ⌋ x + y ⌋ ans=\sum_{k=1}^{\sqrt{n}}μ(k)\sum_{x=1}^{\lfloor{\frac{\sqrt{n}}{k}}\rfloor}\sum_{y=1}^{x-1}\lfloor{\frac{\lfloor{\frac{n}{xk^2}}\rfloor}{x+y}}\rfloor ans=k=1n μ(k)x=1kn y=1x1x+yxk2n

  • 那么枚举 x , k x,k x,k,然后对 x + y x+y x+y 整除分块即可。

Code

#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int e = 1e6 + 5;
int n, mul[e];
bool bo[e];
ll ans;

inline ll solve(int x, int k)
{
	int m = 2 * x, j, t = n / (x * k * k), i;
	ll res = 0;
	for (i = x + 1; i < m; i = j + 1)
	{
		if (!(t / i)) return res;
		j = min(m - 1, t / (t / i));
		if (j - i + 1 >= 0) res += (ll)(j - i + 1) * (t / i);
	}
	return res;
}

int main()
{
	cin >> n;
	int s = sqrt(n), x, k, i, j;
	for (i = 1; i <= s; i++) mul[i] = 1;
	for (i = 2; i <= s; i++)
	if (!bo[i])
	{
		mul[i] = -1;
		for (j = 2 * i; j <= s; j += i)
		{
			bo[j] = 1;
			if (j / i % i == 0) mul[j] = 0;
			else mul[j] *= -1;
		}
	}
	for (k = 1; k <= s; k++)
	if (mul[k])
	for (x = 1; x * k * k <= n && x * k <= s; x++)
	ans += mul[k] * solve(x, k);
	cout << ans << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值