题意:给定函数\(f(x)\),有\(n^2-3*n+2=\sum_{d|n}f(d)\),求\(\sum_{i=1}^nf(i)\)

题解:很显然的杜教筛,假设\(g(n)=n^2-3*n+2\),那么有\(g=f*I\),由莫比乌斯反演,\(f=g*\mu\),可以O(nlogn)预处理到1e6,剩余部分杜教筛

我们先观察杜教筛的推导过程,假设要求\(s(n)=\sum_{i=1}^nf(i)\),

\(\sum_{i=1}^ng*f=\sum_{i=1}^n\sum_{d|i}g(d)f(\frac{i}{d})=\sum_{d=1}^ng(d)\sum_{i=1}^{\lfloor \frac{n}{d} \rfloor}f(i)=\sum_{d=1}^ng(d)S(\lfloor \frac{n}{d} \rfloor)\)

\(S(n)=\sum_{i=1}^ng*f-\sum_{i=1}^ng(d)S(\lfloor \frac{n}{d} \rfloor)\)

我们考虑s就是我们要求的答案,g是常函数,那么I*f就是g,所以前半部分即\(\sum_{i=1}^ng(i)\)

分块处理后半部分,复杂度\(O(n^{\frac{2}{3})\)

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
//#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
template<typename T>
inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>
inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const double eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=1000000+10,maxn=3000000+10,inf=0x3f3f3f3f; int prime[N],cnt,mu[N];
bool mark[N];
ll f[N];
map<ll,ll>ff;
map<ll,ll>::iterator it1;
ll inv3=qp(3,mod-2);
void init()
{
mu[1]=1;
for(int i=2;i<N;i++)
{
if(!mark[i])prime[++cnt]=i,mu[i]=-1;
for(int j=1;j<=cnt&&i*prime[j]<N;j++)
{
mark[i*prime[j]]=1;
if(i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
mu[i*prime[j]]=-mu[i];
}
}
for(int i=1;i<N;i++)
for(int j=i;j<N;j+=i)
{
ll te=1ll*(j/i-2)*(j/i-1)*mu[i];
te=(te%mod+mod)%mod;
add(f[j],te);
}
// printf("%lld\n",f[1000000]);
for(int i=1;i<N;i++)add(f[i],f[i-1]);
}
ll getf(ll n)
{
if(n<N)return f[n];
if((it1=ff.find(n))!=ff.end())return it1->se;
ll ans=n*(n+1)%mod*(n-4)%mod*inv3%mod+2ll*n%mod;
ans=(ans%mod+mod)%mod;
for(ll i=2,j;i<=n;i=j+1)
{
j=n/(n/i);
sub(ans,1ll*(j-i+1)*getf(n/i)%mod);
}
return ff[n]=ans;
}
int main()
{
init();
int T;scanf("%d",&T);
while(T--)
{
ll n;scanf("%lld",&n);
printf("%lld\n",getf(n));
}
return 0;
}
/******************** ********************/

hdu5608杜教筛的更多相关文章

  1. 51nod 1244 莫比乌斯函数之和(杜教筛)

    [题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...

  2. 51nod 1237 最大公约数之和 V3(杜教筛)

    [题目链接] https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1237 [题目大意] 求[1,n][1,n]最大公约数之和 ...

  3. 杜教筛 && bzoj3944 Sum

    Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1,ans ...

  4. 51NOD 1220 约数之和 [杜教筛]

    1220 约数之和 题意:求\(\sum_{i=1}^n \sum_{j=1}^n \sigma_1(ij)​\) \[ \sigma_0(ij) = \sum_{x\mid i}\sum_{y\mi ...

  5. BZOJ 4176: Lucas的数论 [杜教筛]

    4176: Lucas的数论 题意:求\(\sum_{i=1}^n \sum_{j=1}^n \sigma_0(ij)\) \(n \le 10^9\) 代入\(\sigma_0(nm)=\sum_{ ...

  6. 51NOD 1222 最小公倍数计数 [莫比乌斯反演 杜教筛]

    1222 最小公倍数计数 题意:求有多少数对\((a,b):a<b\)满足\(lcm(a,b) \in [1, n]\) \(n \le 10^{11}\) 卡内存! 枚举\(gcd, \fra ...

  7. 51NOD 1237 最大公约数之和 V3 [杜教筛]

    1237 最大公约数之和 V3 题意:求\(\sum_{i=1}^n\sum_{j=1}^n(i,j)\) 令\(A(n)=\sum_{i=1}^n(n,i) = \sum_{d\mid n}d \c ...

  8. hihocoder #1456 : Rikka with Lattice(杜教筛)

    hihocoder #1456 : Rikka with Lattice(杜教筛) 题意 : 给你一个\(n*m\)方格图,统计上面有多少个格点三角形,除了三个顶点,不覆盖其他的格点(包括边和内部). ...

  9. 【BZOJ4805】欧拉函数求和(杜教筛)

    [BZOJ4805]欧拉函数求和(杜教筛) 题面 BZOJ 题解 好久没写过了 正好看见了顺手切一下 令\[S(n)=\sum_{i=1}^n\varphi(i)\] 设存在的某个积性函数\(g(x) ...

随机推荐

  1. oracle 之 基础操作

    //删除存在的表空间及数据 drop tablespace TS_YYGL including contents and datafiles 若是出现了提示 错误 导致无法全部删除,那么就执行以下语句 ...

  2. Unity3D学习笔记(二十六):MVC框架下的背包系统(1)

    MVC背包 需求: 1.背包格子的装备是可以拖动的 2.装备栏的装备也是可以拖动的 3.当背包格子的装备拖动到装备栏时,如果是装备类型和装备栏类型是一致的能装上 4.背包的装备是按照顺序放在格子中的, ...

  3. Xilinx_ISE 14.7 Win10 闪退

    打开D:\Xilinx\14.7\ISE_DS\ISE\lib\nt64 将libPortabilityNOSH.dll 重命名为libPortability.dll,替换原libPortabilit ...

  4. VS 编译后 install报错(error MSB3073)

    vs编译出现如下错误: 错误 1 error MSB3073: 命令"setlocal H:\PCL_BACKUP\PCL\CMake\bin\cmake.exe -DBUILD_TYPE= ...

  5. 【Selenium2】【环境搭建】

    Windows7  64位 Mozilla Firefox 36.0.4 + Firebug 2.0.19 + FirePath 0.9.7.1.1-signed.1-signed 火狐历史版本:ht ...

  6. 前端如何应对笔试算法题?(用node编程)

    用nodeJs写算法题 咱们前端使用算法的地方不多,但是为了校招笔试,不得不针对算法题去练习呀! 好不容易下定决心 攻克算法题.发现js并不能像c语言一样自建输入输出流.只能回去学习c语言了吗?其实不 ...

  7. 无视编码都统一转成unicode 然后截断 例如 。“发发发发发发” 操作之后显示为 “发发发发...”

    -- local function checkPlayName( str ) -- str = Utils.utf8_to_unicode(str)-- local retStr = "&q ...

  8. linux查看历史操作记录并且显示执行时间

    vim  ~/.bashrc 或者 ~/.bash_profile 增加:export HISTTIMEFORMAT="%F %T  " 查看历史记录之前先执行: 然后使用hist ...

  9. 1. eclipse异常问题解决办法

    1. 内存溢出问题 问题描述:报错信息 java.lang.OutOfMemoryError: PermGen space 解决办法: 在Tomcat/bin/catalina.bat 文件下加入: ...

  10. hibernate框架模板(可复制修改)

    简易搭建jar包 User类 package com.littlepage.test; public class User { private int uid; private String unam ...