How Many CILS?

Source: Internet
Author: User

UVA_10518

It is easier to think of f (n) = f (n-1) + f (n-2) + 1, but if you can think of f (n) = 2 * F (n) -1 is not very easy. After reading the forum of ultraviolet A, I realized that it could have been represented like this, where F (n) is the Fibonacci number, with this formula, it is the first step, and the subsequent computing process is not troublesome.

Later, when we discussed it in the group, we suddenly found that S (n) = F (n) + F (n + 1)-1, S (n) is the first n sum of Fibonacci, then I thought of a conclusion I pushed before, f (n) = S (n)-F (n-1), and found that f (n) = 2 * F (n) -1 can be converted. Due to the fact that we do not know the expression S (n) At the time, we missed the opportunity to draw a conclusion.

Let's talk about the process of launching f (n) = S (n)-F (n). When we calculate the number of recursion times, each time you add a new row to the table, we will get the following table.

F (0) f (1) f (2) f (3) f (4) f (5) f (6)

1

1 1

1 1 2

1 1 2 3

1 1 2 3 5

1 1 2 3 5 8 13

If there is no number, it can be regarded as 0. The corresponding integer and the corresponding integer under f (I) are the values of f (I), which is equivalent to calculating f (n) = f (n-1) + f (n-2) + 1, each line is f (n) = f (n-1) + f (n) calculation, and then 1 another line written on the above.

In this way, we can clearly see that each row is a Fibonacci series, and it is not difficult to get f (n) = S (n)-F (n-1), so we can put S (n) if F (n) + F (n + 1)-1 is substituted, f (n) = 2 * F (n)-1 is obtained.

No matter what method is used, we get the expression f (n) now. After returning to the question below, the question is transformed into the value of f (n) % B. Because n is very large, we cannot calculate F (n) directly by recursive calculation, therefore, F (n) can be written as a matrix, and then the value of F (n) % B can be obtained using the rapid power modulo, and then the value of f (n) % B will exist.

#include<stdio.h>
#include<string.h>
#include<math.h>
long long int N, B;
long long int a[100][5], b[5];
void pow_mod(long long int n, int e)
{
if(n == 1)
{
a[e][0] = a[e][1] = a[e][2] = 1;
a[e][3] = 0;
return ;
}
pow_mod(n / 2, e + 1);
a[e][0] = (a[e + 1][0] * a[e + 1][0] + a[e + 1][1] * a[e + 1][2]) % B;
a[e][1] = (a[e + 1][0] * a[e + 1][1] + a[e + 1][1] * a[e + 1][3]) % B;
a[e][2] = (a[e + 1][2] * a[e + 1][0] + a[e + 1][3] * a[e + 1][2]) % B;
a[e][3] = (a[e + 1][2] * a[e + 1][1] + a[e + 1][3] * a[e + 1][3]) % B;
if(n % 2)
{
b[0] = (a[e][0] * a[0][0] + a[e][1] * a[0][2]) % B;
b[1] = (a[e][0] * a[0][1] + a[e][1] * a[0][3]) % B;
b[2] = (a[e][2] * a[0][0] + a[e][3] * a[0][2]) % B;
b[3] = (a[e][2] * a[0][1] + a[e][3] * a[0][3]) % B;
a[e][0] = b[0], a[e][1] = b[1], a[e][2] = b[2], a[e][3] = b[3];
}
}
void solve()
{
a[0][0] = a[0][1] = a[0][2] = 1;
a[0][3] = 0;
if(N == 0)
{
printf("1\n");
return ;
}
pow_mod(N, 1);
printf("%lld\n", (2 * a[1][0] + B - 1) % B);
}
int main()
{
int t = 0;
for(;;)
{
scanf("%lld%lld", &N, &B);
if(!N && !B)
break;
printf("Case %d: %lld %lld ", ++ t, N, B);
solve();
}
return 0;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.