UVA-10518 How Many Calls ?
题目大意:给出n,b,问求第n个fibonacci数需要调用多少次函数,并输出其在b进制下的最后一个数
解题思路:设F(N)为求第n个fib数的时候需要调用函数的次数,f(N)为1,1,2,。。。的fib数,通过计算可以得到F(n) = F(n-2) + F(n-1) + 1
列出前十个F(N)和f(N)
f(0) = 1, f(1) = 1, f(2) = 2, f(3) = 3, f(4) = 5, f(5) = 8, f(6) = 13, f(7) = 21,f(8) = 34,f(9) = 55, f(10) = 89
F(0) = 1, F(1) = 1, F(2) = 3, F(3) = 5, F(4) = 9, F(5) = 15, F(6) = 25, F(7) = 41,F(8) = 67,F(9) = 109,F(10) = 175
通过上面的十个,可以看出F(N) = 2 * f(N) - 1,可以验证一下,假设f(n-2) = a, f(n-1) = b。 那么f(n) = a + b, F(n-2) = 2 * a - 1, F(n-1) = 2 * b - 1,F(n) = F(n-1) + F(n-2) + 1
可得F(n) = 2 * (a + b) - 1,所以求F(n)的话就可以通过f(n)间接求了,而求f(n)的话可以通过矩阵快速幂得到
#include<cstdio>
long long n, b;
struct Mar{
long long v[3][3];
}m;
Mar MarMul(Mar A, Mar B) {
Mar C;
C.v[0][0] = (A.v[0][0] * B.v[0][0] + A.v[0][1] * B.v[1][0]) % b;
C.v[0][1] = (A.v[0][0] * B.v[0][1] + A.v[0][1] * B.v[1][1]) % b;
C.v[1][0] = (A.v[1][0] * B.v[0][0] + A.v[1][1] * B.v[1][0]) % b;
C.v[1][1] = (A.v[1][0] * B.v[0][1] + A.v[1][1] * B.v[1][1]) % b;
return C;
}
Mar MarPow(Mar A, long long k) {
if(k == 1)
return A;
A = MarPow(A,k/2);
if(k % 2 == 0)
return MarMul(A,A);
else
return MarMul(MarMul(A,A),m);
}
int main() {
m.v[0][0] = m.v[0][1] = m.v[1][0] = 1;
m.v[1][1] = 0;
int mark = 1;
while(scanf("%lld%lld",&n,&b) == 2 && n + b) {
if(n == 1 || n == 0) {
printf("Case %d: %lld %lld 1\n",mark++,n,b);
continue;
}
Mar A = MarPow(m,n);
printf("Case %d: %lld %lld %lld\n",mark++,n,b,(2*A.v[0][0]-1+b) % b);
}
return 0;
}
UVA-10229 Modular Fibonacciu
题目大意:给你N和M,要求求出第N个fib数,然后模上2^M
解题思路:只需加上几个特判,然后直接用矩阵快速幂求出就可以了,
1.N == 0 || M == 0 直接输出0
2.N == 1 输出1
#include<cstdio>
long long M;
struct Mar{
long long v[3][3];
}m;
Mar MarMul(Mar A, Mar B) {
Mar C;
C.v[0][0] = (A.v[0][0] * B.v[0][0] + A.v[0][1] * B.v[1][0]) % M;
C.v[0][1] = (A.v[0][0] * B.v[0][1] + A.v[0][1] * B.v[1][1]) % M;
C.v[1][0] = (A.v[1][0] * B.v[0][0] + A.v[1][1] * B.v[1][0]) % M;
C.v[1][1] = (A.v[1][0] * B.v[0][1] + A.v[1][1] * B.v[1][1]) % M;
return C;
}
Mar MarPow(Mar A,long long k) {
if(k == 1)
return A;
A = MarPow(A,k/2);
if(k % 2 == 0)
return MarMul(A,A);
else
return MarMul(MarMul(A,A),m);
}
int main() {
long long n;
m.v[0][0] = m.v[0][1] = m.v[1][0] = 1;
m.v[1][1] = 0;
while(scanf("%lld%lld",&n,&M) == 2) {
if(n == 1) {
printf("1\n");
continue;
}
if(n == 0 || M == 0) {
printf("0\n");
continue;
}
M = 1 << M;
Mar A = MarPow(m,n);
printf("%lld\n",A.v[0][1] % M);
}
return 0;
}
UVA-763 Fibinary Numbers
题目大意:给你两个Fibinary进制数,要求你输出相加后的结果
解题思路:先把两个相加,再进行讨论
1.两个相邻的位出现大于等于1的数,设第i位和第i+1位出现了这种情况,只需第i+2位++,i和i-1位--
2.某一位上是大于1的情况,这就要分这一位是在哪了
A.如果再最后一位的话,也就第0位,那么只需第1位++,第0位-2即可
B.如果这一位在第1位,那么只需第0位和第2位++,第1位-2
C.如果这位在除了第0或1位的情况,设在第i位,只需第i-2位++,i+1位--
以上这些情况都可以推导出来的,这里就不推导了
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 150
char str1[maxn], str2[maxn];
int ans[maxn];
void solve() {
memset(ans,0,sizeof(ans));
int len1 = strlen(str1);
for(int i = len1 - 1, k = 0; i >= 0; i--, k++)
ans[k] += str1[i] - '0';
int len2 = strlen(str2);
for(int i = len2 - 1, k = 0; i >= 0; i--, k++)
ans[k] += str2[i] - '0';
int i;
while(1) {
for(i = 0; i < maxn; i++) {
if(ans[i] && ans[i+1]) {
ans[i+2]++;
ans[i]--;
ans[i+1]--;
break;
}
else if(ans[i] > 1) {
if(i == 0) {
ans[i+1]++;
ans[i] -= 2;
}
else if (i == 1) {
ans[i-1]++;
ans[i+1]++;
ans[i] -= 2;
}
else {
ans[i+1]++;
ans[i-2]++;
ans[i] -= 2;
}
break;
}
}
if(i == maxn)
break;
}
int end = maxn - 1;
while(!ans[end] && end)
end--;
while(end >= 0)
printf("%d",ans[end--]);
printf("\n");
}
int main() {
int bo = 1;
while(scanf("%s%s",str1,str2) == 2) {
if(bo)
bo = 0;
else
printf("\n");
solve();
}
return 0;
}
UVA-10236 The Fibonacci Prime
题目大意:要求输出第N个fib素数的前9位
解题思路:fib素数定理:斐波那契数列中,若i为素数(i>5),则fib(i)为素数
这样的话,就先打表,但这里的表要改两个位置,本来的表第1个素数是2,第2个素数是3,这里要改成第1个是3,第二个是4,这是题目给的
然后需要处理的就是求出来的数的前9位,这里用的是long double 确保精度。
#include<cstdio>
#include<cstring>
#define maxn 250010
#define maxm 22010
int vis[maxn];
int prime[maxm], count ;
long double fib[maxn];
void get_prime() {
memset(vis,0,sizeof(vis));
count = 0;
for(int i = 2; i <= maxn; i++)
if(!vis[i]) {
prime[++count] = i;
for(int j = 2 * i; j <= maxn; j += i)
vis[j] = 1;
}
prime[1] = 3; prime[2] = 4;
}
void get_fib() {
const long double E = 1e9;
fib[1] = 1; fib[2] = 1; fib[3] = 2; fib[4] = 3;
int flag = 0;
for(int i = 5; i <= maxn; i++) {
if(flag) {
fib[i] = fib[i-1] + fib[i-2] / 10;
flag = 0;
}else
fib[i] = fib[i-1] + fib[i-2];
if(fib[i] > E) {
fib[i] /= 10;
flag = 1;
}
}
}
int main() {
get_prime();
get_fib();
int n;
while(scanf("%d",&n) == 1) {
printf("%d\n",(int)fib[prime[n]]);
}
return 0;
}