HDU5942 : Just a Math Problem
\[\begin{eqnarray*}
ans&=&\sum_{i=1}^ng(i)\\
&=&\sum_{i=1}^n\sum_{d|i}\mu^2(d)\\
&=&\sum_{i=1}^n\sum_{d|i}\sum_{k^2|d}\mu(k)\\
&=&\sum_{k=1}^n\mu(k)\sum_{k^2|d}\lfloor\frac{n}{d}\rfloor\\
&=&\sum_{k=1}^n\mu(k)\sum_{i=1}^{\lfloor\frac{n}{k^2}\rfloor}\lfloor\frac{n}{k^2i}\rfloor\\
&=&\sum_{k=1}^{\sqrt{n}}\mu(k)S(\lfloor\frac{n}{k^2}\rfloor)
\end{eqnarray*}\]
其中
\[S(n)=\sum_{i=1}^n\lfloor\frac{n}{i}\rfloor\]
枚举所有$k$,然后分段计算$S$即可,当$n$比较小的时候可以记忆化$S(n)$。
时间复杂度
\[\begin{eqnarray*}
T(n)&=&O(\sqrt{n}+\sum_{i=1}^{\sqrt{n}}\sqrt{\frac{n}{i^2}})\\
&=&O(\sqrt{n}\sum_{i=1}^{\sqrt{n}}\frac{1}{i})\\
&=&O(\sqrt{n}\log n)
\end{eqnarray*}\]
#include<cstdio>
typedef long long ll;
const int N=1000010,P=1000000007;
int T,C,tot,p[N/10],i,j,ans,f[N];char mu[N],v[N];ll n;
inline int F(ll n){
if(n<N)if(f[n])return f[n];
ll t=0;
for(ll i=1,j;i<=n;i=j+1)j=n/(n/i),t+=n/i*(j-i+1);
t%=P;
if(n<N)f[n]=t;
return t;
}
int main(){
for(mu[1]=1,i=2;i<N;i++){
if(!v[i])mu[i]=-1,p[tot++]=i;
for(j=0;j<tot&&i*p[j]<N;j++){
v[i*p[j]]=1;
if(i%p[j])mu[i*p[j]]=-mu[i];else break;
}
}
for(scanf("%d",&T);T--;printf("Case #%d: %d\n",++C,(ans+P)%P)){
scanf("%I64d",&n);
for(ans=0,i=1;i<=n/i;i++)if(mu[i])ans=(ans+F(n/i/i)*mu[i])%P;
}
return 0;
}
HDU5942 : Just a Math Problem的更多相关文章
- hdu 1757 A Simple Math Problem (乘法矩阵)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU1757 A Simple Math Problem 矩阵快速幂
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 5615 Jam's math problem
Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...
- hdu----(5055)Bob and math problem(贪心)
Bob and math problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu------(1757)A Simple Math Problem(简单矩阵快速幂)
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- BestCoder Round #70 Jam's math problem(hdu 5615)
Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ...
- FZYZ-2071 A Simple Math Problem IX
P2071 -- A Simple Math Problem IX 时间限制:1000MS 内存限制:262144KB 状态:Accepted 标签: 数学问题-博弈论 ...
- Jam's math problem(思维)
Jam's math problem Submit Status Practice HDU 5615 Description Jam has a math problem. He just lea ...
- HDU 5055 Bob and math problem(结构体)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055 Problem Description Recently, Bob has been think ...
随机推荐
- Java都有什么进阶技术
Java都有什么进阶技术? 看到有人给题主推荐<代码整洁之道>,评论有人说那不是JAVA进阶的书- 私以为,一些人对JAVA进阶的理解片面了,JAVA不过也是一门语言,提升和进阶还是内 ...
- PHP5.6启动失败
PHP编译安装完毕,启动失败,提示 1 [23-Jun-2014 12:27:02] ERROR: failed to open configuration file '/usr/local/php/ ...
- win10 64位安装memcache扩展和开启redis扩展
前面有关于win10下搭建wamp环境的介绍,在此不在赘述,php操作memcache有memcache库和memcached库,其中memcache是php内置的扩展库,支持面向对象和面向过程两种操 ...
- VTK初学一,vtkDelaunay2D创建球冠曲面
#ifndef INITIAL_OPENGL #define INITIAL_OPENGL #include <vtkAutoInit.h> VTK_MODULE_INIT(vtkRend ...
- Coursera-Getting and Cleaning Data-Week2-课程笔记
Coursera-Getting and Cleaning Data-Week2 Saturday, January 17, 2015 课程概述 week2主要是介绍从各个来源读取数据.包括MySql ...
- Android高手速成--第一部分 个性化控件(View)
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
1.前言 使用框架都是较新的版本: Spring 4.0.2 RELEASE Spring MVC 4.0.2 RELEASE MyBatis 3.2.6 2.Maven引入需要的JAR包 2.1设置 ...
- .htaccess 基础教程(二)
今天要介绍的:.htaccess的第一个应用是自定义错误页面,这将使你可以拥有自己的.个性化的错误页面(例如找不到文件时),而不是你的服务商提供的错误页或没有任何页面.这会让你的网站在出错的时候看上去 ...
- 安卓APP关于切图标
bin res drawable-hdpi drawable-ldpi drawable-mdpi drawable-nodpi drawable-xhdpi drawable-xxhdpi x越大代 ...
- nginx + tomcat配置负载均衡
目标:Nginx做为HttpServer,连接多个tomcat应用实例,进行负载均衡. 注:本例程以一台机器为例子,即同一台机器上装一个nginx和2个Tomcat且安装了JDK1.7. 1.安装Ng ...