传送门

Sol

好神仙的题目。。

一开始就直接莫比乌斯反演然后就 \(GG\) 了

orz 题解

permui

枚举 \(n\),就是求 \(\sum_{i=1}^{n}S(i,m)\)

其中\(S(n,m)=\sum _{i=1}^m\varphi (ni)\)

设 \(n=\prod_{i}p_i^{c_i}\)

设 \(y=\prod _{i=1} p_i^{c_i-1}\),\(w=\prod _{i=1}p_j\)

那么

\[\begin{aligned}
S(n,m)&=y\sum _{i=1}^m\varphi (wi) \\
&=y\sum _{i=1}^m\varphi (\frac{w}{gcd(i,w)})\varphi (i)gcd(i,w) \\
&=y\sum _{i=1}^m\varphi (\frac{w}{gcd(i,w)})\varphi (i)\sum _{e|gcd(i,w)}\varphi (e) \\
&=y\sum _{i=1}^m\varphi (i)\sum _{e|gcd(i,w)}\varphi (\frac{w}{e}) \\
&=y\sum _{i=1}^m\varphi (i)\sum _{e|i,e|w}\varphi (\frac{w}{e}) \\
&=y\sum _{e|w} \varphi (\frac{w}{e})S(e,\lfloor\frac{m}{e}\rfloor) \\
\end{aligned}
\]

运用了 \(n=\sum _{d|n}\varphi (d)\) 去掉了 \(gcd\)

# include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int mod(1e9 + 7);
const int maxn(1e6 + 5);
const int blk(80); inline void Inc(int &x, int y) {
if ((x += y) >= mod) x -= mod;
} int pr[maxn], phi[maxn], tot, d, id1[maxn], id2[maxn], idx, m, ans, sphi[maxn], low[maxn];
bitset <maxn> ispr;
map <int, int> s[maxn]; # define ID(x) ((x) <= d ? id1[x] : id2[m / (x)]) int Sumphi(int x) {
if (x < maxn) return phi[x];
if (sphi[ID(x)]) return sphi[ID(x)];
register int ans = (ll)(x + 1) * x / 2 % mod, i, j;
for (i = 2; i <= x; i = j + 1) j = x / (x / i), Inc(ans, mod - (ll)Sumphi(x / i) * (j - i + 1) % mod);
return sphi[ID(x)] = ans;
} int Calc(int n, int v) {
if (!v || !n) return 0;
if (n == 1) return Sumphi(v);
if (v == 1) return (phi[n] - phi[n - 1] + mod) % mod;
if (s[n].count(v)) return s[n][v];
register int i, j, ret, y = 1, w = 1, x, cnt, e, dv[30];
for (cnt = 0, x = n; x > 1; ) {
w *= low[x], dv[++cnt] = low[x], x /= low[x];
while (low[x] == dv[cnt]) y *= low[x], x /= low[x];
}
for (ret = 0, i = (1 << cnt) - 1; ~i; --i) {
for (e = 1, j = 0; j < cnt; ++j) if (i >> j & 1) e *= dv[j + 1];
Inc(ret, (ll)(phi[w / e] - phi[w / e - 1] + mod) * Calc(e, v / e) % mod);
}
return s[n][v] = (ll)ret * y % mod;
} int main() {
register int i, j, n;
phi[1] = 1, ispr[1] = 1;
for (i = 2; i < maxn; ++i) {
if (!ispr[i]) pr[++tot] = i, phi[i] = i - 1, low[i] = i;
for (j = 1; j <= tot && i * pr[j] < maxn; ++j) {
ispr[i * pr[j]] = 1;
if (i % pr[j]) phi[i * pr[j]] = phi[i] * (pr[j] - 1), low[i * pr[j]] = pr[j];
else {
phi[i * pr[j]] = phi[i] * pr[j], low[i * pr[j]] = low[i];
break;
}
}
}
for (i = 1; i < maxn; ++i) Inc(phi[i], phi[i - 1]);
scanf("%d%d", &n, &m);
for (d = sqrt(m), i = 1; i <= m; i = j + 1) {
j = m / (m / i);
(m / i <= d) ? id1[m / i] = ++idx : id2[j] = ++idx;
}
for (i = 1; i <= n; ++i) Inc(ans, Calc(i, m));
printf("%d\n", ans);
return 0;
}

BZOJ3512:DZY Loves Math IV的更多相关文章

  1. 【BZOJ3512】DZY Loves Math IV(杜教筛)

    [BZOJ3512]DZY Loves Math IV(杜教筛) 题面 BZOJ 求 \[\sum_{i=1}^n\sum_{j=1}^m\varphi(ij)\] 其中\(n\le 10^5,m\l ...

  2. 【bzoj3512】DZY Loves Math IV 杜教筛+记忆化搜索+欧拉函数

    Description 给定n,m,求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\varphi(ij)\)模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅 ...

  3. BZOJ 3512: DZY Loves Math IV [杜教筛]

    3512: DZY Loves Math IV 题意:求\(\sum_{i=1}^n \sum_{j=1}^m \varphi(ij)\),\(n \le 10^5, m \le 10^9\) n较小 ...

  4. BZOJ3512 DZY Loves Math IV

    解:这又是什么神仙毒瘤题...... 我直接把后面那个phi用phi * I = id反演一波,得到个式子,然后推不动了...... 实际上第一步我就大错特错了.考虑到n很小,我们有 然后计算S,我们 ...

  5. BZOJ3512 DZY Loves Math IV(杜教筛+线性筛)

    注意到n很小,考虑枚举i.现在要求的是f(n,m)=Σφ(in) (i=1~m).显然当n没有平方因子时,φ(in)=φ(i)·φ(n/gcd(i,n))·gcd(i,n).利用φ*1=id又可得φ( ...

  6. 数学(数论)BZOJ 3309:DZY Loves Math

    Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...

  7. ●BZOJ 3512 DZY Loves Math IV

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3512 题解: $$求ANS=\sum_{i=1}^{N}\sum_{j=1}^{M}\phi ...

  8. 【刷题】BZOJ 3512 DZY Loves Math IV

    Description 给定n,m,求 模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sampl ...

  9. bzoj 3512: DZY Loves Math IV【欧拉函数+莫比乌斯函数+杜教筛】

    参考:http://blog.csdn.net/wzf_2000/article/details/54630931 有这样一个显然的结论:当\( |\mu(n)|==1 \)时,\( \phi(nk) ...

随机推荐

  1. 编译原理(六)自底向上分析之LR分析法

    自底向上分析之LR分析法 说明:以老师PPT为标准,借鉴部分教材内容,AlvinZH学习笔记. 基本概念 1. LR分析:从左到右扫描(L)自底向上进行规约(R),是规范规约,也即最右推导(规范推导) ...

  2. CountDownLatch、CyclicBarrier和 Semaphore

    在java 1.5中,提供了一些非常有用的辅助类来帮助我们进行并发编程,比如CountDownLatch,CyclicBarrier和Semaphore,今天我们就来学习一下这三个辅助类的用法. 以下 ...

  3. datetime之 utcnow 和now的用法

    from datetime import datetime print(datetime.now()) 本地时间 print(datetime.utcnow()) 国际时间

  4. 修改hosts文件的脚本1.0

    import sys IP_input = input("Please input IP:")DNS_input = input("Please input Domain ...

  5. Mac下关闭Sublime Text 3的更新检查

    操作如下: 注意:update_check的属性前后都要有一个逗号. , "update_check":false, 然后还需要一步,就是注册破解,在[Help]->[Ent ...

  6. Ejb3.0+jboss 8 创建EJB demo

    工具:Eclipse , wildfly 8.x 1.服务端: 1)创建接口 package com.welv.ejb; public interface FirstEjb { public Stri ...

  7. linux通过源码安装nodejs

    1.下载nodejs源码,下载地址:https://nodejs.org/en/download/ 2.下载回来后,解压缩,ubuntu系统ctrl + alt +T 打开终端 假如tar.gz压缩包 ...

  8. JavaScript设计模式-13.组合模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. MySQL数据库-错误1166 - Incorrect column name 'xxx' 的解决方法

    在用Navicat for MySQL给MySQL数据库修改表的字段时报如下的错误: 解决方法:检查字段里面是不是有空格,去掉就可以了.

  10. Linux打开txt文件乱码解决方案

    在ubuntu16.04下打开dic_ec.txt,出现中文乱码. 先输入 gsettings set org.gnome.gedit.preferences.encodings auto-detec ...