The collection of bzoj4547 kit

Source: Internet
Author: User
Tags mul

Description

There is a re-set of size n, kit each operation can be joined a number of a+b (A, a, b all belong to s), after K operations it can obtain the maximum of S and

values. (The data guarantees that the value is non-negative)Input

The first line has two integers n,k represents the initial number of elements and operands, and the second row contains n integers representing the elements that can be re-set initially.

for 100% of data, there are n<=10^5,k<=10^9,|ai|<=10^5Outputoutputs an integer that represents the maximum value. Answer to 10000007 modulo.

Add up to two maximum values in a re-set each time you should take a

If the maximum value is non-negative, the sum of the new number of K operations can be obtained by Fibonacci sequence.

If the maximum value and the second large value of a positive and negative, each time the maximum and sub-large value is added to the K-operation or the second-largest non-negative with the Fibonacci sequence to find the remaining operation.

N

Σfi = fn+2-1

I=1

#include <cstdio>#include<algorithm>intn,k;Const intp=10000007;intv[100005];typedefintmat[2];mat M1,m2;inlineintinput () {intx=0, C=getchar (), f=1;  while(c> $|| c< -){        if(c=='-') f=-1; C=GetChar (); }     while(c> -&&c< -) x=x*Ten+c- -, c=GetChar (); returnx*F;} InlinevoidMul (Mat A,mat b) {intC= (1ll* (a[1]-a[0]) *b[0]+1ll*a[0]*b[1])%Q; intD= (1ll*a[0]*b[0]+1ll*a[1]*b[1])%P; a[0]=c;a[1]=D;} InlinevoidFibintx) {x+=1; m1[0]=0; m1[1]=1; m2[0]=m2[1]=1;  while(x) {if(x&1) Mul (M1,M2);        Mul (M2,M2); X>>=1; }}intans=0;intMain () {n=input (); k=input ();  for(intI=0; i<n;i++) v[i]=input (); Std::nth_element (V,v+n-2, v+N);  for(intI=0; i<n;i++) {Ans+=V[i]; if(ans>=p) ans-=Q; if(ans<p) ans+=P; }    intv1=v[n-1],v2=v[n-2]; if(v1<=0) {Ans= (ans+ (v1+v2) *1ll*k)%Q; if(ans<0) ans+=Q; printf ("%d", Ans); }Else if(v2<0){         while(v2<0&&k) {v2+=v1; Ans+=v2; if(ans>=p) ans-=Q; if(ans<p) ans+=P; --K; }        if(V2&GT;V1) v1^=v2,v2^=v1,v1^=v2; if(k) {fib (k); intAns= ((m1[1]-1) *1ll*v2+ (m1[1]+m1[0]-2) *1ll*v1)%Q; Ans= (Ans+ans)%P; }        if(ans<0) ans+=Q; printf ("%d", Ans); }Else{fib (k); intAns= ((m1[1]-1) *1ll*v2+ (m1[1]+m1[0]-2) *1ll*v1)%P; Ans= (Ans+ans)%P; if(ans<0) ans+=Q; printf ("%d", Ans); }    return 0;}

The collection of bzoj4547 kit

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.