hdu 5459 Jesus Is Here 2015沈阳网络赛 递推

题目

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5459

题目来源:2015沈阳网络赛第三题

简要题意: s1=c,s2=ff,si=si2+si1 ,求 sn c 之间的距离之和。

数据范围:1T100;3n201314

题解

模数 530600414 是一个妹纸的QQ,看大神花式出题秀恩爱。

首先考虑距离之间肯定是有递推关系。

disi1+disi2 有了之后就差两段之间的c的距离之和。

而我们很好统计出长度和c的个数。

再进一步考虑,我们若是知道一段中c到左端的距离之和和到右端的距离之后就可以得到两段之间的c的距离之和了。

实现

维护五个数组

  • l 表示到左边距离之和
  • r表示到右边距离+1之和(方便计算)
  • len 表示长度
  • cnt 表示c的个数
  • res 表示结果,即距离之和

其中的递推关系画个图基本就能想明白了,代码中有就不赘述了。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 201444;
const LL MOD = 530600414;
LL len[N];
LL cnt[N];
LL l[N];
LL r[N];
LL res[N];

void init(int n) {
    len[3] = 3, len[4] = 5;
    l[4] = 2;
    r[3] = r[4] = 3;
    cnt[3] = cnt[4] = 1;
    for (int i = 5; i <= n; i++) {
        cnt[i] = (cnt[i-1] + cnt[i-2]) % MOD;
        len[i] = (len[i-1] + len[i-2]) % MOD;
        l[i] = (l[i-1] + l[i-2] + cnt[i-1]*len[i-2]%MOD) % MOD;
        r[i] = (r[i-1] + r[i-2] + cnt[i-2]*len[i-1]%MOD) % MOD;
        res[i] = (res[i-1] + res[i-2] + r[i-2]*cnt[i-1]%MOD + l[i-1]*cnt[i-2]%MOD) % MOD;
    }
}
int main()
{
    init(201314);
    int t, n, cas = 1;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        printf("Case #%d: %I64d\n", cas++, res[n]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值