1002 GTY's birthday gift

GTY's birthday gift

                                                                      Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                    Total Submission(s): 209    Accepted Submission(s): 71


Problem Description
FFZ's birthday is coming. GTY wants to give a gift to ZZF. He asked his gay friends what he should give to ZZF. One of them said, 'Nothing is more interesting than a number multiset.' So GTY decided to make a multiset for ZZF. Multiset can contain elements with same values. Because GTY wants to finish the gift as soon as possible, he will use JURUO magic. It allows him to choose two numbers a and b(a,bS), and add a+b to the multiset. GTY can use the magic for k times, and he wants the sum of the multiset is maximum, because the larger the sum is, the happier FFZ will be. You need to help him calculate the maximum sum of the multiset.
 


Input
Multi test cases (about 3) . The first line contains two integers n and k (2n100000,1k1000000000). The second line contains n elements ai (1ai100000)separated by spaces , indicating the multiset S .
 


Output
For each case , print the maximum sum of the multiset (mod 10000007).
 


Sample Input
3 2
3 6 2
 


Sample Output
35
 
100111110Sn1Fn1Fn2=SnFnFn−1⎤⎦⎥
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <cstdlib>
 7 #include <string>
 8 #include <vector>
 9 #include <set>
10 #include <map>
11 #include <stack>
12 #include <queue>
13 using namespace std;
14 const int INF = 0x7fffffff;
15 const int MS = 100005;
16 const double EXP = 1e-8;
17 const int mod = 10000007;
18 typedef long long LL;
19 int a[MS];
20 int n, k;
21 struct Matrix
22 {
23     LL matrix[3][3];
24     Matrix()
25     {
26         memset(matrix, 0, sizeof(matrix));
27     }
28     Matrix operator *(const Matrix &b)const
29     {
30         Matrix x;
31         for (int i = 0; i < 3; i++)
32             for (int j = 0; j < 3; j++)
33                 for (int k = 0; k < 3; k++)
34                     x.matrix[i][j] = ((x.matrix[i][j] + matrix[i][k] * b.matrix[k][j]) % mod) % mod;
35         return x;
36     }
37 };
38 void solve()
39 {
40     sort(a, a + n);
41     LL sum = 0;
42     LL s, t;
43     for (int i = 0; i < n; i++)
44         sum += a[i];
45     s = static_cast<LL>(a[n - 2]);
46     t = static_cast<LL>(a[n - 1]);
47     Matrix m1, m2, ans;
48     m1.matrix[0][1] = m1.matrix[0][0] = m1.matrix[0][2] = 1;
49     m1.matrix[1][1] = m1.matrix[1][2] = 1;
50     m1.matrix[2][1] = 1;
51     ans.matrix[0][0] = ans.matrix[1][1] = ans.matrix[2][2] = 1;
52     while (k)
53     {
54         if (k & 1)
55             ans = ans*m1;
56         k >>= 1;
57         m1 = m1*m1;
58     }
59 
60     m2.matrix[0][0] = sum;
61     m2.matrix[1][0] = t;
62     m2.matrix[2][0] = s;
63     ans = ans*m2;
64     cout << ans.matrix[0][0] % mod << endl;
65     return;
66 }
67 int main()
68 {
69     while (cin >> n >> k)
70     {
71         for (int i = 0; i < n; i++)
72             cin >> a[i];
73         solve();
74     }
75     return 0;
76 }

 

 
 

转载于:https://www.cnblogs.com/767355675hutaishi/p/4280744.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值