杜教筛模板

#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;

}

相关推荐
gsfl42 分钟前
贪心算法1
算法·贪心算法
kk”1 小时前
C++ stack 和 queue
开发语言·c++
小猪咪piggy1 小时前
【算法】day8 二分查找+前缀和
算法
Word码1 小时前
[排序算法]希尔排序
c语言·数据结构·算法·排序算法
前端小刘哥1 小时前
解析视频直播点播平台EasyDSS在视频点播领域的技术架构与性能优势
算法
给大佬递杯卡布奇诺1 小时前
FFmpeg 基本API avcodec_send_packet函数内部调用流程分析
c++·ffmpeg·音视频
QT 小鲜肉1 小时前
【数据结构与算法基础】05. 栈详解(C++ 实战)
开发语言·数据结构·c++·笔记·学习·算法·学习方法
lingran__1 小时前
算法沉淀第七天(AtCoder Beginner Contest 428 和 小训练赛)
c++·算法
前端小刘哥1 小时前
新版视频直播点播平台EasyDSS,打通远程教研与教师培训新通路
算法
2401_840105201 小时前
P1049 装箱问题 题解(四种方法)附DP和DFS的对比
c++·算法·深度优先·动态规划