luogu P3768 简单的数学题 杜教筛 + 欧拉反演 + 逆元
#include<bits/stdc++.h>
#define maxn 10200006
#define ll long long
#define M 10000007
using namespace std;
int cnt;
ll sumv[maxn], rev4, rev6, mod, rev2;
bool vis[maxn];
ll phi[maxn], prime[maxn];
map<ll,ll>ansphi;
void setIO(string s)
{
string in=s+".in";
freopen(in.c_str(),"r",stdin);
}
ll qpow(ll base, ll k)
{
ll tmp=1;
while(k)
{
if(k&1) tmp=tmp*base%mod;
base=base*base%mod;
k>>=1;
}
return tmp;
}
void init()
{
int i,j;
rev4=qpow(4ll, mod-2), rev6=qpow(6ll, mod-2), rev2=qpow(2ll, mod-2);
phi[1]=1;
for(i=2;i<=M;++i)
{
if(!vis[i]) prime[++cnt]=i, phi[i]=i-1;
for(j=1;j<=cnt&&1ll*i*prime[j]<=M;++j)
{
vis[i*prime[j]]=1;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
for(i=1;i<=M;++i) sumv[i]=(sumv[i-1]+(1ll*phi[i]*i%mod*i%mod))%mod;
}
// 平方
ll cal1(ll i)
{
i%=mod;
ll re=i%mod;
re=re*(i+1)%mod;
re=re*(i+i+1)%mod;
re=(re*rev6)%mod;
return re;
}
// 立方
ll cal2(ll i)
{
i%=mod;
ll re=i%mod;
re=(re*i)%mod;
re=(re*(i+1))%mod;
re=re*(i+1)%mod;
re=(re*rev4)%mod;
return re;
}
ll get(ll n)
{
if(n<=M) return sumv[n];
if(ansphi[n]) return ansphi[n];
ll i,j,re=cal2(n),tmp;
for(i=2;i<=n;i=j+1)
{
j=n/(n/i);
tmp=(cal1(j)-cal1(i-1)+mod)%mod;
tmp=(tmp*get(n/i))%mod;
re=(re-tmp+mod)%mod;
}
return ansphi[n]=re;
}
ll calc(ll n)
{
n%=mod;
return (((n*(n+1))%mod)*(rev2%mod))%mod ;
}
int main()
{
// setIO("input");
ll n,i,j,re=0,tmp=0;
scanf("%lld%lld",&mod,&n);
init();
for(i=1;i<=n;i=j+1)
{
j=n/(n/i);
tmp=(calc(n/i)*calc(n/i)%mod*(get(j)-get(i-1)+mod)%mod)%mod;
re=(re+tmp+mod)%mod;
}
printf("%lld\n",re);
return 0;
}
luogu P3768 简单的数学题 杜教筛 + 欧拉反演 + 逆元的更多相关文章
- P3768 简单的数学题 [杜教筛,莫比乌斯反演]
\[\sum_{i=1}^{n}\sum_{j=1}^{n} ij\gcd(i,j)\] \[=\sum_{d=1}^{n} d \sum_{i=1}^{n}\sum_{j=1}^{n} ij[\gc ...
- P3768 简单的数学题 杜教筛+推式子
\(\color{#0066ff}{ 题目描述 }\) 由于出题人懒得写背景了,题目还是简单一点好. 输入一个整数n和一个整数p,你需要求出(\(\sum_{i=1}^n\sum_{j=1}^n ij ...
- [luogu3768] 简单的数学题 [杜教筛]
题面: 传送门 实际上就是求: 思路: 看到gcd就先反演一下,过程大概是这样: 明显的一步反演 这里设,S(x)等于1到x的和 然后把枚举d再枚举T变成先枚举T再枚举其约数d,变形: 后面其中两项展 ...
- 51Nod.1237.最大公约数之和 V3(莫比乌斯反演 杜教筛 欧拉函数)
题目链接 \(Description\) \(n\leq 10^{10}\),求 \[\sum_{i=1}^n\sum_{j=1}^ngcd(i,j)\ mod\ (1e9+7)\] \(Soluti ...
- 2019年南京网络赛E题K Sum(莫比乌斯反演+杜教筛+欧拉降幂)
目录 题目链接 思路 代码 题目链接 传送门 思路 首先我们将原式化简: \[ \begin{aligned} &\sum\limits_{l_1=1}^{n}\sum\limits_{l_2 ...
- Luogu P3768 简单的数学题
非常恶心的一道数学题,推式子推到吐血. 光是\(\gcd\)求和我还是会的,但是多了个\(ij\)是什么鬼东西. \[\sum_{i=1}^n\sum_{j=1}^nij\gcd(i,j)=\sum_ ...
- Luogu 4213 【模板】杜教筛(Sum)
当作杜教筛的笔记吧. 杜教筛 要求一个积性函数$f(i)$的前缀和,现在这个东西并不是很好算,那么我们考虑让它卷上另外一个积性函数$g(i)$,使$(f * g)$的前缀和变得方便计算,然后再反推出这 ...
- LOJ# 572. 「LibreOJ Round #11」Misaka Network 与求和(min25筛,杜教筛,莫比乌斯反演)
题意 求 \[ \sum_{i = 1}^{n} \sum_{i = 1}^{n} f(\gcd(i, j))^k \pmod {2^{32}} \] 其中 \(f(x)\) 为 \(x\) 的次大质 ...
- bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
4176: Lucas的数论 Time Limit: 30 Sec Memory Limit: 256 MB Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么 ...
随机推荐
- Ceiling analysis
Course note: Coursera Machine learning by Andrew Ng, 2014, week 10: Application example: photo OCR ( ...
- 杂项-Unicode:Unicode
ylbtech-杂项-Unicode:Unicode Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局 ...
- 002-Visio绘制时序图
一.概述 1.1.什么时候使用 当编码的时候,知道有的用例的业务逻辑按照比较确定的时间先后顺序进行展开.这时候,我们就需要知道我们设计的系统中的不同类之间传递消息(可以认为是不同对象函数间的调用)要按 ...
- 【The type javax.servlet.http.HttpServletRequest cannot be resolved】解决方案
是缺少serverlet的引用库,解决如下 1.工程右键-properties->java build path 2.在java build path的libraries tab页中选择Add ...
- oracle blob 反序列化错误
代码的目的是先将一个配置类JobConfig序列化存进Oracle中的Blob中,然后查的时候反序列化出来. 先看一下控制台报错 ### Cause: com.audaque.lib.core.exc ...
- Learn Python the hard way, ex39 列表的操作
#!/usr/bin/python #coding:utf-8 ten_things = "apples oranges crows telephone light sugar" ...
- 【FICO系列】SAP FICO模块-财务账期的打开和关闭
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO模块-财务账期的 ...
- 【ABAP系列】SAP ABAP实现LOG显示的方法
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP实现LOG显示的 ...
- HDU 1269 迷宫城堡 (Kosaraju)
题目链接:HDU 1269 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000), ...
- hacking 学习站
综合 idf实验室:http://ctf.idf.cn/ writeup: 部分参见本博 网络信息安全攻防学习平台:http://hackinglab.cn/ writeup: 部分参见本博 WeCh ...