hdu.5211.Mutiple(数学推导 && 在logn的时间内求一个数的所有因子)

Mutiple

Accepts: 476

Submissions: 1025

Time Limit: 4000/2000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

问题描述

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

Hint

F(1)=2
F(2)=0
F(3)=4
F(4)=0

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<vector>
 4 const int M = 1e4 + 10 ;
 5 std::vector <int> g[M] ;
 6 int f[M] ;
 7 int a[M] ;
 8 int n ;
 9
10 void init ()
11 {
12     for (int i = 1 ; i <= 10000 ; i ++) {
13         for (int j = i ; j <= 10000 ; j += i ) {
14             g[j].push_back (i) ;
15         }
16     }
17 }
18
19 int main ()
20 {
21    // freopen ("a.txt" , "r" , stdin ) ;
22     init () ;
23     while (~ scanf ("%d" , &n)) {
24         for (int i = 1 ; i <= n ; i ++) scanf ("%d" , &a[i]) ;
25         memset (f , 0 , sizeof(f)) ;
26         int sum = 0 ;
27         for (int i = n; i > 0 ; i --) {
28             sum += f[a[i]] ;
29            // printf ("a[%d]=%d , f = %d\n" , i , a[i] , f[a[i]]) ;
30             for (int j = 0 ; j < g[a[i]].size () ; j ++) {
31                 f[  g[a[i]][j]  ]=  i ;
32             }
33         }
34         printf ("%d\n" , sum ) ;
35     }
36     return 0 ;
37 }

杰神教的打法,其实是令求1 , 2 , ……n的每个数的因子复杂度降到O(n/1 + n/2 + n/3 ……n/n) = O(nlogn) ,所以平均下来就是O(logn)了。

时间: 2024-11-06 11:45:50

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 Description 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 对于任意

HDU 5211 Mutiple 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5211 题解: 1.筛法: 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 const int maxn=10000+10; 9 const i

hdu 1719 Friend 数学推导

题链:http://acm.hdu.edu.cn/showproblem.php?pid=1719 Friend Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2099    Accepted Submission(s): 1058 Problem Description Friend number are defined recur

hdu 5211 Mutiple(枚举)

题意: 思路: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int a[10000+100]; int vis[10000+100]; int main() { int n; int i,j,k; while(scanf("%d",&n)!=EOF) { int ans=0; memse

HDU 5734 Acperience(数学推导)

Problem Description Deep neural networks (DNN) have shown significant improvements in several application domains including computer vision and speech recognition. In computer vision, a particular type of DNN, known as Convolutional Neural Networks (

HDU - 5210 - Delete &amp;&amp; 5211 - Mutiple (BestCoder Round #39)

题目传送:HDU - 5210 HDU - 5210 - Delete 思路:简单题 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include &l

HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 556    Accepted Submission(s): 127 Special Judge Problem Description Good news for us: to release the financial pressure, the government

HDU 5073 Galaxy(Anshan 2014)(数学推导,贪心)

Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 556    Accepted Submission(s): 127 Special Judge Problem Description Good news for us: to release the financial pressure, the government

HDU1719 Friend (数学推导)

friend numbers = 2^x + 3^y -1 1 #include<stdio.h> 2 int main() 3 { 4 __int64 a; 5 while(scanf("%I64d",&a)!=EOF) 6 { 7 if(!a) 8 { 9 printf("NO!\n"); 10 continue; 11 } 12 a+=1; 13 while(a%2==0||a%3==0) 14 { 15 if(a%2==0) a/=2;