combinatorics implementation math matrices number theory *2100

Please click on ads to support us..

C++ Code:

/* In the name of God */

#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <list>
#include <map>
#include <numeric>
#include <limits>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define POP pop_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"

const ll INF = (ll)8e18 + 1386;
const ld EPS = 0.000000000000001;
int MOD = 1e9 + 7;

inline int mod_add(int a, int b){ int res = a + b; return (res >= MOD? res - MOD : res); }
inline int mod_neg(int a, int b){ int res = (abs(a - b) < MOD? a - b : (a - b) % MOD); return (res < 0? res + MOD : res); }
inline int mod_mlt(int a, int b){ return (1ll * a * b % MOD); }
inline string intToString(ll a){ char x[100]; sprintf(x, "%lld", a); string s = x; return s; }
inline ll stringToInt(string s){ ll res; char x[100]; strcpy(x, s.c_str()); sscanf(x, "%lld", &res); return res; }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }

const int MAXN = 2e5 + 5;

struct Mat {
    vector<vector<ll>> mat;
    int height, width;
    Mat (int a, int b, ll fill, ll diam = -23){
        height = a, width = b;
        vector<ll> row;
        for (int i = 0; i <= b; i++) row.push_back(fill);
        for (int i = 0; i <= a; i++) mat.push_back(row);
        if (diam != -23) for (int i = 1; i <= a; i++) mat[i][i] = diam;
    };
    Mat (){
        height = width = MAXN - 1; // CHECK THIS
        vector<ll> row;
        for (int i = 0; i <= width; i++) row.push_back(0);
        for (int i = 0; i <= height; i++) mat.push_back(row);
    };
};

Mat mat_mlt(Mat m1, Mat m2){ // i,k,j for square
    int _N = m1.height, _M = m1.width, _K = m2.width;
    Mat res(_N, _K, 0);
    for (int i = 1; i <= _N; i++){
        for (int j = 1; j <= _M; j++){
            for (int k = 1; k <= _K; k++){
                res.mat[i][k] = mod_add(res.mat[i][k], mod_mlt(m1.mat[i][j], m2.mat[j][k]));
            }
        }
    }
    return res;
}

Mat mat_pow(Mat m1, ll n){
    if (!n) return Mat(m1.height, m1.width, 0, 1);
    Mat res = mat_pow(m1, n >> 1);
    res = mat_mlt(res, res);
    if (n & 1) res = mat_mlt(res, m1);
    return res;
}

ll poww(ll x, ll y, int p = MOD){ // OOPS!
    ll res = 1;
    x %= p;
    while (y > 0){
        if (y & 1)
            res = (res * x) % p;
        y >>= 1;
        x = (x * x) % p;
    }
    return res;
}

ll _mlt(ll x, ll y){
    if (x == 0 || y == 0) return 0;
    if (x > INF / y) return INF;
    return x * y;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll n, k; int l;
    cin >> n >> k >> l >> MOD;
    ll _p2 = 1;
    for (int i = 1; i <= l; i++){
        _p2 = _mlt(_p2, 2);
    }
    if (k >= _p2) return cout << 0, 0;

    Mat fib(2, 2, 1); fib.mat[2][2] = 0;
    fib = mat_pow(fib, n + 1);

    int ans0 = fib.mat[1][1];
    int ans1 = mod_neg(poww(2, n), ans0);
    int c[2] = {0, 0};
    for (int i = 0; i < l; i++){
        int B = (k >> 1ll * i) & 1;
        c[B]++;
    }
    cout << mod_mlt(poww(ans0, c[0]), poww(ans1, c[1]));
    return 0;
}


Comments

Submit
0 Comments
More Questions

1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons
200B - Drinks
13A - Numbers
129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps