Logs Stacking(第二届中国计量大学ACM程序设计竞赛个人赛)

Logs Stacking(第二届中国计量大学ACM程序设计竞赛个人赛)

链接:https://ac.nowcoder.com/acm/contest/3190/I
来源:牛客网

在这里插入图片描述
示例1
输入
5
1
3
5
7
2000000000

输出
1
2
5
13
3125

说明
In the third sample, you can accumulate 5 logs within such following ways:
First, you can put all the 5 logs at the ground floor.
Then, you can put 4 logs at the bottom layer, and there are 3 positions for the last log to pile up.
After that, you can also put 3 logs at the bottom layer, while the other 2 logs at the top layer.
Above all, you can get 1 + 3 + 1 = 5 figures in order to accumulate 5 logs.

本题其实就是一个斐波那契数,但是卡时间,所以运用矩阵快速幂快速求出斐波那契数即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
using namespace std;
 
const int maxn = 2;
const int mod = 10000;
 
struct ma
{
    int a[maxn][maxn];
    void init()
    {  
        memset(a, 0, sizeof(a));
        for (int i = 0; i < maxn; ++i) a[i][i] = 1;
    }
};
 
 
ma mul(ma a, ma b)
{
    ma ans;
    for (int i = 0; i < maxn; ++i)
    {
        for (int j = 0; j < maxn; ++j)
        {
            ans.a[i][j] = 0;
            for (int k = 0; k < maxn; ++k)
            {
                ans.a[i][j] += a.a[i][k] * b.a[k][j];
                ans.a[i][j] %= mod;
            }
        }
    }
    return ans;
}
 
ma qpow(ma a, int n)
{
    ma ans;
    ans.init();
    while (n)
    {
        if (n & 1) ans = mul(ans, a);
        a = mul(a, a);
        n /= 2;
    }
    return ans;
}
int main()
{
    long long n,T;
    scanf("%lld", &T);
    while (T--)
    {
        ma a;
        a.a[0][0] = 1;
        a.a[0][1] = 1;
        a.a[1][0] = 1;
        a.a[1][1] = 0;
        scanf("%lld", &n);
        ma ans = qpow(a, n);
        printf("%d\n", ans.a[0][1]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值