杜教筛模板

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

}

相关推荐
博笙困了2 小时前
AcWing学习——差分
c++·算法
NAGNIP2 小时前
认识 Unsloth 框架:大模型高效微调的利器
算法
NAGNIP2 小时前
大模型微调框架之LLaMA Factory
算法
echoarts2 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Python技术极客2 小时前
一款超好用的 Python 交互式可视化工具,强烈推荐~
算法
徐小夕2 小时前
花了一天时间,开源了一套精美且支持复杂操作的表格编辑器tablejs
前端·算法·github
小刘鸭地下城2 小时前
深入浅出链表:从基础概念到核心操作全面解析
算法
青草地溪水旁2 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(2)
c++·设计模式·抽象工厂模式
青草地溪水旁2 小时前
设计模式(C++)详解—抽象工厂模式 (Abstract Factory)(1)
c++·设计模式·抽象工厂模式
小刘鸭地下城2 小时前
哈希表核心精要:从 O(1) 原理到链式地址与开放寻址
算法