Cod sursa(job #3370197)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 24 octombrie 2026 14:06:43
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#define int long long
using namespace std;
ifstream fin("kfib.in");
ofstream fout("kfib.out");
int mod=666013;
void inmultire(int a[3][3], int b[3][3])
{
    int c[3][3];
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            c[i][j]=0;
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            for(int k=0; k<2; k++)
                c[i][j]=(c[i][j]+a[i][k]*b[k][j]%mod)%mod;
    for(int i=0; i<2; i++)
        for(int j=0; j<2; j++)
            a[i][j]=c[i][j];
}
void exp(int a[3][3], int ans[3][3], int pow)
{
    if(pow)
    {
        if(pow%2==1)
            inmultire(ans, a);
        inmultire(a, a);
        exp(a, ans, pow/2);
    }
}
signed main()
{
    int n, ans[3][3], b[3][3];
    b[0][0]=1, b[0][1]=1, b[1][0]=1, b[1][1]=0;
    ans[0][0]=1, ans[0][1]=0, ans[1][0]=0, ans[1][1]=0;
    fin>>n;
    exp(b, ans, n);
    fout<<ans[0][1];
    return 0;
}