Cod sursa(job #1379159)

Utilizator corul_barbatescUNIBUC Kira96 lockmihai corul_barbatesc Data 6 martie 2015 16:38:25
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 2.29 kb
/*
    Those without power, seek us!
    Those with power, fear us!
    We are the Order of the Black Knights!
*/

#include<fstream>
#include<cstdio>
#include<map>
#include<set>
#define FIT(a,b) for(vector<int >::iterator a=b.begin();a!=b.end();a++)
#define FITP(a,b) for(vector<pair<int,int> >::iterator a=b.begin();a!=b.end();a++)
#define RIT(a,b) for(vector<int>::reverse_iterator a=b.end();a!=b.begin();++a)
#include<stack>
#define ROF(a,b,c) for(int a=b;a>=c;--a)
#include<vector>
#include<algorithm>
#define FOR(a,b,c) for(int a=b;a<=c;++a)
#define REP(a,b) for(register int a=0;a<b;++a)
#include<cstring>
#include<ctime>
#include<bitset>
#include<cmath>
#include<iomanip>
#include<set>
#define f cin
#define g cout
#include<queue>
#define debug cerr<<"OK";
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ull unsigned int
#define mod 1000000009LL
#define SQR 350
#define db double
#define inf 1<<30
#define div fdasfasd
#define hash dsafdsfds
#define od 100003
#define mod 666013
#define DIM 60010000
#define base 256
#define bas 255
#define N 1000100
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("kfib.in");
ofstream g("kfib.out");
/*
    int dx[]={0,0,0,1,-1};
    int dy[]={0,1,-1,0,0};
*/
ll n,A[3][3],B[3][3],C[3][3];
int main ()
{
    f>>n;
    if(!n)
    {
        g<<"0";
        return 0;
    }
    n--;
    A[1][1]=A[1][2]=A[2][1]=1;
    B[1][1]=B[2][2]=1;
    while(n)
    {
        if(n&1)
        {
            FOR(i,1,2)
            FOR(j,1,2)
            C[i][j]=0;

            FOR(i,1,2)
            FOR(j,1,2)
            FOR(k,1,2)
            {
                C[i][j]+=A[i][k]*B[k][j]%mod;
                if(C[i][j]>=mod)
                    C[i][j]-=mod;
            }
            FOR(i,1,2)
            FOR(j,1,2)
            B[i][j]=C[i][j];
        }
            FOR(i,1,2)
            FOR(j,1,2)
            C[i][j]=0;
        FOR(i,1,2)
        FOR(j,1,2)
        FOR(k,1,2)
        {
            C[i][j]+=A[i][k]*A[k][j]%mod;
            if(C[i][j]>=mod)
                C[i][j]-=mod;
        }
        FOR(i,1,2)
        FOR(j,1,2)
        A[i][j]=C[i][j];
        n>>=1;
    }
    g<<B[1][1];
    return 0;
}