Boring Sum http://acm.hdu.edu.cn/showproblem.php?pid=4961

这个二分也过了,on更快些。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<vector>
 4 #define mt(a,b) memset(a,b,sizeof(a))
 5 using namespace std;
 6 typedef __int64 LL;
 7 const int M=100010;
 8 int a[M];
 9 LL b[M],c[M];
10 int val[M];
11 vector<int> yueshu[M];
12 int main(){
13     for(int i=1;i<=100000;i++){
14         yueshu[i].clear();
15         for(int j=1;j*j<=i;j++){
16             if(!(i%j)){
17                 yueshu[i].push_back(j);
18                 if(i/j!=j)
19                     yueshu[i].push_back(i/j);
20             }
21         }
22     }
23     int n;
24     while(~scanf("%d",&n),n){
25         for(int i=0;i<n;i++){
26             scanf("%d",&a[i]);
27         }
28         mt(val,0);
29         for(int i=n-1;i>=0;i--){
30             int now=a[i];
31             if(!val[now]){
32                 b[i]=now;
33             }
34             else{
35                 b[i]=val[now];
36             }
37             int len=yueshu[now].size();
38             for(int j=0;j<len;j++){
39                 int yue=yueshu[now][j];
40                 val[yue]=now;
41             }
42         }
43         mt(val,0);
44         for(int i=0;i<n;i++){
45             int now=a[i];
46             if(!val[now]){
47                 c[i]=now;
48             }
49             else{
50                 c[i]=val[now];
51             }
52             int len=yueshu[now].size();
53             for(int j=0;j<len;j++){
54                 int yue=yueshu[now][j];
55                 val[yue]=now;
56             }
57         }
58         LL ans=0;
59         for(int i=0;i<n;i++){
60             ans+=b[i]*c[i];
61         }
62         printf("%I64d\n",ans);
63     }
64     return 0;
65 }
View Code

相关文章: