2019.8.1 Niuke Duo School 5th

index > Niuke Duo School Game 5


Question number title Passing rate practice status
A digits 2 1017/2383 Sign in √+
B generator 1 555/3660 Matrix fast power/decimal optimization
C generator 2 37/626
D generator 3 4/23
E independent set 1 45/110
F maximum click 1 93/838
G subsequence 1 522/2513 dp √-
H subsequence 2 286/1366
I three points 1 139/2701
J three points 2 7/76
  • Representative post-match supplement
  • √+ I passed on behalf of the game
  • √- It means I didn’t do it
  • √-○It means I didn’t do it in the match, so I made up

A - digits 2

It seems that our brains are not turning very fast, so it took a bit of effort to think of splicing.

B - generator 1

In the game, I thought inexplicably that matrix multiplication can also be reduced by Euler. Then wa from the beginning to the end.

It's a very clever idea.

The meaning of the question is to give a formula (obviously it is quickly exponentiated with a two-dimensional matrix). Then give you an extremely huge n ( 1 0 1000000 10^{1000000}101 0 0 0 0 0 0 ). But the number cannot be saved, and it is very troublesome to convert to binary.

So what to do? It can be done directly in decimal. Let’s review the binary fast exponentiation. When each bit of the binary is 1, multiply the base number after the exponent. Since there are only two choices for each bit in binary, it is relatively simple.

Decimal can also handle bases base = base 10 base = base^{10}base=base1 0 , but there are 9 choices for each bit of the binary, which is easy to handle, if this bit isxxx r e s = r e s × ( b a s e 10 ) x res =res \times (base^{10})^{x} res=res×(base10)x

The number of powers is reduced to 10 101 0 or less, and this power is necessary to take the fast power Binary look, this is a serious problem in this little card. But this also avoids the trouble of global conversion to binary, and the complexity is not too high.

#define _debug(x) cerr<<#x<<" = "<<x<<endl

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;


template<typename _Tp, const int MAXMatrixSize>
struct Matrix {
    
    
    _Tp m[MAXMatrixSize][MAXMatrixSize];
    _Tp mod = 0;

    Matrix() {
    
    
        memset(m, 0, sizeof m);
    }

    Matrix(int _mod) : mod(_mod) {
    
    
        memset(m, 0, sizeof m);
    }

    void init1() {
    
    
        //*this = Matrix(mod);
        set(0, 0, 1);
        set(1, 1, 1);
//        for (int i = 0; i < MAXMatrixSize; i++)
//            m[i][i] = 1;
    }

    inline void set(const int &r, const int &c, const _Tp &v) {
    
     this->m[r][c] = v; }

    inline _Tp get(const int &r, const int &c) {
    
     return this->m[r][c]; }

    inline void setMod(const _Tp &_mod) {
    
     this->mod = _mod; }

    inline Matrix operator*(const Matrix t) {
    
    
        Matrix res(mod);//= Matrix(mod);
        res.setMod(mod);
        for (int i = 0; i < MAXMatrixSize; i++)
            for (int j = 0; j < MAXMatrixSize; j++)
                for (int k = 0; k < MAXMatrixSize; k++)
                    res.m[i][j] = (res.m[i][j] + m[i][k] * t.m[k][j]) % mod;
        return res;
    }
};

typedef Matrix<ll, 2> mat;

mat A, B;
ll x0, x1, a, b;
ll mo, len;
char n[1000059];

inline mat fpow(mat base, ll exp) {
    
    
    mat res(mo);
    res.init1();
    while (exp) {
    
    
        if (exp & 1)res = res * base;
        exp >>= 1;
        base = base * base;
    }
    return res;
}

inline ll calc() {
    
    

    len = strlen(n);
    //reverse(n, n + len);

    mat res(mo);
    res.init1();
    mat base = B;

    for (int i = len - 1; i >= 0; --i) {
    
    
        if (n[i] > '0')
            res = res * fpow(base, n[i] - '0');
        base = fpow(base, 10);
    }

    res = A * res;
    return res.get(0, 0);
}


int main() {
    
    

    scanf("%lld%lld%lld%lld", &x0, &x1, &a, &b);
    scanf("%s %lld", n, &mo);

    A = mat(mo);
    A.set(0, 0, x0);
    A.set(0, 1, x1);

    B = mat(mo);
    B.set(0, 0, 0);
    B.set(0, 1, b);
    B.set(1, 0, 1);
    B.set(1, 1, a);

    printf("%lld\n", calc());
    return 0;
}
/*




 * */

Guess you like

Origin blog.csdn.net/Tighway/article/details/98114977