Fibonacci
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 16 Accepted Submission(s) : 14
In the Fibonacci integer sequence, F0 = 0, F1 = 1, andFn = Fn − 1 + Fn − 2 forn ≥ 2. For example, the first ten terms of the Fibonacci sequence are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
An alternative formula for the Fibonacci sequence is
.
Given an integer n, your goal is to compute the last 4 digits of Fn.
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number −1.
For each test case, print the last four digits of Fn. If the last four digits ofFn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., printFn mod 10000).
0 9 999999999 1000000000 -1
0 34 626 6875
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int mod = 10000;
#define N 2
struct Matrix
{
int a[N][N];
};
Matrix A = {1, 1, 1, 0};
Matrix B = {1, 1, 1, 0};
void mul(int A[][N], int B[][N])
{
int R[N][N] = zero;
int sum;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
sum = 0;
for (int k = 0; k < N; k++)
{
sum += A[i][k] * B[k][j];
}
R[i][j] = sum % mod;
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
A[i][j] = R[i][j];
}
}
}
void cal(Matrix &R, int n)
{
if (n == 1)
{
R = B;
return;
}
cal(R, n / 2);
mul(R.a, R.a);
if (n & 1)mul(R.a, B.a);
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int n;
while (scanf("%d", &n), n != -1)
{
if (n == 0)
{
printf("0\n");
continue;
}
cal(A, n);
printf("%d\n", A.a[0][1]);
}
return 0;
}
模板2
题目
A Simple Math Problem
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2235 Accepted Submission(s): 1305
If x < 10 f(x) = x.
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);
And ai(0<=i<=9) can only be 0 or 1 .
Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m.
In each case, there will be two lines.
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9.
10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
45 104
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
struct node
{
int m[10][10];
};
node b, tp, res, init;
int n, m;
node juzhen(node a, node b)
{
node c;
int i, j, k;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
c.m[i][j] = 0;
for (int k = 0; k < 10; k++)
{
c.m[i][j] += a.m[i][k] * b.m[k][j];
}
c.m[i][j] %= m;
}
}
return c;
}
void f(int temp)
{
tp = b;
b = res;
while (temp)
{
if (temp & 1)
b = juzhen(b, tp);
tp = juzhen(tp, tp);
temp = temp >> 1;
}
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int i, j, sum;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (i - 1 == j)
init.m[i][j] = 1;
else
init.m[i][j] = 0;
}
}
while (scanf("%d%d", &n, &m) + 1)
{
b = init;
for (i = 0; i < 10; i++)
scanf("%d", &b.m[0][i]);
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (i == j)
res.m[i][j] = 1;
else
res.m[i][j] = 0;
}
}
if (n >= 10)
{
sum = 0;
f(n - 9);
for (i = 0; i < 10; i++)
sum += (b.m[0][i] * (9 - i))%m;
printf("%d\n", sum % m);
}
else
printf("%d\n", n % m);
}
return 0;
}