QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#795806#4867. So Easy!JEdwardAC ✓10ms3696kbC++201.5kb2024-12-01 01:19:432024-12-01 01:19:43

Judging History

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

  • [2024-12-01 01:19:43]
  • 评测
  • 测评结果:AC
  • 用时:10ms
  • 内存:3696kb
  • [2024-12-01 01:19:43]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
using namespace std;
 
#define N 2
#define ll long long
 
struct Matrix {
    ll v[N][N];
};
 
Matrix A,B={1L,0L,0L,1L};
ll M;
 
Matrix mul(Matrix m1,Matrix m2,ll M){
    int i,j,k;
    Matrix c;
    for(i = 0; i < N; i++)
        for(j = 0; j < N; j++){
            c.v[i][j] = 0;
            for(k = 0; k < N; k++){
                c.v[i][j] += (m1.v[i][k]*m2.v[k][j])%M;
                c.v[i][j] %= M;
            }
            c.v[i][j] %= M;
    }
    return c;
}
 
Matrix Mpow(Matrix A,Matrix B,ll n,ll M){
    Matrix x = A,c = B;
    while(n >= 1){
        if(n&1L)c = mul(c,x,M);
        n >>= 1;
        x = mul(x,x,M);
    }
    return c;
}
 
int main() {
    //freopen("F:\\problem A\\in.txt","r",stdin);
    //freopen("F:\\problem A\\out.txt","w",stdout);
    ll n, a, b, t;
    while(cin>>a>>b>>n>>M){
        if(n == 1){
            cout << (2*(a%M)) % M <<endl;continue;
        }
        if(n == 2){
            cout << ((2*((a%M)*(a%M))%M)%M + (2*b)%M) % M <<endl;continue;
        }
        A.v[0][0] = (2*(a%M)) % M;
        t = a;
        t *= a;
        A.v[0][1] = 1;
        t = b - t;
        while(t < 0)t += M;
        A.v[1][0] = t;
        A.v[1][1] = 0;
        Matrix p = Mpow(A,B,n-2,M), q = {((2*((a%M)*(a%M))%M)%M + (2*b)%M) % M, (2*(a%M)) % M, 0L, 0L};
        p = mul(q,p,M);
        cout<<p.v[0][0]<<endl;
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 10ms
memory: 3696kb

input:

2 3 1 2012
2 3 2 2012
2 2 1 2012
31603 998691525 860250282 20381
4062 16491843 2741468 17921
26061 679145378 8235021 22991
16225 263237214 865378598 7182
18902 357253267 137732528 3407
25414 645847050 674769818 7114
6903 47648125 254957745 1556
16577 274779382 158233677 1099
939 880815 990631973 610...

output:

4
14
4
16026
12458
669
860
1778
3014
1524
253
5833
5554
5972
2764
2585
15006
7922
1156
312
12362
1570
5631
9732
13890
2981
3242
21364
1992
21044
8621
9794
11113
5616
1981
1194
49
9320
18894
19060
8148
896
903
78
2476
11226
9348
10281
1922
18
14946
1010
4647
2916
5598
4722
3972
5888
2456
14040
7037
7...

result:

ok 4028 lines