暴力+降复杂度 BestCoder Round #39 1002 Mutiple

 

题目传送门

 1 /*  2  设一个b[]来保存每一个a[]的质因数的id,从后往前每一次更新质因数的id,  3  若没有,默认加0,nlogn复杂度;  4  我用暴力竟然水过去了:)  5 */  6 #include <cstdio>  7 #include <iostream>  8 #include <cstring>  9 #include <string> 10 #include <algorithm> 11 using namespace std; 12 13 const int MAXN = 1e4 + 10; 14 const int INF = 0x3f3f3f3f; 15 int a[MAXN], b[MAXN]; 16 17 int main(void) //BestCoder Round #39 1002 Mutiple 18 { 19 //freopen ("1002.in", "r", stdin); 20 21 int n; 22 23 while (scanf ("%d", &n) == 1) 24  { 25 memset (b, 0, sizeof (b)); 26 for (int i=1; i<=n; ++i) scanf ("%d", &a[i]); 27 28 long long ans = 0; 29 for (int i=n; i>=1; --i) 30  { 31 ans += b[a[i]]; 32 for (int j=1; j*j<=a[i]; ++j) 33  { 34 if (a[i] % j == 0) 35  { 36 b[j] = i; b[a[i]/j] = i; 37  } 38  } 39  } 40 41 printf ("%lld\n", ans); 42  } 43 44 return 0; 45 }
 1 #include <cstdio>

 2 #include <iostream>

 3 #include <cstring>

 4 #include <string>

 5 #include <algorithm>

 6 using namespace std;  7 

 8 const int MAXN = 1e4 + 10;  9 const int INF = 0x3f3f3f3f; 10 int a[MAXN]; 11 

12 int main(void)        //BestCoder Round #39 1002 Mutiple

13 { 14     //freopen ("1002.in", "r", stdin);

15 

16     int n; 17     long long sum = 0; 18 

19     while (scanf ("%d", &n) == 1) 20  { 21         for (int i=1; i<=n; ++i)    scanf ("%d", &a[i]); 22 

23         sum = 0;    int k = 0; 24         for (int i=1; i<=n-1; ++i) 25  { 26             k = 0; 27             for (int j=i+1; j<=n; ++j) 28  { 29                 if (a[j] % a[i] == 0) 30  { 31                     k = j;    break; 32  } 33  } 34             sum += k; 35  } 36 

37         printf ("%lld\n", sum); 38  } 39 

40     return 0; 41 }
brute

 

你可能感兴趣的:(round)