BestCoder Round #8

Summary

 1 /*
 2 ID:esxgx1
 3 LANG:C++
 4 PROG:bestcoder8_A
 5 */
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 
12 int a[107];
13 long long b[107*107];
14 
15 int main(void)
16 {
17     #ifndef ONLINE_JUDGE
18     freopen("in.txt", "r", stdin);
19     #endif
20 
21     int N;
22     while(scanf("%d", &N) > 0) {
23         for(int i=0; i<N; ++i) scanf("%I64d", &a[i]);
24         int k = 0;
25         for(int i=0; i<N; ++i) {
26             for(int j=i+1; j<N; ++j) {
27                 b[k++] = a[i] + a[j];
28             }
29         }
30         sort(b, b+k);
31         int j = unique(b, b+k) - b;
32         long long S = 0;
33         for(int i=0; i<j; ++i) {
34             S += b[i];
35         }
36         printf("%I64d\n", S);
37     }
38     return 0;
39 }

 

Reading comprehension

 1 /*
 2 ID:esxgx1
 3 LANG:C++
 4 PROG:bestcoder8_B
 5 */
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <iostream>
 9 #include <algorithm>
10 using namespace std;
11 
12 int M;
13 
14 const int n = 2;
15 struct mat {
16     int at[n][n];
17 };
18 
19 #define mod M
20 mat m22_m(const mat &a,const mat &b)
21 {
22     mat t;
23     for(int i=0;i<n;++i) {
24         for(int k=0;k<n;++k) {
25             long long u = 0;
26             for(int j=0;j<n;++j)
27                 u += (long long)a.at[i][j] * b.at[j][k];
28             t.at[i][k] = u % mod;
29         }
30     }
31     return t;
32 }
33 
34 mat expo(mat p,int k)
35 {
36     mat e;
37     memset(e.at,0,sizeof(e.at));
38     for(int i=0; i<n; ++i) e.at[i][i]=1;
39     for(;k; k>>=1) {
40         if(k & 1) e = m22_m(e, p);
41         p = m22_m(p,p);
42     }
43     return e;
44 }
45 
46 inline void DEC(int &a, int b){a -= b; if (a < 0) a += M;}
47 
48 int main(void)
49 {
50     #ifndef ONLINE_JUDGE
51     freopen("in.txt", "r", stdin);
52     #endif
53 
54     int N;
55     while(scanf("%d%d", &N, &M)>=0) {
56         mat p;
57         p.at[0][0] = 1, p.at[0][1] = 2;
58         p.at[1][0] = 1, p.at[1][1] = 0;
59         int rslt = expo(p, N).at[0][0];
60         if (!(N&1)) DEC(rslt, 1);
61         printf("%d\n", rslt);
62     }
63     return 0;
64 }

 

posted @ 2014-09-07 23:49  e0e1e  阅读(207)  评论(0编辑  收藏  举报