Description

给出A,B,考虑所有满足l<=a<=A,l<=b<=B,且不存在n>1使得n^2同时整除a和b的有序数

对(a,b),求其lcm(a,b)之和。答案模2^30。

Input

第一行一个整数T表示数据组数。接下来T行每行两个整数A,B表示一组数据。

T ≤ 2000,A,B ≤ 4 × 10^6

Output

对每组数据输出一行一个整数表示答案模2^30的值

Sample Input

5

2 2

4 6

3 4

5 1

23333 33333

Sample Output

7

148

48

15

451085813

Solution

设\(f(i)\)表示\(i\)有无平方因子(有为\(0\),否则为\(1\))

那么题目的式子其实就是

\[\sum_{i=1}^n\sum_{j=1}^mlcm(i,j)f(gcd(i,j))
\]

稍微用一下套路就可以化简到这步:

(设\(S(i)=\sum_{i=1}^ni\) )

\[\sum_{d=1}^n df(d)\sum_{k=1}^{\lfloor \frac{n}{d} \rfloor}\mu(k)k^2S(\lfloor \frac{n}{kd} \rfloor)S(\lfloor \frac{m}{kd} \rfloor)
\]

设\(T=kd\),换元可得

\[\sum_{T=1}^nS(\lfloor \frac{n}{T} \rfloor)S(\lfloor \frac{m}{T} \rfloor)T\sum_{k|T}k\mu(k)f(\frac{T}{k})
\]

后面显然是个积性函数(两个积性函数的点积与另一个积性函数的狄利克雷卷积)

那么讨论一下来筛出来后面的就好了

设\(g(i)=\sum_{k|i}k\mu(k)f(\frac{i}{k})\)

当\(i=1\)时,\(g(i)=1\)

当\(i\in prime\)时,\(g(i)=1-i\)

当\(i\) 有平方质因子时,把平方质因子分离出来,手动展开一下就知道\(g(p^2)=-p,g(p^c)=0(c>2)\)。所以分离出来平方质因子然后分类讨论一下就好

复杂度\(O(n+T\sqrt{n})\)

因为取模是对\(2\)的整数次幂取模,所以直接在最后与\(2^{30}-1\)取与即可。

#include <bits/stdc++.h>
using namespace std; namespace io {
char buf[1<<21], *p1 = buf, *p2 = buf;
inline char gc() {
if(p1 != p2) return *p1++;
p1 = buf;
p2 = p1 + fread(buf, 1, 1 << 21, stdin);
return p1 == p2 ? EOF : *p1++;
}
#define G gc #ifndef ONLINE_JUDGE
#undef G
#define G getchar
#endif template<class I>
inline void read(I &x) {
x = 0; I f = 1; char c = G();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = G(); }
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = G(); }
x *= f;
} template<class I>
inline void write(I x) {
if(x == 0) {putchar('0'); return;}
I tmp = x > 0 ? x : -x;
if(x < 0) putchar('-');
int cnt = 0;
while(tmp > 0) {
buf[cnt++] = tmp % 10 + '0';
tmp /= 10;
}
while(cnt > 0) putchar(buf[--cnt]);
} #define in(x) read(x)
#define outn(x) write(x), putchar('\n')
#define out(x) write(x), putchar(' ') } using namespace io; #define ll long long
const int N = 4000100;
const ll mod = (1 << 30); ll g[N];
int n, m;
int cnt, p[N / 10], vis[N], tot[N], lev[N]; void init() {
g[1] = 1;
for(int i = 2; i < N; ++i) {
if(!vis[i]) {
p[++cnt] = i;
g[i] = 1LL - i;
lev[i] = i;
tot[i] = 1;
}
for(int j = 1; j <= cnt && i * p[j] < N; ++j) {
vis[i * p[j]] = 1;
if(i % p[j] == 0) {
lev[i * p[j]] = lev[i] * p[j];
tot[i * p[j]] = tot[i] + 1;
int tmp = i / lev[i];
if(tot[i * p[j]] == 2) g[i * p[j]] = g[tmp] * (-p[j]);
else g[i * p[j]] = 0;
break;
}
g[i * p[j]] = g[i] * g[p[j]];
lev[i * p[j]] = p[j];
tot[i * p[j]] = 1;
}
}
for(int i = 1; i < N; ++i) g[i] = (i * g[i] + g[i - 1]);
} ll c1(ll n) {
return 1LL * n * (n + 1LL) / 2LL;
} int main() {
int T;
read(T);
init();
while(T--) {
ll ans = 0;
read(n), read(m);
if(n > m) swap(n, m);
for(ll l = 1, r; l <= n; l = r + 1) {
r = min(n / (n / l), m / (m / l));
ans = (ans + c1(n / l) * c1(m / l) * (g[r] - g[l - 1]));
}
outn(ans & (mod - 1));
}
}

BZOJ4659: Lcm的更多相关文章

  1. BZOJ4659:lcm

    传送门 题目所给的不合法的条件可以转化为 \[\exists p,p^2|gcd(a,b) \Leftrightarrow \mu(gcd(a,b))\ne 0\] 那么 \[ans=\sum_{a= ...

  2. [bzoj4659\2694]Lcm_数论_莫比乌斯反演

    Lcm bzoj-4659 bzoj-2694 题目大意:给出A,B,考虑所有满足l<=a<=A,l<=b<=B,且不存在n>1使得n^2同时整除a和b的有序数对(a,b ...

  3. LCM性质 + 组合数 - HDU 5407 CRB and Candies

    CRB and Candies Problem's Link Mean: 给定一个数n,求LCM(C(n,0),C(n,1),C(n,2)...C(n,n))的值,(n<=1e6). analy ...

  4. CodeBlocks及LCM应用

    以下是在开发过程中遇到的一些细节点: 1)called after throwing an instance of std::bad_alloc 此问题是由于publish(data),当中data赋 ...

  5. LCM 轻量级通信组件

    LCM和ZMQ比较 http://www.doc88.com/p-6711552253536.html 基于LCM和ZeroMQ的进程间通信研究 2.简介 LCM(Lightweight Commuc ...

  6. uva12546. LCM Pair Sum

    uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...

  7. UVA 10791 Minimum Sum LCM(分解质因数)

    最大公倍数的最小和 题意: 给一个数字n,范围在[1,2^23-1],这个n是一系列数字的最小公倍数,这一系列数字的个数至少为2 那么找出一个序列,使他们的和最小. 分析: 一系列数字a1,a2,a3 ...

  8. LCM在Kernel中的代码分析

    lcm的分析首先是mtkfb.c 1.mtk_init中platform_driver_register(&mtkfb_driver)注册平台驱动 panelmaster_init(); DB ...

  9. Pairs Forming LCM(素因子分解)

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=109329#problem/B    全题在文末. 题意:在a,b中(a,b<=n) ...

随机推荐

  1. C++全排列 next_permutation

    全排列函数  next_permutation 这是C++的STL中专门用来排列的函数(可以自动处理存在重复数据集的排列问题) 使用时要加上头文件 #include <algorithm> ...

  2. QT QML与C++混搭

    "那些杀不死我的必使我更加强大"----尼采 QML与C++混合编程就是使用QML高效便捷地构建UI,而C++则用来实现业务逻辑和复杂算法. ML访问C++Qt集成了QML引擎和Q ...

  3. Apache Kafka Connect - 2019完整指南

    今天,我们将讨论Apache Kafka Connect.此Kafka Connect文章包含有关Kafka Connector类型的信息,Kafka Connect的功能和限制.此外,我们将了解Ka ...

  4. openstack-nova源码之阅读流程

    以创建虚拟机为例 1.项目入口setup.cfg文件 2.根据nova-compute = nova.cmd.compute:main找到功能入口 3.nova/api/openstack/compu ...

  5. 小贴士--Python

    1.查看python安装好的包版本信息:pip list 原贴,有空完善.http://yangzb.iteye.com/blog/1824761 2.Python文件快速执行. 加头文件快速执行Py ...

  6. Js学习02--变量、关键字、标识符

    一.Js变量的定义 1.定义变量的目的 在内存中分配一块存储空间给变量,方便以后存储数据. 2.如何定义变量 任何变量在使用前都必须定义变量 var 变量名称 eg: var name,age,sex ...

  7. Java中Date时区的转换

    1.Date中保存的是什么?  在java中,只要我们执行 Date date = new Date(); 就可以得到当前时间.如: Date date = new Date(); System.ou ...

  8. How to read request body in a asp.net core webapi controller?

    原文 How to read request body in a asp.net core webapi controller? A clearer solution, works in ASP.Ne ...

  9. Socket HttpListen

    HttpListener sSocket = new HttpListener(); sSocket.Prefixes.Add("http://127.0.0.1:8080/"); ...

  10. bootstrap-datetimepicker 日期控件起始时间和结束时间

    项目中经常会用到起止时间,如下图: 需要引用以下几个文件: <link href="~/lib/bootstrap/dist/css/bootstrap.min.css" r ...