bzoj 3944: Sum(杜教筛)
3944: Sum
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 4930 Solved: 1313
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1
2
8
13
30
2333
Sample Output
2 0
22 -2
58 -3
278 -3
1655470 2
/*
就相当于111112222222333333333每个数都有相同的一坨,然后只算出一坨的第一个数,乘个他的次数,直接跳到下一坨
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define maxn 5000005
#define N 5000000
using namespace std;
int p[maxn],mu[maxn],cnt;
long long phi[maxn];
bool vis[maxn];
void prepare(){
memset(vis,,sizeof(vis));
phi[]=;mu[]=;
for(int i=;i<=N;i++){
if(!vis[i])p[++cnt]=i,phi[i]=i-,mu[i]=-;
for(int j=;j<=cnt&&i*p[j]<=N;j++){
vis[i*p[j]]=;
if(i%p[j]==){
phi[i*p[j]]=phi[i]*p[j];
mu[p[j]*i]=;break;
}
phi[i*p[j]]=phi[i]*phi[p[j]];
mu[i*p[j]]=-mu[i];
}
}
for(int i=;i<=N;i++)phi[i]+=phi[i-],mu[i]+=mu[i-];
}
map<long long,long long>lst_phi;
map<long long,int>lst_mu;
long long sum_phi(long long n){
if(n<=N)return phi[n];
if(lst_phi[n]!=)return lst_phi[n];
long long res=n*(n+)/;
for(long long i=,nxt;i<=n;i=nxt+){
nxt=n/(n/i);
res-=sum_phi(n/i)*(nxt-i+);
}
return lst_phi[n]=res;
}
int sum_mu(long long n){
if(n<=N)return mu[n];
if(lst_mu[n]!=)return lst_mu[n];
int res=;
for(long long i=,nxt;i<=n;i=nxt+){
nxt=n/(n/i);
res-=sum_mu(n/i)*(nxt-i+);
}
return lst_mu[n]=res;
}
int main(){
freopen("Cola.txt","r",stdin);
prepare();
int Q;scanf("%d",&Q);
while(Q--){
int n;scanf("%d",&n);
cout<<sum_phi(n)<<' '<<sum_mu(n)<<endl;
}
}
bzoj 3944: Sum(杜教筛)的更多相关文章
- bzoj 3944 Sum —— 杜教筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3944 杜教筛入门题! 看博客:https://www.cnblogs.com/zjp-sha ...
- BZOJ 3944: Sum [杜教筛]
3944: Sum 贴模板 总结见学习笔记(现在还没写23333) #include <iostream> #include <cstdio> #include <cst ...
- 3944: Sum[杜教筛]
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3471 Solved: 946[Submit][Status][Discuss] ...
- [BZOJ3944]Sum(杜教筛)
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6201 Solved: 1606[Submit][Status][Discuss ...
- 洛谷P4213 Sum(杜教筛)
题目描述 给定一个正整数N(N\le2^{31}-1)N(N≤231−1) 求ans_1=\sum_{i=1}^n\phi(i),ans_2=\sum_{i=1}^n \mu(i)ans1=∑i=1 ...
- [bzoj3944] sum [杜教筛模板]
题面: 传送门 就是让你求$ \varphi\left(i\right) $以及$ \mu\left(i\right) $的前缀和 思路: 就是杜教筛的模板 我们把套路公式拿出来: $ g\left( ...
- bzoj3944: Sum 杜教筛板子题
板子题(卡常) 也可能是用map太慢了 /************************************************************** Problem: 3944 Us ...
- ●杜教筛入门(BZOJ 3944 Sum)
入门杜教筛啦. http://blog.csdn.net/skywalkert/article/details/50500009(好文!) 可以在$O(N^{\frac{2}{3}})或O(N^{\f ...
- bzoj 3944: Sum【莫比乌斯函数+欧拉函数+杜教筛】
一道杜教筛的板子题. 两个都是积性函数,所以做法是一样的.以mu为例,设\( f(n)=\sum_{d|n}\mu(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1} ...
随机推荐
- cf 429B Working out(简单dp)
B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Web中常用字体介绍
1.在Web编码中,CSS默认应用的Web字体是有限的,虽然在新版本的CSS3,我们可以通过新增的@font-face属性来引入特殊的浏览器加载字体. 浏览器中展示网页文字内容时,文字字体都会按照设计 ...
- 4_Prototype 原型
#Prototype ``` // 不好的做法 monster ghost demon sorcerer class Spawner { public: virtual ~Spawner() {} ; ...
- codeforces 612D The Union of k-Segments (线段排序)
D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...
- leetcode 231 Power of Two(位运算)
Given an integer, write a function to determine if it is a power of two. 题解:一次一次除2来做的话,效率低.所以使用位运算的方 ...
- 【leetcode刷题笔记】Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- FFMPEG-AVFilter研究
FFMPEG中的libswscale是做像素转换的,但是对于一些复杂的操作,比如添加水印等,这个库就不行了,这时候就要说一下另外一个AVFilter.AVFilter完全可以替代libswscale的 ...
- 【LeetCode】015 3Sum
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...
- 装了Ironpython还需要装Python吗?
IronPython 是一种在 NET 和 Mono 上实现的 Python 语言,由 Jim Hugunin(同时也是 Jython 创造者)所创造.IronPython是优雅的python编程语言 ...
- python setuptools安装
执行# python setup.py install 时发生如下错误 Traceback (most recent call last): File "setup.py", li ...