結果

問題 No.1050 Zero (Maximum)
ユーザー milanis48663220milanis48663220
提出日時 2020-05-31 01:41:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 2,199 bytes
コンパイル時間 1,305 ms
コンパイル使用メモリ 92,808 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-26 17:32:10
合計ジャッジ時間 2,005 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 21 ms
6,940 KB
testcase_03 AC 5 ms
6,944 KB
testcase_04 AC 18 ms
6,944 KB
testcase_05 AC 18 ms
6,940 KB
testcase_06 AC 9 ms
6,940 KB
testcase_07 AC 11 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 6 ms
6,940 KB
testcase_10 AC 24 ms
6,940 KB
testcase_11 AC 19 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 26 ms
6,944 KB
testcase_17 AC 28 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

template <typename T>
struct matrix{
    int n, m;
    vector<vector<T>> dat;
    matrix(int n_, int m_){
        n = n_; m = m_;
        for(int i = 0; i < n; i++){
            dat.push_back(vector<T>(m));
        }
    }
};

template <typename T>
bool prod(matrix<T> a, matrix<T> b, matrix<T> &ans){
    if(a.m != b.n) return false;
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            ans.dat[i][j] = 0;
            for(int k = 0; k < b.m; k++){
                ans.dat[i][j] += a.dat[i][k]*b.dat[k][j];
                ans.dat[i][j] %= MOD;
            }
        }
    }
    return true;
}

template <typename T>
void copy_mat(matrix<T> a, matrix<T> &b){
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            b.dat[i][j] = a.dat[i][j];
        }
    }
}

template <typename T>
void pow(matrix<T> a, ll n, matrix<T> &ans){
    matrix<T> buf(a.n, a.n);
    matrix<T> tmp(a.n, a.n);
    copy_mat(a, tmp);

    for(int i = 0; i < a.n; i++) {
        for(int j = 0; j < a.n; j++){
            ans.dat[i][j] = 0;
        }
        ans.dat[i][i] = 1;
    }
    
    for(int i = 0; i <= 30; i++){
        ll m = (ll)1 << i;
        if(m&n){
            prod(tmp, ans, buf);
            copy_mat(buf, ans);
        }
        prod(tmp, tmp, buf);
        copy_mat(buf, tmp);
    }
}

template <typename T>
void print_mat(matrix<T> a){
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            cout << a.dat[i][j] << ' ';
        }
        cout << endl;
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int M, K;
    cin >> M >> K;
    matrix<ll> m(M,M);
    for(int i = 0; i < M; i++){
        for(int j = 0; j < M; j++){
            int add = (i+j)%M;
            int mul = (i*j)%M;
            m.dat[i][add]++;
            m.dat[i][mul]++;            
        }
    }
    matrix<ll> ans(M, M);
    pow<ll>(m, K, ans);
    cout << ans.dat[0][0] << endl;
}
0