hdu5608:function
$n^2-3n+2=\sum_{d|i}f(i)$,问$f(i)$前$n$项和。
方法一:直接切入!
$S(n)=\sum_{i=1}^{n}f(i)=\sum_{i=1}^{n}(i^2-3i+2-\sum_{d|i,d<i}f(d))=\sum_{i=1}^{n}(i^2-3i+2)-\sum_{i=1}^{n}\sum_{d|i,d<i}f(d)=\frac{n(n+1)(2n+1)}{6}-\frac{3n(n+1)}{2}+2n-\sum_{k=2}^{n}\sum_{d=1}^{\left \lfloor \frac{n}{k} \right \rfloor}f(d)$
且不管他是不是什么积性函数了,符合杜教筛形式直接算!!
方法二:仔细乱搞
这一看就是反演形式,那就令$g(n)=n^2-3n+2$,反演得
$f(n)=\sum_{d|n}\mu (d)g(\frac{n}{d})=\sum_{d|n}\mu(d)(\frac{n}{d})^2-3\sum_{d|n}\mu(d)\frac{n}{d}+2\sum_{d|n}\mu(d)=h(n)-3\varphi (n)+2[n=1]$
现在需要知道这三坨东西的前缀和。其中欧拉函数之前写过了,[n=1]的话。。然后就剩个$h(n)$也就是$\sum_{d|n}\mu(d)(\frac{n}{d})^2$。
$\sum_{i=1}^{n}\sum_{d|i}\mu (d)(\frac{i}{d})^2=\sum_{k=1}^{n}k^2\sum_{d=1}^{\left \lfloor \frac{n}{k} \right \rfloor}\mu (d)$。
莫比乌斯的前缀和就可以杜教筛啦!!
方法一是便捷的方式,但方法二是常用的套路,各有千秋。代码方法一。
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
//#include<assert.h>
#include<algorithm>
//#include<iostream>
//#include<bitset>
using namespace std; int T,n,pr;
#define maxn 1000011
#define LL long long
const int mod=1e9+;
LL s[maxn],f[maxn];
void pre(int n)
{
for (int i=;i<=n;i++)
{
f[i]+=1ll*i*i-*i+;
for (int j=i+i;j<=n;j+=i) f[j]-=f[i];
}
for (int i=;i<=n;i++) s[i]=((s[i-]+f[i])%mod+mod)%mod;
} struct Edge{int to,v,next;};
#define maxh 1000007
struct Hash
{
int first[maxh],le; Edge edge[maxn];
Hash() {le=;}
void insert(int y,int v)
{int x=y%maxh; Edge &e=edge[le]; e.to=y; e.v=v; e.next=first[x]; first[x]=le++;}
int find(int y)
{int x=y%maxh; for (int i=first[x];i;i=edge[i].next) if (edge[i].to==y) return edge[i].v; return -;}
}h; int six=(mod+)/,two=(mod+)>>;
int calc(int n)
{
if (n<=pr) return s[n];
int tmp=h.find(n); if (tmp!=-) return tmp;
int tot=;
for (int i=,last;i<=n;i=last+)
{
last=n/(n/i);
tot+=(last-i+)*1ll*calc(n/i)%mod;
tot-=tot>=mod?mod:;
}
int ans=((six*1ll*n%mod*(n+)%mod*(*n+)%mod-two*3ll*n%mod*(n+)%mod+*n)%mod+mod)%mod;
ans=(ans-tot+mod)%mod;
h.insert(n,ans);
return ans;
} int main()
{
pre(pr=);
scanf("%d",&T);
while (T--) scanf("%d",&n),printf("%d\n",calc(n));
return ;
}
hdu5608:function的更多相关文章
- 原生JS:Function对象(apply、call、bind)详解
Function对象(apply.call.bind) 原创文章,转摘请注明出处:苏福:http://www.cnblogs.com/susufufu/p/5850180.html 本文参考MDN做的 ...
- ES6新特性:Function函数扩展, 扩展到看不懂
本文所有Demo的运行环境为nodeJS, 参考:让nodeJS支持ES6的词法----babel的安装和使用 : 函数的默认值: 如果有参数 ,那就用参数, 如果没有参数, 那就用默认的参数: aj ...
- 为什么这样写js:(function ($) { })(jQuery);
很多时候,会这样写js,比如公司的项目里面的js页面都这样,所以我就想搞清楚意思: <script language="javascript" type="text ...
- 一起Polyfill系列:Function.prototype.bind的四个阶段
昨天边参考es5-shim边自己实现Function.prototype.bind,发现有不少以前忽视了的地方,这里就作为一个小总结吧. 一.Function.prototype.bind的作用 其实 ...
- HDU5875:Function
题目链接: Function 分析: icpccamp里的方法不会,我用了一个nex[]数组存储当前点ai需要取模的下一个点aj的编号j,如果aj>ai,就不用遍历. 时间为920ms 代码: ...
- 声明:function FileSetAttr ( const FileName : string
对文件和文件夹都有效 FileSetAttr('D:\Administrator\Desktop\patcher\Win32\Release\config\element\update',faHid ...
- JavaScript 再认识(一):Function调用模式对this的影响
近来,学习了一下<JavaScript精粹>,读到了函数这章,理清了JavaScript中this在不同调用模式下的指向. 1.Function调用模式:Function是JavaScri ...
- esnext:Function.prototype.toString 终于有规范了
从 ES1 到 ES5 的这 14 年时间里,Function.prototype.toString 的规范一字未变: An implementation-dependent representati ...
- 自执行匿名函数: (function() { /* code */ })();
1,常见格式:(function() { /* code */ })(); 2,解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括 ...
随机推荐
- 前端之HTML样式
<!doctype html> h5的文档声明 <html> 网页的根标签(根元素 html)--所有的代码都放置在此内 <head> <meta chars ...
- Elasticsearch (2) - 映射
常用映射类型 核心的字段类型如下: String 字符串包括text和keyword两种类型: 1.text analyzer 通过analyzer属性指定分词器. 下边指定name的字段类型为tex ...
- a=a+b与a+=b的区别
在一次工作中身边的一位资深的同事突然问了个a=a+b与a+=b有什么区别 此时有点尴尬了 不知道是真的不知道咧还是别有用意....今天抽点时间针对此问题做个小总结 一.性能方面 a=a+b是加法运算 ...
- 线程池ThreadPoolExecutor参数分析
概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...
- qt QTableView/QTableWidget样式设置
转载请注明出处:http://www.cnblogs.com/dachen408/p/7591409.html 选中设置: QTableView::item:selected { background ...
- easybcd 支持 windows 10 和 ubuntu 14.04 双系统启动
家里计算机系统 windows 10 全新安装. 原本是双系统的,还有一个ubuntu. windows 10 安装以后,恢复ubuntu就是问题了. (事后经验:请不要立刻安装bcd修改工具) 最初 ...
- RGB颜色空间、色调、饱和度、亮度,HSV颜色空间详解
本文章会详细的介绍RGB颜色空间与RGB三色中色调.饱和度.亮度之间的关系,最后会介绍HSV颜色空间! RGB颜色空间 概述 RGB颜色空间以R(Red:红).G(Green:绿).B(Blue:蓝) ...
- CPP-STL:随机数发生器random_shuffle
//--------------------------------------------------------------------------- #include <string.h& ...
- 在虚拟机linux环境下编译windows版adb fastboot
原文出自:http://blog.chinaunix.net/uid-20546441-id-1746200.html 我根据虚拟机编译遇到的问题进行一些添加 [前提条件] Linux Android ...
- merge dict key
#!/usr/local/python # -*- coding:utf-8 -*-user_dict = {'python': 23, 'Python': 51, '机器':10, 'PYTHON' ...