暴力好像也可以过,不过正解应该是用类似筛法的思想来做。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int INF = 9999999; 7 const int N = 10001; 8 int a[N]; 9 int mp[N]; 10 11 int main() 12 { 13 int n; 14 while ( scanf("%d", &n) != EOF ) 15 { 16 memset( mp, -1, sizeof(mp) ); 17 int maxn = -1; 18 for ( int i = 1; i <= n; i++ ) 19 { 20 scanf("%d", a + i); 21 if ( a[i] > maxn ) maxn = a[i]; 22 mp[a[i]] = i; 23 } 24 int ans = 0; 25 for ( int i = 1; i <= n; i++ ) 26 { 27 int f = INF; 28 for ( int j = a[i] + a[i]; j <= maxn; j += a[i] ) 29 { 30 if ( mp[j] > i ) 31 { 32 if ( mp[j] < f ) f = mp[j]; 33 } 34 } 35 if ( f == INF ) f = 0; 36 ans += f; 37 } 38 printf("%d ", ans); 39 } 40 return 0; 41 }