Hdu5171 --- GTY & amp; #39; s birthday gift

Source: Internet
Author: User

Hdu5171 --- GTY & #39; s birthday gift

Problem Description
FFZ'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.

Input
Multi 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.

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

Sample Input

3 2 3 6 2

Sample Output

35

Source
BestCoder Round #29

Recommend
Hujie | We have carefully selected several similar problems for you: 5173 5172 5169 5168 5165

Set the number of items a and B, and a> = B, then the number of items a + B,
There is a recursive relationship. After obtaining the recursive matrix, you can quickly solve the power problem. complexity O (logn)

/*************************************** * *********************************> File Name: hdu5171.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           
             # Include using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
            
              PLL; const int mod = 10000007; struct MARTIX {LL mat [3] [3] ;}; MARTIX mutiple (martix a, martix B) {MARTIX C; for (int I = 0; I <3; ++ I) {for (int j = 0; j <3; ++ j) {C. mat [I] [j] = 0; for (int k = 0; k <3; ++ k) {C. mat [I] [j] + =. mat [I] [k] * B. mat [k] [j]; C. mat [I] [j] % = mod ;}} return C;} void POW (LL a, LL B, LL s, int k) {MARTIX ret, ans, bse; ret. mat [0] [0] = ret. mat [0] [1] = ret. mat [0] [2] = 1; r Et. mat [1] [0] = ret. mat [2] [0] = ret. mat [2] [2] = 0; ret. mat [1] [1] = ret. mat [1] [2] = ret. mat [2] [1] = 1; for (int I = 0; I <3; ++ I) {for (int j = 0; j <3; ++ j) {ans. mat [I] [j] = (I = j? 1: 0); bse. mat [I] [j] = 0 ;}} bse. mat [0] [0] = s; bse. mat [1] [0] = a; bse. mat [2] [0] = B; while (k) {if (k & 1) {ans = mutiple (ans, ret);} k> = 1; ret = mutiple (ret, ret);} ans = mutiple (ans, bse); printf ("% lld \ n", ans. mat [0] [0]);} int main () {int n, k; while (~ Scanf ("% d", & n, & k) {LL m1 =-inf, m2 =-inf; LL s = 0; LL t; for (int I = 1; I <= n; ++ I) {scanf ("% lld", & t); s + = t; if (m1 <t) {m2 = m1; m1 = t;} else if (m2 <t) {m2 = t;} POW (m1, m2, s, k);} return 0 ;}
            
           
          
         
        
       
      
     
    
   

Related Article

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.