BZOJ 3944: Sum [杜教筛]
3944: Sum
贴模板
总结见学习笔记(现在还没写23333)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
#define pii pair<ll, ll>
#define fir first
#define sec second
const int N=1664512, U=1664510;
inline ll read(){
char c=getchar(); ll x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
bool notp[N]; int p[N/10], mu[N]; ll phi[N];
void sieve(int n) {
mu[1] = phi[1] = 1;
for(int i=2; i<=n; i++) {
if(!notp[i]) p[++p[0]] = i, mu[i] = -1, phi[i] = i-1;
for(int j=1; j <= p[0] && i*p[j] <= n; j++) {
int t = i*p[j];
notp[t] = 1;
if(i % p[j] == 0) {mu[t] = 0; phi[t] = phi[i]*p[j]; break;}
mu[t] = -mu[i]; phi[t] = phi[i]*(p[j]-1);
}
mu[i] += mu[i-1]; phi[i] += phi[i-1];
}
}
ll n;
pii nul(-1, -1);
namespace ha {
const int p = 1001001;
struct meow{int ne; pii val; ll r;} e[30000];
int cnt=1, h[p];
inline void insert(ll x, pii &val) {
ll u = x % p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return;
e[++cnt] = (meow){h[u], val, x}; h[u]=cnt;
}
inline pii quer(ll x) {
ll u = x % p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return e[i].val;
return nul;
}
} using ha::insert; using ha::quer;
pii dj_sieve(ll n) {
if(n <= U) return make_pair(mu[n], phi[n]);
if(quer(n) != nul) return quer(n);
pii ans(1, n*(n+1)/2); ll r;
for(ll i=2; i<=n; i=r+1) {
r = n/(n/i);
pii t = dj_sieve(n/i);
ans = make_pair(ans.fir - t.fir * (r-i+1), ans.sec - t.sec * (r-i+1));
}
insert(n, ans);
return ans;
}
int main() {
freopen("in", "r", stdin);
sieve(U);
int T=read();
while(T--) {
n=read(); if(n==0) {puts("0 0"); continue;}
pii t = dj_sieve(n);
printf("%lld %lld\n", t.sec, t.fir);
}
}
BZOJ 3944: Sum [杜教筛]的更多相关文章
- bzoj 3944 Sum —— 杜教筛
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3944 杜教筛入门题! 看博客:https://www.cnblogs.com/zjp-sha ...
- 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(杜教筛)
3944: Sum Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4930 Solved: 1313[Submit][Status][Discuss ...
- bzoj 3944: Sum【莫比乌斯函数+欧拉函数+杜教筛】
一道杜教筛的板子题. 两个都是积性函数,所以做法是一样的.以mu为例,设\( f(n)=\sum_{d|n}\mu(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1} ...
随机推荐
- Wolf and Rabbit
http://acm.hdu.edu.cn/showproblem.php?pid=1222 Wolf and Rabbit Time Limit: 2000/1000 MS (Java/Others ...
- c语言基础学习09_复合类型
=============================================================================涉及到的知识点有:一.结构体1.定义结构体st ...
- c++(线性堆栈)
前面我们讲到了队列,今天我们接着讨论另外一种数据结构:堆栈.堆栈几乎是程序设计的命脉,没有堆栈就没有函数调用,当然也就没有软件设计.那么堆栈有什么特殊的属性呢?其实,堆栈的属性主要表现在下面两个方面: ...
- Oracle忘记密码如何重
---恢复内容开始--- 昨天安装Oracle11g R2的时候给scott用户设置密码,当时没有显示而且还只以输入一次,可能密码输入错误,结果今天用scott用户登录果然密码不对,还好sys和sys ...
- Entity framework 中Where、First、Count等查询函数使用时要注意
在.Net开发中,Entity framework是微软ORM架构的最佳官方工具.我们可以使用Lambda表达式在Entity framework中DbSet<T>类上直接做查询(比如使用 ...
- 属性getter和setter
我们知道,对象属性是由名字.值和一组特性(attribute)构成的.在ECMAScript5 中,属性值可以用一个或两个方法替代,这两个方法就是getter和setter.由getter和sette ...
- Spark算子--cogroup
转载请标明出处http://www.cnblogs.com/haozhengfei/p/b612b1e6d9b951fad5574cd0ce573d7e.html cogroup--Transform ...
- 分享给有需要的你,精选了10套web开发免费视频教程~~
在这里给大家精选百度传课里的一些比较好的视频教程,比较适合新手们,有需要的可以收藏着学习!! 蓝鸥微信小程序全方位深度解析 链接:http://www.chuanke.com/v4702151-19 ...
- LNMP下FTP服务器的安装和使用(Pureftpd和Proftpd)
FTP是网站文件维护中使用比较多的,目前LNMP一键安装包中有Pureftpd和Proftpd服务器安装脚本,LNMP默认不安装任何FTP服务器,需要用户自行安装(1.2开始不再提供proftpd的安 ...
- windows 防火墙拦截nginx的问题
今天在azure vm上安装了nginx并配置了代理设置,但域名访问始终无法中转,一开始怀疑是nginx的服务没起来,但在本地访问localhost看下如下界面,证明服务是没问题的. 本地访问没问题, ...