http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239

还是模板题。

杜教筛:$$S(n)=\frac{n(n+1)}{2}-\sum_{i=2}^nS\left(\left\lfloor\frac ni\right\rfloor\right)$$

基于质因子分解的筛法:详见2016年论文《积性函数求和的几种方法》(讲得很详细的~~~)

为什么我写的洲哥筛常熟巨大QAQ

杜教筛\(O\left(n^{\frac 23}\right)\)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll N = 1E10;
const int UP = 3981071;
const int mo = 1000000007;
const int ni2 = 500000004; int phi[UP + 3], prime[UP + 3], num = 0, sum[UP + 3];
bool notp[UP + 3]; void Euler_shai() {
sum[1] = phi[1] = 1;
for (int i = 2; i <= UP; ++i) {
if (!notp[i]) {
prime[++num] = i;
phi[i] = i - 1;
}
for (int j = 1, pro; j <= num && (pro = prime[j] * i) <= UP; ++j) {
notp[pro] = true;
if (i % prime[j] == 0) {
phi[pro] = phi[i] * prime[j];
break;
} else
phi[pro] = phi[i] * phi[prime[j]];
}
sum[i] = (sum[i - 1] + phi[i]) % mo;
}
} struct HashTable {
static const int p = 1000007;
ll val[p], ref[p];
HashTable() {memset(ref, -1, sizeof(ref));} void add(ll pos, ll nu) {
int tmp = pos % p;
while (ref[tmp] != -1) {
if (ref[tmp] == pos) return;
++tmp; if (tmp == p) tmp = 0;
}
ref[tmp] = pos;
val[tmp] = nu;
} ll query(ll pos) {
int tmp = pos % p;
while (ref[tmp] != pos) {++tmp; if (tmp == p) tmp = 0;}
return val[tmp];
}
} HT; ll Sum(ll x) {
return x <= UP ? sum[x] : HT.query(x);
} void DJ_shai(ll n) {
for (ll i = n, y; i >= 1; i = n / (y + 1)) {
y = n / i;
if (y <= UP) continue;
ll ret = 0;
for (ll j = 2, l, pre = 1; j <= y; ++j) {
l = y / j;
j = y / l;
ret = (ret + Sum(l) * ((j - pre) % mo) % mo) % mo;
pre = j;
}
HT.add(y, (y % mo * ((y + 1) % mo) % mo * ni2 % mo - ret + mo) % mo);
}
} main() {
Euler_shai();
ll top;
scanf("%lld", &top);
DJ_shai(top);
printf("%lld\n", Sum(top));
return 0;
}

基于质因子分解的筛法\(O\left(\frac{n^{\frac 34}}{\log n}\right)\)

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1E10;
const int UP = 1E5;
const int mo = 1000000007;
const int ni2 = 500000004; bool notp[UP + 3];
ll G0[UP * 2 + 3], G1[UP * 2 + 3], F[UP * 2 + 3], J[UP * 2 + 3];
int prime[UP + 3], sum_prime[UP + 3], sum_phi[UP + 3], phi[UP + 3], sum_p[UP + 3];
int pre[UP * 2 + 3], num = 0, ma[UP + 3]; void Euler_shai(int n) {
phi[1] = sum_phi[1] = 1;
for (int i = 2; i <= n; ++i) {
if (!notp[i]) {
prime[++num] = i;
sum_prime[num] = (sum_prime[num - 1] + i) % mo;
phi[i] = i - 1;
sum_p[i] = (sum_p[i - 1] + i - 1) % mo;
ma[i] = num;
} else {
sum_p[i] = sum_p[i - 1];
ma[i] = ma[i - 1];
}
for (int j = 1, pro; j <= num && (pro = i * prime[j]) <= n; ++j) {
notp[pro] = true;
if (i % prime[j] == 0) {
phi[pro] = 1ll * phi[i] * prime[j] % mo;
break;
} else
phi[pro] = 1ll * phi[i] * phi[prime[j]] % mo;
}
sum_phi[i] = (sum_phi[i - 1] + phi[i]) % mo;
}
} struct HashTable {
static const int ppp = 2333333;
ll ref[ppp]; int val[ppp];
void clr() {memset(ref, -1, sizeof(ref)); ref[0] = val[0] = 0;} void add(ll pos, int nu) {
int tmp = pos % ppp;
while (ref[tmp] != -1) {++tmp; if (tmp == ppp) tmp = 0;}
ref[tmp] = pos; val[tmp] = nu;
} int query(ll pos) {
int tmp = pos % ppp;
while (ref[tmp] != pos) {++tmp; if (tmp == ppp) tmp = 0;}
return val[tmp];
}
} HT; #define maa(x) (x >= sqc ? num : ma[x]) ll ZY_shai(ll n) {
int cnt = 0, sqf = floor(sqrt(n)), sqc = ceil(sqrt(n)); HT.clr();
for (ll i = n, y; i >= 1; i = n / (y + 1)) {
J[++cnt] = (y = n / i);
HT.add(y, cnt);
G0[cnt] = y;
G1[cnt] = y % mo * ((y + 1) % mo) % mo * ni2 % mo;
pre[cnt] = 0;
} ll pp, delta;
for (int i = 1, p = prime[i]; i <= num; p = prime[++i]) {
pp = 1ll * p * p;
for (int j = cnt; j >= 1 && J[j] >= pp; --j) {
int id = HT.query(J[j] / p);
delta = max(G0[id] - (i - 1 - pre[id]), 1ll);
G0[j] -= delta;
delta = (G1[id] - ((sum_prime[min(i - 1, maa(J[id]))] - sum_prime[pre[id]] + mo) % mo) + mo) % mo;
G1[j] = (G1[j] - p * delta % mo + mo) % mo;
pre[j] = i;
}
} for (int j = cnt; j >= 1; --j) {
G0[j] = max(G0[j] - (num - pre[j]), 1ll);
G1[j] = (G1[j] - ((sum_prime[maa(J[j])] - sum_prime[pre[j]] + mo) % mo) + mo) % mo;
} ll ans = 0;
for (int i = 1; i < sqc; ++i) {
int id = HT.query(n / i);
ans = (ans + (1ll + G1[id] - G0[id] + mo) % mo * phi[i] % mo) % mo;
} ll prep = 0, sqrprep;
for (int j = 1; j <= cnt; ++j) F[j] = 1;
for (int i = num, p = prime[i]; i >= 1; p = prime[--i]) {
pp = 1ll * p * p;
for (int j = cnt; j >= 1 && J[j] >= pp; --j) {
ll J_j = J[j];
if (J_j < sqrprep) {
if (J_j >= prep) F[j] = (1 + sum_p[min(J_j, 1ll * sqf)] - sum_p[prep - 1] + mo) % mo;
else F[j] = 1;
}
int id = HT.query(J_j / p);
if (J[id] < sqrprep) {
if (J[id] >= prep) delta = (1 + sum_p[min(J[id], 1ll * sqf)] - sum_p[prep - 1] + mo) % mo;
else delta = 1;
} else
delta = F[id];
F[j] = (F[j] + (p - 1) * delta % mo) % mo; ll pic = pp;
while (J_j >= pic) {
id = HT.query(J_j / pic);
if (J[id] < sqrprep) {
if (J[id] >= prep) delta = (1 + sum_p[min(J[id], 1ll * sqf)] - sum_p[prep - 1] + mo) % mo;
else delta = 1;
} else
delta = F[id];
F[j] = (F[j] + pic / p * (p - 1) % mo * delta % mo) % mo;
pic *= p;
}
}
prep = p; sqrprep = pp;
} return ((ans + F[cnt]) % mo - sum_phi[sqc - 1] + mo) % mo;
} int main() {
ll top;
scanf("%lld\n", &top);
Euler_shai((int) sqrt(top));
printf("%lld\n", ZY_shai(top));
return 0;
}

【51Nod 1239】欧拉函数之和的更多相关文章

  1. [51Nod 1244] - 莫比乌斯函数之和 & [51Nod 1239] - 欧拉函数之和 (杜教筛板题)

    [51Nod 1244] - 莫比乌斯函数之和 求∑i=1Nμ(i)\sum_{i=1}^Nμ(i)∑i=1N​μ(i) 开推 ∑d∣nμ(d)=[n==1]\sum_{d|n}\mu(d)=[n== ...

  2. 51nod 1239 欧拉函数之和(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 [题目大意] 计算欧拉函数的前缀和 [题解] 我们 ...

  3. 51nod 1239 欧拉函数之和【欧拉函数+杜教筛】

    和bzoj 3944比较像,但是时间卡的更死 设\( f(n)=\sum_{d|n}\phi(d) g(n)=\sum_{i=1}^{n}f(i) s(n)=\sum_{i=1}^{n}\phi(i) ...

  4. 51 NOD 1239 欧拉函数之和(杜教筛)

    1239 欧拉函数之和 基准时间限制:3 秒 空间限制:131072 KB 分值: 320 难度:7级算法题 收藏 关注 对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究 ...

  5. 【51nod】1239 欧拉函数之和 杜教筛

    [题意]给定n,求Σφ(i),n<=10^10. [算法]杜教筛 [题解] 定义$s(n)=\sum_{i=1}^{n}\varphi(i)$ 杜教筛$\sum_{i=1}^{n}(\varph ...

  6. 【51nod】1239 欧拉函数之和

    题解 写完上一道就开始写这个,大体上就是代码改了改而已= = 好吧,再推一下式子! \(\sum_{i = 1}^{n}i = \sum_{i = 1}^{n}\sum_{d | i}\phi(d) ...

  7. 51Nod 1239 欧拉函数前n项和 杜教筛

    http://www.51nod.com/Challenge/Problem.html#!#problemId=1239 AC代码 #include <bits/stdc++.h> #de ...

  8. 51nod1239 欧拉函数之和

    跟1244差不多. //由于(x+1)没有先mod一下一直WA三个点我... //由于(x+1)没有先mod一下一直WA三个点我... #include<cstdio> #include& ...

  9. 欧拉函数之和(51nod 1239)

    对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...

  10. 【51nod-1239&1244】欧拉函数之和&莫比乌斯函数之和 杜教筛

    题目链接: 1239:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1239 1244:http://www.51nod. ...

随机推荐

  1. 如果你也想写个完整的 Vue 组件项目

    1.一个完整的组件项目需要什么? 必要的: 组件构建方式 ( webpack / rollup 之类 ),并提供至少一个主流的输出格式 (ESModule) Demo 及 Demo 源码 文档,可以是 ...

  2. 当你启动Redis的时候,Redis做了什么

    直奔主题,当启动Redis的时候,Redis执行了哪些操作? 假设Redis安装在了/usr/local/目录下,那么启动Redis是通过执行/usr/local/bin/redis-server - ...

  3. Sql Server 2014/2012/2008/2005 数据库还原出现 3154错误的解决办法

    在Sql Server  数据库还原出现 3154错误 解决方法1:不要在数据库名字上点右键选择还原,而要是在根目录“数据库”三个字上点右键选择还原,然后再选择数据库,问题便可以解决,如果不行参照方法 ...

  4. 【bzoj3545】peaks

    离线一下,动态开点+线段树合并,然后权值线段树上询问kth即可. #include<bits/stdc++.h> ; *; using namespace std; ; inline in ...

  5. java 和 JVM

    C++和Java的区别 指针:java中不存在指针的概念,编程者无法直接通过指针来直接访问内存,有利于维护java程序的安全 多重继承:C++支持多重继承,java不支持多重继承,但是允许一个类继承多 ...

  6. [New learn]讲解Objective-c的block知识-实践

    1.简介 在之前的文章[New learn]讲解Objective-c的block知识中介绍了block的相关知识.本章中我们将以一个实际例子来简单介绍一下block如何代替代理. 2.原有通过代理实 ...

  7. 字符串aaaa......bbbb....ccc...dddddd用正则替换为abcd

    public static void main(String[] args) { String s = "aaaa......bbbb....ccc...dddddd"; Stri ...

  8. Spring boot 集成hessian - LocalDateTime序列化和反序列化

    - 反序列化 import com.caucho.hessian.HessianException; import com.caucho.hessian.io.AbstractDeserializer ...

  9. TP-LINK路由器设置内网的一台电脑在外网可以远程操控

    1.[IP和MAC绑定]--[静态ARP绑定设置]对MAC和IP进行绑定 2.[转发规则]--[DMZ主机],选择启用并把刚才设置的内网IP填入 3.直接访问路由器的外网IP就可以直接访问绑定的MAC ...

  10. Linux下GCC相关知识点

    摘要: 总结GCC的具体使用,动态库静态库的相关问题 参考资料: <Linux网络编程> ISBN:9787302207177 p19 1 GCC简介 GCC是Linux下的编译工具集,是 ...