olp_61 Ước số

Xem dạng PDF

Gửi bài giải

Điểm: 10,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 1G
Input: stdin
Output: stdout

Nguồn bài:
OLP Tin học 2023
Dạng bài


Bình luận

Hãy đọc nội quy trước khi bình luận.



  • 0
    01_ThaiBao_252AIS  đã bình luận lúc 2, Tháng 12, 2026, 11:12

    include <bits/stdc++.h>

    using namespace std;

    define ll long long

    ll m;

    struct matrix { ll f[2][2]; };

    matrix mul(matrix a, matrix b) { matrix c; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { c.f[i][j] = 0; for (int k = 0; k < 2; k++) { // ep sang int128 de nhan khong bi tran so c.f[i][j] = (c.f[i][j] + (int128)a.f[i][k] * b.f[k][j]) % m; } } } return c; }

    matrix power(matrix a, ll n) { matrix res; res.f[0][0] = 1; res.f[0][1] = 0; res.f[1][0] = 0; res.f[1][1] = 1; while (n > 0) { if (n % 2 == 1) res = mul(res, a); a = mul(a, a); n /= 2; } return res; }

    ll gcd(ll a, ll b) { while (b != 0) { ll r = a % b; a = b; b = r; } return a; }

    int main() { iosbase::syncwith_stdio(false); cin.tie(0);

    ll a, b;
    cin >> a >> b >> m;
    
    ll g = gcd(a, b);
    if (g == 0) {
        cout << 0;
        return 0;
    }
    if (g == 1) {
        cout << 1 % m;
        return 0;
    }
    
    matrix base;
    base.f[0][0] = 1; base.f[0][1] = 1;
    base.f[1][0] = 1; base.f[1][1] = 0;
    
    matrix ans = power(base, g - 1);
    cout << ans.f[0][0] % m;
    
    return 0;
    

    }


  • 0
    01_ThaiBao_252AIS  đã bình luận lúc 2, Tháng 12, 2026, 11:12

    include <bits/stdc++.h>

    using namespace std;

    define ll long long

    ll m;

    struct matrix { ll f[2][2]; };

    matrix mul(matrix a, matrix b) { matrix c; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { c.f[i][j] = 0; for (int k = 0; k < 2; k++) { // ep sang int128 de nhan khong bi tran so c.f[i][j] = (c.f[i][j] + (int128)a.f[i][k] * b.f[k][j]) % m; } } } return c; }

    matrix power(matrix a, ll n) { matrix res; res.f[0][0] = 1; res.f[0][1] = 0; res.f[1][0] = 0; res.f[1][1] = 1; while (n > 0) { if (n % 2 == 1) res = mul(res, a); a = mul(a, a); n /= 2; } return res; }

    ll gcd(ll a, ll b) { while (b != 0) { ll r = a % b; a = b; b = r; } return a; }

    int main() { iosbase::syncwith_stdio(false); cin.tie(0);

    ll a, b;
    cin >> a >> b >> m;
    
    ll g = gcd(a, b);
    if (g == 0) {
        cout << 0;
        return 0;
    }
    if (g == 1) {
        cout << 1 % m;
        return 0;
    }
    
    matrix base;
    base.f[0][0] = 1; base.f[0][1] = 1;
    base.f[1][0] = 1; base.f[1][1] = 0;
    
    matrix ans = power(base, g - 1);
    cout << ans.f[0][0] % m;
    
    return 0;
    

    }