hdu.5211.Mutiple(数学推导 && 在logn的时间内求一个数的所有因子)
Mutiple
wld有一个序列a[1..n], 对于每个1≤i<n, 他希望你求出一个最小的j(以后用记号F(i)表示),满足i<j≤n, 使aj为ai的倍数(即aj mod ai=0),若不存在这样的j,那么此时令F(i) = 0
保证1≤n≤10000,1≤ai≤10000 对于任意 1≤i≤n, 且对于任意1≤i,j≤n(i!=j),满足ai != aj
多组数据(最多10组)
对于每组数据:
第一行:一个数n表示数的个数
接下来一行:n个数,依次为a1,a2,…,an
对于每组数据:
输出F(i)之和(对于1≤i<n)
4
1 3 2 4
6
F(1)=2
F(2)=0
F(3)=4
F(4)=0
#include<stdio.h>
#include<string.h>
#include<vector>
const int M = 1e4 + ;
std::vector <int> g[M] ;
int f[M] ;
int a[M] ;
int n ; void init ()
{
for (int i = ; i <= ; i ++) {
for (int j = i ; j <= ; j += i ) {
g[j].push_back (i) ;
}
}
} int main ()
{
// freopen ("a.txt" , "r" , stdin ) ;
init () ;
while (~ scanf ("%d" , &n)) {
for (int i = ; i <= n ; i ++) scanf ("%d" , &a[i]) ;
memset (f , , sizeof(f)) ;
int sum = ;
for (int i = n; i > ; i --) {
sum += f[a[i]] ;
// printf ("a[%d]=%d , f = %d\n" , i , a[i] , f[a[i]]) ;
for (int j = ; j < g[a[i]].size () ; j ++) {
f[ g[a[i]][j] ]= i ;
}
}
printf ("%d\n" , sum ) ;
}
return ;
}
杰神教的打法,其实是令求1 , 2 , ……n的每个数的因子复杂度降到O(n/1 + n/2 + n/3 ……n/n) = O(nlogn) ,所以平均下来就是O(logn)了。
hdu.5211.Mutiple(数学推导 && 在logn的时间内求一个数的所有因子)的更多相关文章
- hdu 5211 Mutiple 数学
Mutiple Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5211 ...
- HDU 5211 Mutiple 水题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5211 题解: 1.筛法: #include<iostream> #include< ...
- HDU 5734 Acperience(数学推导)
Problem Description Deep neural networks (DNN) have shown significant improvements in several applic ...
- HDU 5984 题解 数学推导 期望
Let’s talking about something of eating a pocky. Here is a Decorer Pocky, with colorful decorative s ...
- HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)
Galaxy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total S ...
- HDU-1719 Friend 数学推导
Friend HDU - 1719 Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend n ...
- 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5
上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...
- 关于不同进制数之间转换的数学推导【Written By KillerLegend】
关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...
- UVA - 10014 - Simple calculations (经典的数学推导题!!)
UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...
随机推荐
- ExceptionLess异常日志收集框架-1
哈哈,中秋和代码更配哦,不知不觉一年过半了,祝园友们中秋快乐 前一阵子在博客园看到了一篇博文 http://www.cnblogs.com/savorboard/p/exceptionless.htm ...
- RESTful的理解
REST(Representational State Transfer ),有中文翻译为"具象状态传输"(也有:"代表性状态传输").是由 Roy Thoma ...
- [Android] View.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)
转自: http://blog.csdn.net/brokge/article/details/8536906 setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息 ...
- Visual Studio原生开发的20条调试技巧(下)
我的上篇文章<Vistual Studio原生开发的10个调试技巧>引发了很多人的兴趣,所以我决定跟大家分享更多的调试技巧.接下来你又能看到一些对于原生应用程序的很有帮助的调试技巧(接着上 ...
- Raspberry Pi 3 FAQ --- connect automatically to 'mirrors.zju.edu.cn' when downloading and how to accelerate download
modify the software source: The software source is a place where several free application for linux ...
- 前端必备:FastStoneCapture 和 Licecap
前端必备:FastStoneCapture 和 Licecap FastStoneCapture这个软件非常小,只有2M多,并且其功能很强大,包括截图,录制视频,量尺,取色等等,对于前端工程师绝对是必 ...
- python调用ggsci.exe程序
需求:通过python调用windows server 2008下的ogg同步程序,实现图形化控制. 简单GUI
- window.location.href的用法
在写ASP.Net程序的时候,我们经常遇到跳转页面的问题,我们经常使用Response.Redirect 做ASP.NET框架页跳转,如果客户要在跳转的时候使用提示,这个就不灵光了,如: Respon ...
- join的理解
thread.Join把指定的线程加入到当前线程,可以将两个交替执行的线程合并为顺序执行的线程.比如在线程B中调用了线程A的Join()方法,直到线程A执行完毕后,才会继续执行线程B. t.join( ...
- css006 文本格式化
css006 文本格式化 文本格式化:字体(font-family).颜色(color).字号(font-size). 行距(line-height).粗体(font-weight).斜体(font- ...