BZOJ3512:DZY Loves Math IV
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\)
那么
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的更多相关文章
- 【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 ...
- 【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 仅 ...
- 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较小 ...
- BZOJ3512 DZY Loves Math IV
解:这又是什么神仙毒瘤题...... 我直接把后面那个phi用phi * I = id反演一波,得到个式子,然后推不动了...... 实际上第一步我就大错特错了.考虑到n很小,我们有 然后计算S,我们 ...
- 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又可得φ( ...
- 数学(数论)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, ...
- ●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 ...
- 【刷题】BZOJ 3512 DZY Loves Math IV
Description 给定n,m,求 模10^9+7的值. Input 仅一行,两个整数n,m. Output 仅一行答案. Sample Input 100000 1000000000 Sampl ...
- bzoj 3512: DZY Loves Math IV【欧拉函数+莫比乌斯函数+杜教筛】
参考:http://blog.csdn.net/wzf_2000/article/details/54630931 有这样一个显然的结论:当\( |\mu(n)|==1 \)时,\( \phi(nk) ...
随机推荐
- leetcode-79-单词搜索(用dfs解决)
题目描述: 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许 ...
- Java导包问题
eclipse中,导包只可以导到当前层,不可以再导入包里面的包中的类 包目录如下: 导包如下 package def; import abc.*; public class CCC { public ...
- 基于python的几种排序算法的实现
#!usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2019/3/28 10:26 # @Author : Yosef-夜雨声烦 # @Email ...
- ECharts概念学习系列之ECharts的下载和安装(图文详解)
不多说,直接上干货! http://echarts.baidu.com/download.html 前言 如果你想要用较少的代码实现比较酷炫的数据统计表,echarts是值得你考虑的一种实现方式.官网 ...
- Cassandra概念学习系列之Windows里下载且安装配置Cassandra(最新的3.11.1版本)(图文详解)
不多说,直接上干货! 最近我开始在windows环境中使用Cassandra,虽然在Cassandra站点的安装命令非常清楚和精简,我仍然在环境配置上遇到一些问题.所以我想为后来者分享下我的经验. ...
- ie和火狐事件addEventListener()及attachEvent()区别分析
Mozilla中: addEventListener的使用方式: target.addEventListener(type, listener, useCapture); target: 文档节点.d ...
- js中字符串的替换
定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法 stringObject.replace(regexp/substr,replac ...
- PTA (Advanced Level) 1013 Battle Over Cities
Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...
- 通配符证书导致 Outlook Anywhere 的客户端连接问题
通配符证书导致 Outlook Anywhere 的客户端连接问题 本主题介绍当您使用 Outlook Anywhere 连接到 Microsoft Exchange 及在组织中跨 Exchange ...
- vs2015 点击cshtml 后提示 "无效指针" 的解决办法
1. 关闭vs 2. 删除 %LocalAppData%\Microsoft\VisualStudio\14.0\ComponentModelCache 3. 打开vs OK 解决