SOJ-2984(Fibonacci,矩阵连乘简化计算)

typedef int ll;
#define MOD 10000
int main()
{
	//freopen("Fibonacci.in", "r", stdin);
	//freopen("Fibonacci.out", "w", stdout);
    ll n;
    ll t[10000];
    while (scanf("%d", &n) == 1 && n != -1){
        if (n == 0) {
            printf("0\n");
            continue ;
        }
        --n;//以下程序基于F0=1,F1=1,所以要做预处理
        ll a = 1;
        ll b = 1;
        ll c = 1;
        ll d = 0;
        ll index = 0;
        while (n) {
            t[index++] = n % 2;
            n /= 2;
        }
        ll i;
        for (i = index - 2; i >= 0; --i) {
            if (t[i] == 0) {
                ll t1 = a, t2 = b, t3 = c, t4 = d;
                a = t1 * t1 + t2 * t3;
                b = t1 * t2 + t2 * t4;
                c = t3 * t1 + t4 * t3;
                d = t3 * t2 + t4 * t4;
            } else {
                ll t1 = a, t2 = b, t3 = c, t4 = d;
                a = t1 * t1 + t2 * t3;
                b = t1 * t2 + t2 * t4;
                c = t3 * t1 + t4 * t3;
                d = t3 * t2 + t4 * t4;
				a %= MOD;
				b %= MOD;
				c %= MOD;
				d %= MOD;
                t1 = a, t2 = b, t3 = c, t4 = d;
                a = t1 * 1 + t2 * 1;
                b = t1 * 1 + t2 * 0;
                c = t3 * 1 + t4 * 1;
                d = t3 * 1 + t4 * 0;
            }
            a %= MOD;
            b %= MOD;
            c %= MOD;
            d %= MOD;
        }
        printf("%d\n", a);
    }
    return 0;
}


 

你可能感兴趣的:(c)