P1306 斐波那契公约数

思路:初看题意可能被吓到,不过肯定是不用求出第n,m个斐波那契数的,我是打表找规律发现gcd(f[n],f[m])=f[gcd(n,m)]的,所有莽了一发居然过了,看题解,证明很麻烦。。。

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define ll long long
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(false)
#define DEBUG cout<<endl<<"DEBUG"<<endl;
using namespace std;
const ll mod = 1e8;
ll siz, n, m;
struct mtx {
    ll x[2 + 1][2 + 1];
    mtx() {
        memset(x, 0, sizeof x);
    }
};
mtx operator *(const mtx &a, const mtx &b) {
    mtx c;
    for(int i = 0; i < siz; i++)
        for(int j = 0; j < siz; j++)
            for(int k = 0; k < siz; k++)
                c.x[i][j] = (c.x[i][j] + a.x[i][k] * b.x[k][j] % mod) % mod;
    return c;
}
mtx operator ^(mtx a, ll k) {
    mtx ret;
    for(int i = 0; i < siz; ++i)
        ret.x[i][i] = 1;
    while(k) {
        if(k & 1)
            ret = ret * a;
        a = a * a;
        k >>= 1;
    }
    return ret;
}
int main() {
    siz = 2;
    cin >> n >> m;
    n = __gcd(n, m);
    if(n <= 2) {
        cout << 1;
        return 0;
    }
    mtx ak;
    ak.x[0][0] = 1;
    ak.x[0][1] = 1;
    ak.x[1][0] = 1;
    mtx w;
    w = ak ^ (n - 2);
    ll ans = (w.x[0][0] + w.x[0][1]) % mod;
    cout << ans;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值