哈尔滨理工大学软件学院OJ 1451 经商(大树/ 斐波那契数列)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    long long a[3][3];
}T;
const int MOD=1000000007;

node muli(node x,node y)
{
    node temp;
    for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            temp.a[i][j]=0;
            for(int k=1;k<=2;k++)
            {
                temp.a[i][j]=(temp.a[i][j]+(x.a[i][k]*y.a[k][j]))%MOD;
            }
        }
    }
    return temp;
}
int Kuick(long long n)
{
    node temp;
    temp.a[1][1]=1; temp.a[1][2]=1;
    temp.a[2][1]=1; temp.a[2][2]=0;

    node ans;
    ans.a[1][1]=1;  ans.a[1][2]=0;
    ans.a[2][1]=0;  ans.a[2][2]=1;

    while(n)
    {
        if(n%2==1)  ans=muli(ans,temp);
        n=n/2;
        temp=muli(temp,temp);
    }
    /*for(int i=1;i<=2;i++)
    {
        for(int j=1;j<=2;j++)
        {
            printf("%d ",ans.a[i][j]);
        }
        printf("\n");
    }*/
    return (ans.a[1][2])%MOD;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long a;    scanf("%lld",&a);
        if(a==1)    {printf("1\n");continue;}
        if(a==2)    {printf("2\n");continue;}
        printf("%d\n",Kuick(a+1));
    }
}

1451: 布置会场(II)

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 169  Solved: 60
[ Submit][ Status][ Web Board]

Description

 

小d接到了一个布置会场的任务。

他需要将贵宾观众席的椅子排成一排,一共需要N个。

上级领导指示,他只能使用两种椅子。(A类型和B类型)并且假设每种椅子的数量都是无限的。

而其如果想要摆置一个B类型的椅子,对应就需要必须有连续两个一起布置。换句话说,就是如果出现了B类型的椅子,其必须且只有两个连着B类型的椅子。

小d突然想知道对应N个椅子排成一列,他能够有多少种布置的方式.

Input

 

本题包含多组输入第一行输入一个整数t,表示测试数据的组数

每组测试数据包含一行,输入一个整数N,表示一共需要摆放的椅子数量

t<=1000

1<=N<=100000000000000000(10^18)

Output

 每组测试数据输出包含一行,表示一共有多少种布置的方式,方案数可能会很大,输出对1000000007取摸的结果。

Sample Input

224

Sample Output

25

HINT

第一个样例,AA,BB两种方案。 

第二个样例,AAAA,BBBB,AABB,ABBA,BBAA五种方案  对于ABBB 因为有连续3个B类型椅子所以不可行

Source

Fibonacci
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7241   Accepted: 5131

Description

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 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.

Input

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.

Output

For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).

Sample Input

0
9
999999999
1000000000
-1

Sample Output

0
34
626
6875

Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

.

Source


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值