P4213 【模板】杜教筛(Sum) min_25筛
\(\color{#0066ff}{ 题目描述 }\)
给定一个正整数\(N(N\le2^{31}-1)\)
求
\(ans_1=\sum_{i=1}^n\varphi(i)\)
\(ans_2=\sum_{i=1}^n \mu(i)\)
\(\color{#0066ff}{输入格式}\)
一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问
\(\color{#0066ff}{输出格式}\)
一共T行,每行两个用空格分隔的数ans1,ans2
\(\color{#0066ff}{输入样例}\)
6
1
2
8
13
30
2333
\(\color{#0066ff}{输出样例}\)
1 1
2 0
22 -2
58 -3
278 -3
1655470 2
\(\color{#0066ff}{数据范围与提示}\)
none
\(\color{#0066ff}{ 题解 }\)
可以用min_25筛写
对于\(\varphi\)
要拆成两个,一个0次项,一个1次项
在收集答案的时候直接加它们两个的差即可
对于\(\mu\)
因为只要指数超过1,就是0了,没用的,不同统计,直接统计1次的就行
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 2e6 + 10;
LL g0[maxn], g1[maxn], a[maxn];
int pri[maxn];
int m, sqt, n, tot;
int getid(LL x) { return x <= sqt? x : m - n / x + 1; }
LL getphi(LL a, int b) {
if(a < pri[b]) return 0;
LL ans = (g1[getid(a)] - g1[getid(pri[b - 1])]) - (g0[getid(a)] - g0[getid(pri[b - 1])]);
for(int i = b; i <= tot && (LL)pri[i] * pri[i] <= a; i++)
for(LL x = pri[i], f = pri[i] - 1; x * pri[i] <= a; x *= pri[i], f *= pri[i])
//phi[p^2]的贡献是p*(p-1)
ans += (getphi(a / x, i + 1) * f + f * pri[i]);
return ans;
}
LL getmu(LL a, int b) {
if(a < pri[b]) return 0;
LL ans = -g0[getid(a)] + g0[getid(pri[b - 1])];
//只需枚举质数,次数就是1即可
for(int i = b; i <= tot && (LL)pri[i] * pri[i] <= a; i++)
//乘的那个f是-1,所以直接减,加的那个f是平方项=0, 不用管
ans -= getmu(a / pri[i], i + 1);
return ans;
}
int main() {
for(int T = in(); T --> 0;) {
n = in();
sqt = sqrt(n);
m = tot = 0;
for(int i = 1; i <= n; i = a[m] + 1)
a[++m] = n / (n / i), g0[m] = a[m] - 1, g1[m] = a[m] * (a[m] + 1) / 2 - 1;
for(int i = 2; i <= sqt; i++) {
if(g0[i] != g0[i - 1]) {
LL sqr = i * i;
pri[++tot] = i;
for(int j = m; a[j] >= sqr; j--) {
int id = getid(a[j] / i);
g0[j] -= g0[id] - g0[i - 1];
g1[j] -= i * (g1[id] - g1[i - 1]);
}
}
}
printf("%lld %lld\n", getphi(n, 1) + 1, getmu(n, 1) + 1);
}
return 0;
}
P4213 【模板】杜教筛(Sum) min_25筛的更多相关文章
- 【51NOD 1847】奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数)
[51NOD 1847]奇怪的数学题(莫比乌斯反演,杜教筛,min_25筛,第二类斯特林数) 题面 51NOD \[\sum_{i=1}^n\sum_{j=1}^nsgcd(i,j)^k\] 其中\( ...
- 【LOJ#572】Misaka Network 与求和(莫比乌斯反演,杜教筛,min_25筛)
[LOJ#572]Misaka Network 与求和(莫比乌斯反演,杜教筛,min_25筛) 题面 LOJ \[ans=\sum_{i=1}^n\sum_{j=1}^n f(gcd(i,j))^k\ ...
- [模板] 杜教筛 && bzoj3944-Sum
杜教筛 浅谈一类积性函数的前缀和 - skywalkert's space - CSDN博客 杜教筛可以在\(O(n^{\frac 23})\)的时间复杂度内利用卷积求出一些积性函数的前缀和. 算法 ...
- luoguP4213 [模板]杜教筛
https://www.luogu.org/problemnew/show/P4213 同 bzoj3944 考虑用杜教筛求出莫比乌斯函数前缀和,第二问随便过,第一问用莫比乌斯反演来做,中间的整除分块 ...
- LOJ572. 「LibreOJ Round #11」Misaka Network 与求和 [莫比乌斯反演,杜教筛,min_25筛]
传送门 思路 (以下令\(F(n)=f(n)^k\)) 首先肯定要莫比乌斯反演,那么可以推出: \[ ans=\sum_{T=1}^n \lfloor\frac n T\rfloor^2\sum_{d ...
- BZOJ.3944.Sum(Min_25筛)
BZOJ 洛谷 不得不再次吐槽洛谷数据好水(连\(n=0,2^{31}-1\)都没有). \(Description\) 给定\(n\),分别求\[\sum_{i=1}^n\varphi(i),\qu ...
- 洛谷P4213(杜教筛)
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int maxn = 3e6 + 3; ...
- LG4213 【模板】杜教筛(Sum)和 BZOJ4916 神犇和蒟蒻
P4213 [模板]杜教筛(Sum) 题目描述 给定一个正整数$N(N\le2^{31}-1)$ 求 $$ans_1=\sum_{i=1}^n\varphi(i)$$ $$ans_2=\sum_{i= ...
- min_25筛入门
目录 1.什么是min_25筛 2.前置知识 2.1.数论函数 2.2.埃拉托色尼筛 2.3.欧拉筛 3.min_25筛 3.1.计算质数贡献 3.2.计算总贡献 3.3.实现 4.例题 4.1.[L ...
随机推荐
- 【转】gem install libv8 错误
转自:http://my.oschina.net/moks/blog/200344 [摘要]Because libv8 is the interface for the V8 engine used ...
- RabbitMQ 消息队列 应用
安装参考 详细介绍 学习参考 RabbitMQ 消息队列 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统.他遵循Mozilla Public License开源协议. M ...
- Celery-4.1 用户指南:Testing with Celery (用 Celery测试)
任务与单元测试 在单元测试中测试任务行为的推荐方法是用mocking. Eager mode: task_always_eager 设置启用的 eager 模式不适用于单元测试. 当使用eager模式 ...
- 将CDM中所有以Relatonship_开头的关系全部重命名,避免生成数据库因为重复关系名报错
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl '当前model '获取当前活 ...
- Eclipse: “The import java.io cannot be resolved”
检查一下选项: 重点看jdk的绑定 43down voteaccepted Check your Eclipse preferences: Java -> Installed JREs. The ...
- Shell编程进阶 2.0 shell中断继续退出
break continue exit break 结束本次for循环 写个for循环脚本 vim for2.sh #!/bin/bash ## 5` do echo $i ] then b ...
- RxAndroid基本使用1
1,基本使用 public class MainActivity extends ActionBarActivity implements View.OnClickListener, View.OnT ...
- UTF8转unicode说明
1.最新版iconv中的char *encTo = "UNICODE//IGNORE"; 是没有这个字符串的,它里面有UNICODELITTLE 和 UNICODEBIG 而且是没 ...
- linux 内核移植和根文件系统的制作
1.1 Linux内核基础知识 在动手进行Linux内核移植之前,非常有必要对Linux内核进行一定的了解,下面从Linux内核的版本和分类说起. 1.1.1 Linux版本 Linux内核的版本号 ...
- vray学习笔记(3)-多维子材质是个什么东西
多维子材质是个什么东西?为什么出现这个概念? 在3dsmax官方网站,我们可以看到它的定义: The Multi/Sub-Object material lets you assign differe ...