QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#135425#5661. Multi-LaddersNicolas125841AC ✓1ms3584kbC++171.5kb2023-08-05 15:08:432023-08-05 15:08:46

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-05 15:08:46]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:3584kb
  • [2023-08-05 15:08:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const ll mod = 1000000007;

ll modpow(ll b, ll e) {
    ll ans = 1;
    for (; e; b = b * b % mod, e /= 2)
        if (e & 1) ans = ans * b % mod;
    return ans;
}

template<class T, int N> struct Matrix {
typedef Matrix M;
array<array<T, N>, N> d{};
M operator*(const M& m) const {
M a;
rep(i,0,N) rep(j,0,N)
rep(k,0,N) {a.d[i][j] += d[i][k]*m.d[k][j] % mod; a.d[i][j] %= mod;}
return a;
}
vector<T> operator*(const vector<T>& vec) const {
vector<T> ret(N);
rep(i,0,N) rep(j,0,N) {ret[i] += d[i][j] * vec[j] % mod; ret[i] %= mod;}
return ret;
}
M operator^(ll p) const {
assert(p >= 0);
M a, b(*this);
rep(i,0,N) a.d[i][i] = 1;
while (p) {
if (p&1) a = a*b;
b = b*b;
p >>= 1;
}
return a;
}
};

int main(){
    cin.tie(NULL)->sync_with_stdio(false);

    int t;
    cin >> t;

    while(t--){
        ll n, k, l;
        cin >> n >> k >> l;

        if(l < 2){
            cout << "0\n";
            continue;
        }

        Matrix<ll, 3> A;
        A.d = {{{{0, l-1, l-2}}, {{1, 0, 0}}, {{0, l-1, l-2}}}};

        vector<ll> vec = {1,0,0};

        vec = (A^(k-1)) * vec;
        
        cout << (modpow(modpow(((l - 1) + modpow(l - 2, 2)) % mod, k), n - 1) * l % mod) * ((vec[1] * (l - 1) % mod + vec[2] * (l - 2) % mod) % mod) % mod  << "\n";
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3584kb

input:

1
2 3 3

output:

162

result:

ok single line: '162'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

20
2 3 3
1 3 3
10 3 0
10 3 2
1 21 2
1 22 0
2000 15000 2000
12000 30000 200000
1000000000 3 3
2 1000000000 3
2 3 100000000
1000000000 1000000000 10
1000000000 3 100000000
2 1000000000 100000000
1 1000000000 10
1 1000000000 100000000
1 1000 100000000
1000000000 1000000000 0
1000000000 1000000000 1
100...

output:

162
6
0
0
0
0
349400141
243010659
52489881
53690844
176686901
218103365
558243892
991895211
693053429
883715672
80402569
0
0
311752813

result:

ok 20 lines