The Preliminary Contest for ICPC Asia Shenyang 2019 K. Guanguan's Happy water(高斯消元)

 

 

K. Guanguan's Happy water

 

Pressing problem solution fought Code: Gaussian elimination and matrix multiplication ... because for a long time did not do Gaussian elimination of the problem of thinking about the way to doing

And then did not reflect on it and then you do first and then find a cow T customer group, said the question of interpretation time complexity is 1e9 .....

 

#include<bits/stdc++.h>
#define debug printf("!");
using namespace std;
const int maxn=1e5+50;
const int mod=1e9+7;
typedef long long ll;
ll a[210],f[210];


ll kpow(ll a,ll b)
{
    ll ans=1,base=a;
    while(b)
    {
        if(b&1)ans=ans*base%mod;
        base=Base * Base % MOD; 
        B >> = . 1 ; 
    } 
    return ANS; 
} 

/// Gaussian elimination template 
const  int Max_M = 75 ;        /// m equations, n variables 
const  int max_n = 75 ; 
LL m, n ; 
LL-Aug [Max_M] [max_n + . 1 ]; /// augmented matrix 
BOOL free_x [max_n];          /// determine whether it is uncertain argument 
LL X [max_n];             /// solution set 

/ * * 
returns value: 
None Solutions -1 
0 YES and only one solution
> = 1 with a plurality of solutions, based on the solution which is uncertain free_x Analyzing 
* / 
int GAUSS () 
{ 
    int I, J;
     int Row, COL, max_r;
     for (Row = 0 , COL = 0 ; Row <m && COL < n-; row ++, COL ++ ) 
    { 
        max_r = row;
         for (I = row + . 1 ; I <m; I ++)   /// maximum is found in all the lines of the current column (reducing errors do division) 
        {
             IF (FABS (-Aug [I] [COL]) - FABS (-Aug [max_r] [COL])> 0 ) 
            max_r = I; 
        } 
        IF ! (max_r = row)             /// the row line switching current
        {
            for(j=row;j<n+1;j++)
                swap(Aug[max_r][j],Aug[row][j]);
        }
        if(Aug[row][col]==0)  ///当前列row行以下全为0(包括row行)
        {
            row--;
            continue;
        }
        for(i=row+1;i<m;i++)
        {
            if(Aug[i][col]==0)continue;
            int ta=Aug[i][col]*kpow(Aug[row][col],mod-2)%mod;
            for(J = COL; J <n-+ . 1 ; J ++ ) 
                -Aug [I] [J] = (-Aug [I] [J] -Aug [Row] [J] * TA% MOD + MOD)% MOD; 
        } 
    } 
    / * 
    kkkek: 
    the last of the resulting augmented matrix: 
    for (I = 0; I <n-; I ++) 
    { 
        for (J = 0; J <= n-; J ++) the printf ( "% LLD",-Aug [I] [J] ); 
        the printf ( "\ n-"); 
    } 
    * / 
    for (I = Row; I <m; I ++)     /// COL = 0 ... n-present case 0, a, and no solution 
    {
         IF (-Aug [I ] [COL]) return - . 1 ; 
        } 
    IF (Row <n-)      /// presence of 0,0 ... 0, a plurality of solution, consisting of variable number of n-row element is a 
    {
        for (I = row- . 1 ; I> = 0 ; i-- ) 
        { 
            int free_num = 0 ;    /// free number Arguments 
            int free_index;      /// free argument number 
            for (J = 0 ; J <n-; J ++ ) 
            { 
                IF ! (-Aug [I] [J] = 0 && free_x [J]) 
                    free_num ++, free_index = J; 
            } 
            IF (free_num> . 1 ) Continue ;   /// the row uncertain the number of arguments over one, can not be solved, they still uncertain variables
             ///Only an uncertain argument free_index, can be solved the arguments, and the argument is determined 
            int tmp = -Aug [I] [n-];
             for (J = 0 ; J <n-; J ++ ) 
            { 
                IF (-Aug ! [I] [J] = 0 ! && J = free_index) 
                    tmp = (tmp--Aug [I] [J] * X [J]% MOD + MOD)% MOD; 
            } 
            X [free_index] = tmp * kpow (-Aug [I] [free_index], mod- 2 )% MOD; 
            free_x [free_index] = to false ; 
        } 
        return N- Row; 
    } 
    /// there is only one solution, strict upper triangular matrix (== n-m) 
    for (i = n-. 1 ; I> = 0 ; i-- ) 
    { 
        int tmp = -Aug [I] [n-];
         for (J = I + . 1 ; J <n-; J ++ )
             IF ! (-Aug [I] [J] = 0 ) 
                tmp = (-Aug-tmp [I] [J] * X [J] + MOD% MOD)% MOD; 
        X [I] = tmp * kpow (-Aug [I] [I], mod- 2 )% MOD; 
    } 
    return  0 ; 
} 
/// template end 

LL S [ 210 ] [ 210 ]; 

// matrix multiplication 
const  int N = 210 ;
ll tmp[N][N];
void multi(ll aa[][N],ll b[][N],ll n)
{
    memset(tmp,0,sizeof tmp);
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        for(int k=0;k<n;k++)
        tmp[i][j]=(tmp[i][j]+aa[i][k]*b[k][j]%mod)%mod;
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
        aa[i][j]=tmp[i][j];
}
ll res[N][N];
void Pow(ll aa[][N],ll b,ll n)
{
    memset(res,0,sizeof res);
    for(int i=0;i<n;i++) res[i][i]=1;
    while(b)
    {
        if(b&1)
            multi(res,aa,n);//res=res*a;复制直接在multi里面实现了;
        multi(aa,aa,n);//a=a*a
        b>>=1;
    }
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            aa[i][j]=res[i][j]%mod;
}
//


ll A[210][210]; 

 

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(Aug,0,sizeof(Aug));
        memset(x,0,sizeof(x));
        memset(free_x,0,sizeof(free_x));
        ll k,N,i,j,ans=0;
        scanf("%lld%lld",&k,&N);
        n=m=k;
        for(i=1;i<=k;i++)
        {
            scanf("%lld",&a[i]);
            f[i]=a[i];
            ans=(ans+a[i])%mod;
        }
        for(i=k+1;i<=2*k;i++)
            scanf("%lld",&f[i]);
        for(i=k;i<2*k;i++)
        {
            Aug[k-i][k]=f[i+1];
            for(j=i;j>i-k;j--)
            {
                Aug[k-i][i-j]=f[j];
            }
        }
        Gauss();
        
        
//        printf("**P:");for(i=0;i<k;i++)printf("%lld ",x[i]);printf("\n");
        
        
        memset(S,0,sizeof(S));
        for(i=0;i<k;i++)S[0][i]=x[i];
        for(i=0;i<k;i++)
        {
            if(i)S[i][i-1]=1;
            for(j=0;j<k;j++)
                S[i][i+k]=1;
                S[i+k][i+k]=1;
        }
        Pow(S,N-k+1,k*2);
        
        
//        printf("##\n");
//        for(i=0;i<2*k;i++)
//        {
//            for(j=0;j<2*k;j++)
//                printf("%lld ",S[i][j]);
//            printf("\n");
//        }printf("##\n");
        
        
        for(i=0;i<k;i++)
        {
            for(j=0;j<k;j++)
            {
                A[i][j]=S[i][j+k];
                if(i==j)A[i][j]--;
//                printf("%lld ",A[i][j]);
            }
//            printf("\n");
        }//A+A^2+……+A^N-k 
        
        for(i=0;i<k;i++)
        {
            ans=(ans+A[0][i]*f[k-i]%mod)%mod;
        }
        printf("%lld\n",ans);
    }
}

 

 

Then after I T, I went to see the cattle customer base. I see someone say: i greater than kf [i] not going to change.

And then I would be able to understand why so many people are so troublesome problem too.

 

Hey....

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug printf("!");
const int mod=1e9+7;
const int maxn=1e3+50;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ll k,n,ans=0,i,t,r;
        scanf("%lld%lld",&k,&n);
        for(i=1;i<=k;i++)
        {
            scanf("%lld\n",&t);
            ans=(ans+t)%mod;
        }
        r=t;
        for(i=1;i<=k;i++)scanf("%d",&t);
        ans=(ans+(n-k)%mod*r)%mod;
        printf("%lld\n",ans);
    }
}

 

 

Hey....

 

Guess you like

Origin www.cnblogs.com/kkkek/p/11520413.html