本文主要是介绍UVA - 10229 Modular Fibonacci,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:矩阵快速幂取模
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;long long mod;
long long p[]={1,1,1,0};void matrix(long long a[],long long b[],long long c[]){long long q = a[0]*b[0] + a[1]*b[2];long long w = a[0]*b[1] + a[1]*b[3];long long e = a[2]*b[0] + a[3]*b[2];long long r = a[2]*b[1] + a[3]*b[3];c[0] = q%mod,c[1] = w%mod,c[2] = e%mod,c[3] = r%mod;
}void pow_mod(long long a[],int n){if (n > 1){pow_mod(a,n/2);matrix(a,a,a);if (n % 2)matrix(a,p,a);}
}int main(){int n,m;while (scanf("%d%d",&n,&m) != EOF){if (n >= 1){mod = pow(2,m); long long a[10];memcpy(a,p,sizeof(p));pow_mod(a,n-1);printf("%lld\n",a[0]);}else printf("0\n");} return 0;
}
这篇关于UVA - 10229 Modular Fibonacci的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!