HDU 5171 GTY & #39; s birthday gift matrix fast power, hdu5171

Source: Internet
Author: User

HDU 5171 GTY's birthday gift matrix fast power, hdu5171
Click Open Link
GTY's birthday giftTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 225 Accepted Submission (s): 78


Problem DescriptionFFZ's birthday is coming. GTY wants to give a gift to ZZF. he asked his gay friends what he shoshould 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, B 'S ), 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.
InputMulti test cases (about 3). The first line contains two integers n and k ( 2 ≤ n ≤ 100000,1 ≤ k ≤ 1000000000 ). The second line contains n elements Ai ( 1 ≤ ai ≤ 100000 ) Separated by spaces, indicating the multiset S.
OutputFor each case, print the maximum sum of the multiset ( Mod 10000007 ).
Sample Input

3 23 6 2
 
Sample Output
35
 
SourceBestCoder Round #29
Select the sum of two numbers from the n number, and then add the n number. Then, perform k operations to obtain the sum of the n number and the sum of the plus number. Select a from the given array, and the maximum number is B. To maximize the final answer, the following numbers are a + B, respectively, a + 2 * B, 2 * a + 3 * c, 3 * a + 5 * c, 5 * a + 8 * c The coefficients of a are respectively 1 1 2 3 5 The coefficients of B are respectively 1 2 3 5 8 The coefficient of a is obviously a Fibonacci series, and the coefficient of B is also a Fibonacci series at the beginning. Therefore, we can use the rapid power of matrix to obtain the coefficient of a and B of nth item, however, the question is the sum of these numbers, rather than the number of the nth number. There is a formula for finding the first n of the Fibonacci series: S (n) = a (n + 2)-1. Therefore, we can obtain the nth + 2 Fibonacci series, and multiply them by a and B respectively, which is the answer. Note that the coefficient of B is supplemented by the first item 1, so the first item must be subtracted.
// 124MS1900K # include <stdio. h> # include <string. h> # include <algorithm> # include <math. h> # define mod 10000007 # define ll _ int64using namespace std; ll s [100007]; struct Matrax {ll m [2] [2];} a, per, tmp; void init () // create a matrix {. m [0] [0] = 1;. m [0] [1] = 1;. m [1] [0] = 1;. m [1] [1] = 0; per. m [0] [0] = 1; per. m [0] [1] = 0; per. m [1] [0] = 0; per. m [1] [1] = 1;} Matrax multi (Matrax a, Matrax B) // matrix multiplication {Matrax c; for (int I = 0; I <2; I ++) for (int j = 0; j <2; j ++) {c. m [I] [j] = 0; for (int k = 0; k <2; k ++) c. m [I] [j] = (c. m [I] [j] +. m [I] [k] * B. m [k] [j]) % mod; // % mod} return c;} Matrax power (ll k) missed during the competition) // matrix fast power {Matrax pp = a, ans = per; while (k) {if (k & 1) {ans = multi (ans, pp); k --;} else {k >>= 1; pp = multi (pp, pp) ;}} return ans ;}int main () {ll n, k; while (scanf ("% I64d % I64d", & n, & k )! = EOF) {init (); ll sum = 0, aa, bb, c; for (int I = 0; I <n; I ++) {scanf ("% I64d", & s [I]); sum = (sum + s [I]) % mod;} sort (s, s + n ); aa = s [N-2]; bb = s [n-1]; Matrax ans1 = power (k + 2 ); // calculate the nth + 2 Fibonacci series of the coefficient Matrax ans2 = power (k + 3); // obtain the nth + 2 Fibonacci series of the B coefficient, because 1 is added, k + 3 ll f = (ans1.m [0] [1]-1 + mod) % mod; ll e = (ans2.m [0] [1]-2 + mod) % mod; // Add 1 sum = (sum + f * aa) % mod; sum = (sum + e * bb) % mod; printf ("% I64d \ n", sum);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.