杜教筛模板

#define _CRT_SECURE_NO_WARNINGS

#include<iostream>

#include<string>

#include<cstring>

#include<cmath>

#include<ctime>

#include<algorithm>

#include<utility>

#include<stack>

#include<queue>

#include<vector>

#include<set>

#include<map>

#include<unordered_map>

using namespace std;

#define LL long long

const int N = 2e6 + 10;

const long long mod = 998244353;

const double PI = acos(-1);

LL su[N], a[N], mu[N], phi[N], cn;

LL n;

map<LL, LL> mpu, mpp;

void init()

{

a[0] = a[1] = mu[1] = phi[1] = 1;

for (int i = 2; i < N; i++)

{

if (!a[i]) su[++cn] = i, mu[i] = -1, phi[i] = i - 1;

for (int j = 1; j <= cn&&(LL)i*su[j]<N; j++)

{

int pm = su[j];

a[pm * i] = 1;

if (i % pm == 0)

{

phi[i * pm] = phi[i] * pm;

break;

}

mu[i * pm] = -1 * mu[i];

phi[i * pm] = phi[i] * (pm - 1);

}

}

for (int i = 1; i < N; i++)

mu[i] += mu[i - 1], phi[i] += phi[i - 1];

}

LL seekphi(LL n)

{

if (n < N) return phi[n];

if (mpp[n]) return mpp[n];

LL ans = n * (n + 1) / 2;

for (LL l = 2, r; l <= n; l = r + 1)

{

if (n / l == 0) r = n;

else

r = min(n / (n / l),n);

ans -= (r - l + 1) * seekphi(n / l);

}

mpp[n] = ans;

return ans;

}

LL seekmu(LL n)

{

if (n < N) return mu[n];

if (mpu[n]) return mpu[n];

LL ans = 1;

for (LL l = 2, r; l <= n; l = r + 1)

{

if (n / l == 0) r = n;

else

r = min(n / (n / l), n);

ans -= (r - l + 1) * seekmu(n / l);

}

mpu[n] = ans;

return ans;

}

int main() {

init();

int T;

cin >> T;

while (T--)

{

cin >> n;

LL ans1 =seekphi(n),ans2=seekmu(n);

cout << ans1 <<" "<<ans2<< endl;

}

return 0;

}

相关推荐
业精于勤的牙1 天前
浅谈:算法中的斐波那契数(二)
算法·职场和发展
不穿格子的程序员1 天前
从零开始写算法——链表篇4:删除链表的倒数第 N 个结点 + 两两交换链表中的节点
数据结构·算法·链表
liuyao_xianhui1 天前
寻找峰值--优选算法(二分查找法)
算法
dragoooon341 天前
[hot100 NO.19~24]
数据结构·算法
神仙别闹1 天前
基于QT(C++)实现学本科教务系统(URP系统)
数据库·c++·qt
deng-c-f1 天前
Linux C/C++ 学习日记(49):线程池
c++·学习·线程池
ulias2121 天前
C++ 的容器适配器——从stack/queue看
开发语言·c++
daidaidaiyu1 天前
FFmpeg 关键的结构体
c++·ffmpeg
Tony_yitao1 天前
15.华为OD机考 - 执行任务赚积分
数据结构·算法·华为od·algorithm